Skip to content

Commit 5a1da6b

Browse files
committed
fix #137
1 parent 8f73d89 commit 5a1da6b

29 files changed

+1117
-986
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/child-process.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// vim: expandtab:ts=2:sw=2
2+
3+
var
4+
path = require('path'),
5+
spawn = require('child_process').spawn;
6+
7+
8+
module.exports = function spawnChildProcess(passError, file, params, exitCallback) {
9+
var
10+
node_path = process.argv[0],
11+
command_args = [ path.join(__dirname, testFile) ].concat(params),
12+
stdoutBufs = [],
13+
stderrBufs = [],
14+
child,
15+
done = false,
16+
stderrDone = false,
17+
stdoutDone = false;
18+
19+
// spawn doesn’t have the quoting problems that exec does,
20+
// especially when going for Windows portability.
21+
child = spawn(node_path, command_args);
22+
child.stdin.end();
23+
// Cannot use 'close' event because not on node-0.6.
24+
function _close() {
25+
var
26+
stderr = _bufferConcat(stderrBufs),
27+
stdout = _bufferConcat(stdoutBufs);
28+
if (stderrDone && stdoutDone && !done) {
29+
done = true;
30+
if (passError) {
31+
if (stderr.length > 0) {
32+
return cb(stderr.toString());
33+
}
34+
}
35+
return cb(null, _bufferConcat(stdoutBufs).toString());
36+
}
37+
}
38+
if (passError) {
39+
child.on('error', function _spawnError(err) {
40+
if (!done) {
41+
done = true;
42+
cb(err);
43+
}
44+
});
45+
}
46+
child.stdout.on('data', function _stdoutData(data) {
47+
stdoutBufs.push(data);
48+
}).on('close', function _stdoutEnd() {
49+
stdoutDone = true;
50+
_close();
51+
});
52+
child.stderr.on('data', function _stderrData(data) {
53+
stderrBufs.push(data);
54+
}).on('close', function _stderrEnd() {
55+
stderrDone = true;
56+
_close();
57+
});
58+
}
59+
60+
61+
function _bufferConcat(buffers) {
62+
if (Buffer.concat) {
63+
return Buffer.concat.apply(this, arguments);
64+
} else {
65+
return new Buffer(buffers.reduce(function (acc, buf) {
66+
for (var i = 0; i < buf.length; i++) {
67+
acc.push(buf[i]);
68+
}
69+
return acc;
70+
}, []));
71+
}
72+
}
73+

0 commit comments

Comments
 (0)