Initial commit
[yaffs-website] / node_modules / tmp / test / dir-test.js
1 var
2   vows   = require('vows'),
3   assert = require('assert'),
4
5   path       = require('path'),
6   fs         = require('fs'),
7   existsSync = fs.existsSync || path.existsSync,
8
9   tmp    = require('../lib/tmp.js'),
10   Test   = require('./base.js');
11
12
13 function _testDir(mode) {
14   return function _testDirGenerated(err, name) {
15     assert.ok(existsSync(name), 'should exist');
16
17     var stat = fs.statSync(name);
18     assert.ok(stat.isDirectory(), 'should be a directory');
19
20     Test.testStat(stat, mode);
21   };
22 }
23
24 vows.describe('Directory creation').addBatch({
25   'when using without parameters': {
26     topic: function () {
27       tmp.dir(this.callback);
28     },
29
30     'should be a directory': _testDir(040700),
31     'should have the default prefix': Test.testPrefix('tmp-')
32   },
33
34   'when using with prefix': {
35     topic: function () {
36       tmp.dir({ prefix: 'something' }, this.callback);
37     },
38
39     'should not return with an error': assert.isNull,
40     'should return with a name': Test.assertName,
41     'should be a directory': _testDir(040700),
42     'should have the provided prefix': Test.testPrefix('something')
43   },
44
45   'when using with postfix': {
46     topic: function () {
47       tmp.dir({ postfix: '.txt' }, this.callback);
48     },
49
50     'should not return with an error': assert.isNull,
51     'should return with a name': Test.assertName,
52     'should be a directory': _testDir(040700),
53     'should have the provided postfix': Test.testPostfix('.txt')
54   },
55
56   'when using template': {
57     topic: function () {
58       tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
59     },
60
61     'should not return with error': assert.isNull,
62     'should return with a name': Test.assertName,
63     'should be a directory': _testDir(040700),
64     'should have the provided prefix': Test.testPrefix('clike-'),
65     'should have the provided postfix': Test.testPostfix('-postfix')
66   },
67
68   'when using name': {
69     topic: function () {
70       tmp.dir({ name: 'using-name' }, this.callback);
71     },
72
73     'should not return with an error': assert.isNull,
74     'should return with a name': Test.assertName,
75     'should be a directory': _testDir(040700),
76     'should have the provided name': Test.testName(path.join(tmp.tmpdir, 'using-name'))
77   },
78
79   'when using multiple options': {
80     topic: function () {
81       tmp.dir({ prefix: 'foo', postfix: 'bar', mode: 0750 }, this.callback);
82     },
83
84     'should not return with an error': assert.isNull,
85     'should return with a name': Test.assertName,
86     'should be a directory': _testDir(040750),
87     'should have the provided prefix': Test.testPrefix('foo'),
88     'should have the provided postfix': Test.testPostfix('bar')
89   },
90
91   'when using multiple options and mode': {
92     topic: function () {
93       tmp.dir({ prefix: 'complicated', postfix: 'options', mode: 0755 }, this.callback);
94     },
95
96     'should not return with an error': assert.isNull,
97     'should return with a name': Test.assertName,
98     'should be a directory': _testDir(040755),
99     'should have the provided prefix': Test.testPrefix('complicated'),
100     'should have the provided postfix': Test.testPostfix('options')
101   },
102
103   'no tries': {
104     topic: function () {
105       tmp.dir({ tries: -1 }, this.callback);
106     },
107
108     'should return with an error': assert.isObject
109   },
110
111   'keep testing': {
112     topic: function () {
113       Test.testKeep('dir', '1', this.callback);
114     },
115
116     'should not return with an error': assert.isNull,
117     'should return with a name': Test.assertName,
118     'should be a dir': function (err, name) {
119       _testDir(040700)(err, name);
120       fs.rmdirSync(name);
121     }
122   },
123
124   'unlink testing': {
125     topic: function () {
126       Test.testKeep('dir', '0', this.callback);
127     },
128
129     'should not return with error': assert.isNull,
130     'should return with a name': Test.assertName,
131     'should not exist': function (err, name) {
132       assert.ok(!existsSync(name), 'Directory should be removed');
133     }
134   },
135
136   'non graceful testing': {
137     topic: function () {
138       Test.testGraceful('dir', '0', this.callback);
139     },
140
141     'should not return with error': assert.isNull,
142     'should return with a name': Test.assertName,
143     'should be a dir': function (err, name) {
144       _testDir(040700)(err, name);
145       fs.rmdirSync(name);
146     }
147   },
148
149   'graceful testing': {
150     topic: function () {
151       Test.testGraceful('dir', '1', this.callback);
152     },
153
154     'should not return with an error': assert.isNull,
155     'should return with a name': Test.assertName,
156     'should not exist': function (err, name) {
157       assert.ok(!existsSync(name), 'Directory should be removed');
158     }
159   },
160
161   'unsafeCleanup === true': {
162     topic: function () {
163       Test.testUnsafeCleanup('1', this.callback);
164     },
165
166     'should not return with an error': assert.isNull,
167     'should return with a name': Test.assertName,
168     'should not exist': function (err, name) {
169       assert.ok(!existsSync(name), 'Directory should be removed');
170     },
171     'should remove symlinked dir': function(err, name) {
172       assert.ok(
173         !existsSync(name + '/symlinkme-target'),
174         'should remove target'
175       );
176     },
177     'should not remove contents of symlink dir': function(err, name) {
178       assert.ok(
179         existsSync(__dirname + '/symlinkme/file.js'),
180         'should not remove symlinked directory\'s content'
181       );
182     }
183   },
184
185   'unsafeCleanup === true with issue62 structure': {
186     topic: function () {
187       Test.testIssue62(this.callback);
188     },
189
190     'should not return with an error': assert.isNull,
191     'should return with a name': Test.assertName,
192     'should not exist': function (err, name) {
193       assert.ok(!existsSync(name), 'Directory should be removed');
194     }
195   },
196
197   'unsafeCleanup === false': {
198     topic: function () {
199       Test.testUnsafeCleanup('0', this.callback);
200     },
201
202     'should not return with an error': assert.isNull,
203     'should return with a name': Test.assertName,
204     'should be a directory': function (err, name) {
205       _testDir(040700)(err, name);
206       // make sure that everything gets cleaned up
207       fs.unlinkSync(path.join(name, 'should-be-removed.file'));
208       fs.unlinkSync(path.join(name, 'symlinkme-target'));
209       fs.rmdirSync(name);
210     }
211   },
212
213   'remove callback': {
214     topic: function () {
215       tmp.dir(this.callback);
216     },
217
218     'should not return with an error': assert.isNull,
219     'should return with a name': Test.assertName,
220     'removeCallback should remove directory': function (_err, name, removeCallback) {
221       removeCallback();
222       assert.ok(!existsSync(name), 'Directory should be removed');
223     }
224   }
225 }).exportTo(module);