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