Skip to content

Commit b00720b

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

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1317
-1261
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": "mocha test/*-test.js",
3637
"doc": "jsdoc -c .jsdoc.json"
3738
}
3839
}

test/assertions.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.strictEqual(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), name + ' should exist');
68+
var stat = fs.statSync(name);
69+
if (isfile) assert.ok(stat.isFile(), name + ' should be a file');
70+
else assert.ok(stat.isDirectory(), name + ' should be a directory');
71+
};
72+
73+
74+
module.exports.assertDoesNotExist = function assertDoesNotExist(name) {
75+
assert.ok(!existsSync(name), name + ' should not exist');
76+
};
77+

test/child-process.js

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

0 commit comments

Comments
 (0)