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