Skip to content

Commit 42cd5f7

Browse files
authored
Merge pull request #111 from binki/windows-tests
Windows tests
2 parents 88107f9 + 0352032 commit 42cd5f7

4 files changed

Lines changed: 88 additions & 18 deletions

File tree

test/base.js

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
var
22
assert = require('assert'),
33
path = require('path'),
4-
exec = require('child_process').exec,
4+
spawn = require('child_process').spawn,
55
tmp = require('../lib/tmp');
66

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

10+
function _bufferConcat(buffers) {
11+
if (Buffer.concat) {
12+
return Buffer.concat.apply(this, arguments);
13+
} else {
14+
return new Buffer(buffers.reduce(function (acc, buf) {
15+
for (var i = 0; i < buf.length; i++) {
16+
acc.push(buf[i]);
17+
}
18+
return acc;
19+
}, []));
20+
}
21+
}
1022

1123
function _spawnTestWithError(testFile, params, cb) {
1224
_spawnTest(true, testFile, params, cb);
@@ -19,25 +31,71 @@ function _spawnTestWithoutError(testFile, params, cb) {
1931
function _spawnTest(passError, testFile, params, cb) {
2032
var
2133
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());
34+
command_args = [ path.join(__dirname, testFile) ].concat(params),
35+
stdoutBufs = [],
36+
stderrBufs = [],
37+
child,
38+
done = false,
39+
stderrDone = false,
40+
stdoutDone = false;
41+
42+
// spawn doesn’t have the quoting problems that exec does,
43+
// especially when going for Windows portability.
44+
child = spawn(node_path, command_args);
45+
child.stdin.end();
46+
// Cannot use 'close' event because not on node-0.6.
47+
function _close() {
48+
var
49+
stderr = _bufferConcat(stderrBufs),
50+
stdout = _bufferConcat(stdoutBufs);
51+
if (stderrDone && stdoutDone && !done) {
52+
done = true;
53+
if (passError) {
54+
if (stderr.length > 0) {
55+
return cb(stderr.toString());
56+
}
3057
}
58+
return cb(null, _bufferConcat(stdoutBufs).toString());
3159
}
32-
33-
return cb(null, stdout.toString());
60+
}
61+
if (passError) {
62+
child.on('error', function _spawnError(err) {
63+
if (!done) {
64+
done = true;
65+
cb(err);
66+
}
67+
});
68+
}
69+
child.stdout.on('data', function _stdoutData(data) {
70+
stdoutBufs.push(data);
71+
}).on('close', function _stdoutEnd() {
72+
stdoutDone = true;
73+
_close();
74+
});
75+
child.stderr.on('data', function _stderrData(data) {
76+
stderrBufs.push(data);
77+
}).on('close', function _stderrEnd() {
78+
stderrDone = true;
79+
_close();
3480
});
3581
}
3682

3783
function _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);
84+
// getuid() and getgid() do not exist on Windows.
85+
if (process.getuid) {
86+
assert.equal(stat.uid, process.getuid(), 'should have the same UID');
87+
}
88+
if (process.getgid) {
89+
assert.equal(stat.gid, process.getgid(), 'should have the same GUID');
90+
}
91+
// mode values do not work properly on Windows. Ignore “group” and
92+
// “other” bits then. Ignore execute bit on that platform because it
93+
// doesn’t exist—even for directories.
94+
if (process.platform == 'win32') {
95+
assert.equal(stat.mode & 0666600, mode & 0666600);
96+
} else {
97+
assert.equal(stat.mode, mode);
98+
}
4199
}
42100

43101
function _testPrefix(prefix) {

test/file-sync-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ vows.describe('Synchronous file creation').addBatch({
7979

8080
'when using name': {
8181
topic: function () {
82-
return tmp.fileSync({ name: 'using-name.tmp' });
82+
return tmp.fileSync({ name: 'using-name-sync.tmp' });
8383
},
8484

8585
'should return with a name': Test.assertNameSync,
86-
'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name.tmp')),
86+
'should have the provided name': Test.testNameSync(path.join(tmp.tmpdir, 'using-name-sync.tmp')),
8787
'should be a file': function (result) {
8888
_testFile(0100600, true);
8989
fs.unlinkSync(result.name);

test/unsafe-sync.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ try {
1818
var symlinkTarget = join(result.name, 'symlinkme-target');
1919

2020
// symlink that should be removed but the contents should be preserved.
21-
fs.symlinkSync(symlinkSource, symlinkTarget, 'dir');
21+
// Skip on Windows because symlinks require elevated privileges (instead just
22+
// create the file so that tdir-sync-test.js can unlink it).
23+
if (process.platform == 'win32') {
24+
fs.writeFileSync(symlinkTarget);
25+
} else {
26+
fs.symlinkSync(symlinkSource, symlinkTarget, 'dir');
27+
}
2228

2329
spawn.out(result.name, spawn.exit);
2430
} catch (e) {

test/unsafe.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ spawn.tmpFunction({ unsafeCleanup: unsafe }, function (err, name) {
2121
var symlinkTarget = join(name, 'symlinkme-target');
2222

2323
// symlink that should be removed but the contents should be preserved.
24-
fs.symlinkSync(symlinkSource, symlinkTarget, 'dir');
24+
// Skip on Windows because symlinks require elevated privileges (instead just
25+
// create the file so that tdir-sync-test.js can unlink it).
26+
if (process.platform == 'win32') {
27+
fs.writeFileSync(symlinkTarget);
28+
} else {
29+
fs.symlinkSync(symlinkSource, symlinkTarget, 'dir');
30+
}
2531

2632
spawn.out(name, spawn.exit);
2733
} catch (e) {

0 commit comments

Comments
 (0)