Skip to content

Commit 45fd84f

Browse files
committed
fix #137
1 parent 8f73d89 commit 45fd84f

26 files changed

+827
-907
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
"os-tmpdir": "~1.0.2"
2626
},
2727
"devDependencies": {
28-
"vows": "~0.7.0"
28+
"vows": "~0.7.0",
29+
"mocha": "~3.4.2"
2930
},
3031
"main": "lib/tmp.js",
3132
"files": [
3233
"lib/"
3334
],
3435
"scripts": {
35-
"test": "vows test/*-test.js",
36+
"test": "vows test/legacy/*-test.js && mocha",
3637
"doc": "jsdoc -c .jsdoc.json"
3738
}
3839
}

test/assertions.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-octal */
2+
3+
var
4+
assert = require('assert'),
5+
fs = require('fs'),
6+
path = require('path'),
7+
existsSync = fs.existsSync || path.existsSync;
8+
9+
10+
module.exports.assertName = function assertName(name, expected) {
11+
assert.ok(typeof name == 'string');
12+
assert.ok(name.length > 0, 'an empty string is not a valid name');
13+
if (expected) {
14+
assert.equal(path.basename(name), expected, 'should be the expected name');
15+
}
16+
};
17+
18+
19+
module.exports.assertStat = function assertStat(name, mode) {
20+
var stat = fs.statSync(name);
21+
22+
/*
23+
// geteuid() and getegid() do not exist on Windows.
24+
// must use the effective gid and effective uid for testing
25+
if (process.geteuid) {
26+
assert.equal(stat.uid, process.geteuid(), 'should have the same UID');
27+
}
28+
if (process.getegid) {
29+
// FIXME does not always work as expected (setgid bit on parent directory)
30+
console.log('stat.gid ' + stat.gid);
31+
console.log('egid ' + process.getegid());
32+
assert.equal(stat.gid, process.getegid(), 'should have the same GUID');
33+
}
34+
*/
35+
36+
// mode values do not work properly on Windows. Ignore “group” and
37+
// “other” bits then. Ignore execute bit on that platform because it
38+
// doesn’t exist—even for directories.
39+
if (process.platform == 'win32') {
40+
assert.equal(stat.mode & 000600, mode & 000600);
41+
} else {
42+
assert.equal(stat.mode & 000777, mode);
43+
}
44+
};
45+
46+
47+
module.exports.assertPrefix = function assertPrefix(name, prefix) {
48+
assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
49+
};
50+
51+
52+
module.exports.assertPostfix = function assertPostfix(name, postfix) {
53+
assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
54+
};
55+
56+
57+
module.exports.assertProperResult = function assertProperResult(result, withfd) {
58+
assert.ok(result);
59+
assert.ok(result.name, 'should have a name');
60+
if (withfd) assert.ok(result.fd, 'should have an fd');
61+
else assert.ok(result.fd == undefined, 'should not have an fd');
62+
assert.ok(typeof result.removeCallback == 'function', 'should have a removeCallback');
63+
};
64+
65+
66+
module.exports.assertExists = function assertExists(name, isfile) {
67+
assert.ok(existsSync(name), 'should exist');
68+
var stat = fs.statSync(name);
69+
if (isfile) assert.ok(stat.isFile(), 'should be a file');
70+
else assert.ok(stat.isDirectory(), 'should be a directory');
71+
}
72+

test/dir-sync-test.js

Lines changed: 41 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -1,232 +1,46 @@
11
/* eslint-disable no-octal */
2+
// vim: expandtab:ts=2:sw=2
23

34
var
4-
vows = require('vows'),
55
assert = require('assert'),
6+
inbandStandardTests = require('./inband-standard.js'),
7+
tmp = require('../lib/tmp.js');
8+
9+
10+
describe('tmp', function () {
11+
describe('#dirSync()', function () {
12+
// API call standard inband tests
13+
describe('when running inband standard tests', function () {
14+
15+
inbandStandardTests(false, function before() {
16+
this.topic = tmp.dirSync(this.opts);
17+
});
18+
19+
describe('with invalid tries', function () {
20+
it('should result in an error on negative tries', function () {
21+
try {
22+
tmp.dirSync({tries: -1});
23+
assert.fail('should have failed');
24+
} catch (err) {
25+
assert.ok(err instanceof Error);
26+
}
27+
});
28+
});
29+
});
30+
31+
// API call issue specific inband tests
32+
describe('when running issue specific inband tests', function () {
33+
// add your issue specific tests here
34+
});
35+
36+
// API call standard outband tests
37+
describe('when running standard outband tests', function () {
38+
});
39+
40+
// API call issue specific outband tests
41+
describe('when running issue specific outband tests', function () {
42+
// add your issue specific tests here
43+
});
44+
});
45+
});
646

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

0 commit comments

Comments
 (0)