Skip to content

Commit ecd0094

Browse files
committed
fix #137
1 parent 8f73d89 commit ecd0094

21 files changed

+350
-105
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/dir-sync-test.js

Lines changed: 104 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/* eslint-disable no-octal */
22

33
var
4-
vows = require('vows'),
54
assert = require('assert'),
65

76
path = require('path'),
87
fs = require('fs'),
98
existsSync = fs.existsSync || path.existsSync,
10-
11-
tmp = require('../lib/tmp.js'),
12-
Test = require('./base.js');
9+
tmp = require('../lib/tmp.js');
1310

1411

1512
function _testDir(mode) {
@@ -23,98 +20,124 @@ function _testDir(mode) {
2320
};
2421
}
2522

26-
vows.describe('Synchronous directory creation').addBatch({
27-
'when using without parameters': {
28-
topic: function () {
29-
return tmp.dirSync();
30-
},
3123

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-
},
24+
function assertName(name, expected) {
25+
assert.ok(typeof name == 'string');
26+
assert.ok(name.length > 0, 'an empty string is not a valid name');
27+
if (expected) {
28+
assert.equal(path.basename(name), expected, 'should be the expected name');
29+
}
30+
}
3631

37-
'when using with prefix': {
38-
topic: function () {
39-
return tmp.dirSync({ prefix: 'something' });
40-
},
4132

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-
},
33+
function assertStat(stat, mode) {
34+
// getuid() and getgid() do not exist on Windows.
35+
if (process.getuid) {
36+
assert.equal(stat.uid, process.getuid(), 'should have the same UID');
37+
}
38+
if (process.getgid) {
39+
// FIXME does not always work as expected
40+
assert.equal(stat.gid, process.getgid(), 'should have the same GUID');
41+
}
42+
// mode values do not work properly on Windows. Ignore “group” and
43+
// “other” bits then. Ignore execute bit on that platform because it
44+
// doesn’t exist—even for directories.
45+
if (process.platform == 'win32') {
46+
assert.equal(stat.mode & 0666600, mode & 0666600);
47+
} else {
48+
assert.equal(stat.mode, mode);
49+
}
50+
}
4651

47-
'when using with postfix': {
48-
topic: function () {
49-
return tmp.dirSync({ postfix: '.txt' });
50-
},
5152

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-
},
53+
function assertPrefix(name, prefix) {
54+
assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
55+
}
5656

57-
'when using template': {
58-
topic: function () {
59-
return tmp.dirSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
60-
},
6157

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-
},
58+
function assertPostfix(name, postfix) {
59+
assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
60+
}
6761

68-
'when using name': {
69-
topic: function () {
70-
return tmp.dirSync({ name: 'using-name' });
71-
},
7262

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-
},
8163

82-
'when using multiple options': {
83-
topic: function () {
84-
return tmp.dirSync({ prefix: 'foo', postfix: 'bar', mode: 0750 });
85-
},
64+
function _prepareInbandTests(testOpts, opts) {
65+
var opts = opts || {};
66+
var testOpts = testOpts || {};
8667

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-
},
68+
return function () {
69+
var result = null;
9270

93-
'when using multiple options and mode': {
94-
topic: function () {
95-
return tmp.dirSync({ prefix: 'complicated', postfix: 'options', mode: 0755 });
96-
},
71+
before(function () {
72+
result = tmp.dirSync(opts);
73+
});
9774

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-
},
75+
it('should return a proper result', function () {
76+
assert.ok(result != undefined);
77+
assert.ok(result.name != undefined);
78+
assert.ok(result.fd == undefined);
79+
assert.ok(typeof result.removeCallback == 'function');
80+
});
10381

104-
'no tries': {
105-
topic: function () {
106-
try {
107-
return tmp.dirSync({ tries: -1 });
108-
}
109-
catch (e) {
110-
return e;
111-
}
112-
},
82+
it('temporary directory should exist', function () {
83+
var stat = fs.statSync(result.name);
84+
assert.ok(stat.isDirectory());
85+
});
86+
87+
it('temporary directory should have the expected stats and mode', function () {
88+
var stat = fs.statSync(result.name);
89+
assertStat(stat, testOpts.mode || opts.mode);
90+
});
11391

114-
'should return with an error': function (topic) {
115-
assert.instanceOf(topic, Error);
92+
if (opts.prefix || opts.testPrefix) {
93+
it('should have the expected prefix', function () {
94+
assertPrefix(result.name, testOpts.prefix || opts.prefix);
95+
});
11696
}
117-
},
97+
98+
if (opts.postfix || opts.testPostfix) {
99+
it('should have the expected postfix', function () {
100+
assertPostfix(result.name, testOpts.postfix || opts.postfix);
101+
});
102+
}
103+
104+
it('should have a valid name', function () {
105+
assertName(result.name, opts.name);
106+
});
107+
108+
it('should have a working removeCallback', function () {
109+
result.removeCallback();
110+
assert.ok(!existsSync(result.name));
111+
});
112+
};
113+
}
114+
115+
116+
describe('tmp', function () {
117+
describe('#dirSync()', function () {
118+
describe('without any parameters', _prepareInbandTests({mode: 040700, prefix: 'tmp-'}));
119+
describe('with prefix', _prepareInbandTests({mode: 040700}, {prefix: 'something'}));
120+
describe('with postfix', _prepareInbandTests({mode: 040700}, {postfix: '.txt'}));
121+
describe('with template', _prepareInbandTests({mode: 040700, prefix: 'clike-', postfix: '.txt'}, {template: 'clike-XXXXXX-postfix'}));
122+
describe('with name', _prepareInbandTests({mode: 040700}, {name: 'using-name'}));
123+
describe('with mode', _prepareInbandTests(null, {mode: 0755}));
124+
describe('with multiple options', _prepareInbandTests(null, {prefix: 'foo', postfix: 'bar', mode: 0750}));
125+
describe('with invalid tries', function () {
126+
it('should result in an error', function () {
127+
try {
128+
tmp.dirSync({tries: -1});
129+
assert.fail('should have failed');
130+
} catch (e) {
131+
assert.ok(e instanceof Error);
132+
}
133+
});
134+
});
135+
});
136+
});
137+
138+
139+
/*
140+
vows.describe('Synchronous directory creation').addBatch({
118141
119142
'keep testing': {
120143
topic: function () {
@@ -217,16 +240,5 @@ vows.describe('Synchronous directory creation').addBatch({
217240
fs.rmdirSync(name);
218241
}
219242
},
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-
}
232243
}).exportTo(module);
244+
*/

test/base.js renamed to test/legacy/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var
22
assert = require('assert'),
33
path = require('path'),
44
spawn = require('child_process').spawn,
5-
tmp = require('../lib/tmp');
5+
tmp = require('../../lib/tmp');
66

77
// make sure that we do not test spam the global tmp
88
tmp.TMP_DIR = './tmp';

0 commit comments

Comments
 (0)