UNPKG

4.2 kBJavaScriptView Raw
1var
2 assert = require('assert'),
3 path = require('path'),
4 exec = require('child_process').exec,
5 tmp = require('../lib/tmp');
6
7// make sure that we do not test spam the global tmp
8tmp.TMP_DIR = './tmp';
9
10
11function _spawnTestWithError(testFile, params, cb) {
12 _spawnTest(true, testFile, params, cb);
13}
14
15function _spawnTestWithoutError(testFile, params, cb) {
16 _spawnTest(false, testFile, params, cb);
17}
18
19function _spawnTest(passError, testFile, params, cb) {
20 var
21 node_path = process.argv[0],
22 command = [ node_path, path.join(__dirname, testFile) ].concat(params).join(' ');
23
24 exec(command, function _execDone(err, stdout, stderr) {
25 if (passError) {
26 if (err) {
27 return cb(err);
28 } else if (stderr.length > 0) {
29 return cb(stderr.toString());
30 }
31 }
32
33 return cb(null, stdout.toString());
34 });
35}
36
37function _testStat(stat, mode) {
38 assert.equal(stat.uid, process.getuid(), 'should have the same UID');
39 assert.equal(stat.gid, process.getgid(), 'should have the same GUID');
40 assert.equal(stat.mode, mode);
41}
42
43function _testPrefix(prefix) {
44 return function _testPrefixGenerated(err, name) {
45 assert.equal(path.basename(name).slice(0, prefix.length), prefix, 'should have the provided prefix');
46 };
47}
48
49function _testPrefixSync(prefix) {
50 return function _testPrefixGeneratedSync(result) {
51 if (result instanceof Error) {
52 throw result;
53 }
54 _testPrefix(prefix)(null, result.name, result.fd);
55 };
56}
57
58function _testPostfix(postfix) {
59 return function _testPostfixGenerated(err, name) {
60 assert.equal(name.slice(name.length - postfix.length, name.length), postfix, 'should have the provided postfix');
61 };
62}
63
64function _testPostfixSync(postfix) {
65 return function _testPostfixGeneratedSync(result) {
66 if (result instanceof Error) {
67 throw result;
68 }
69 _testPostfix(postfix)(null, result.name, result.fd);
70 };
71}
72
73function _testKeep(type, keep, cb) {
74 _spawnTestWithError('keep.js', [ type, keep ], cb);
75}
76
77function _testKeepSync(type, keep, cb) {
78 _spawnTestWithError('keep-sync.js', [ type, keep ], cb);
79}
80
81function _testGraceful(type, graceful, cb) {
82 _spawnTestWithoutError('graceful.js', [ type, graceful ], cb);
83}
84
85function _testGracefulSync(type, graceful, cb) {
86 _spawnTestWithoutError('graceful-sync.js', [ type, graceful ], cb);
87}
88
89function _assertName(err, name) {
90 assert.isString(name);
91 assert.isNotZero(name.length, 'an empty string is not a valid name');
92}
93
94function _assertNameSync(result) {
95 if (result instanceof Error) {
96 throw result;
97 }
98 var name = typeof(result) == 'string' ? result : result.name;
99 _assertName(null, name);
100}
101
102function _testName(expected){
103 return function _testNameGenerated(err, name) {
104 assert.equal(expected, name, 'should have the provided name');
105 };
106}
107
108function _testNameSync(expected){
109 return function _testNameGeneratedSync(result) {
110 if (result instanceof Error) {
111 throw result;
112 }
113 _testName(expected)(null, result.name, result.fd);
114 };
115}
116
117function _testUnsafeCleanup(unsafe, cb) {
118 _spawnTestWithoutError('unsafe.js', [ 'dir', unsafe ], cb);
119}
120
121function _testIssue62(cb) {
122 _spawnTestWithoutError('issue62.js', [], cb);
123}
124
125function _testUnsafeCleanupSync(unsafe, cb) {
126 _spawnTestWithoutError('unsafe-sync.js', [ 'dir', unsafe ], cb);
127}
128
129function _testIssue62Sync(cb) {
130 _spawnTestWithoutError('issue62-sync.js', [], cb);
131}
132
133module.exports.testStat = _testStat;
134module.exports.testPrefix = _testPrefix;
135module.exports.testPrefixSync = _testPrefixSync;
136module.exports.testPostfix = _testPostfix;
137module.exports.testPostfixSync = _testPostfixSync;
138module.exports.testKeep = _testKeep;
139module.exports.testKeepSync = _testKeepSync;
140module.exports.testGraceful = _testGraceful;
141module.exports.testGracefulSync = _testGracefulSync;
142module.exports.assertName = _assertName;
143module.exports.assertNameSync = _assertNameSync;
144module.exports.testName = _testName;
145module.exports.testNameSync = _testNameSync;
146module.exports.testUnsafeCleanup = _testUnsafeCleanup;
147module.exports.testIssue62 = _testIssue62;
148module.exports.testUnsafeCleanupSync = _testUnsafeCleanupSync;
149module.exports.testIssue62Sync = _testIssue62Sync;