0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/simple/test-child-process-cwd.js
Bert Belder 94914135df New api for child_process.spawn; ability to set cwd for spawn()ed process
Tests for child_process.spawn() use new API

Test for deprecated child_process.spawn() API
2010-08-06 13:37:30 -07:00

51 lines
1.4 KiB
JavaScript

common = require("../common");
assert = common.assert
spawn = require('child_process').spawn,
path = require('path');
var returns = 0;
/*
Spawns 'pwd' with given options, then test
- whether the exit code equals forCode,
- optionally whether the stdout result (after removing traling whitespace) matches forData
*/
function testCwd(options, forCode, forData) {
var data = "";
var child = spawn('pwd', [], options);
child.stdout.setEncoding('utf8');
child.stdout.addListener('data', function(chunk) {
data += chunk;
});
child.addListener('exit', function(code, signal) {
forData && assert.strictEqual(forData, data.replace(/[\s\r\n]+$/, ''))
assert.strictEqual(forCode, code);
returns--;
});
returns++;
}
// Assume these exist, and 'pwd' gives us the right directory back
testCwd( { cwd: '/bin' }, 0, '/bin' );
testCwd( { cwd: '/dev' }, 0, '/dev' );
testCwd( { cwd: '/' }, 0, '/' );
// Assume this doesn't exist, we expect exitcode=127
testCwd( { cwd: 'does-not-exist' }, 127 );
// Spawn() shouldn't try to chdir() so this should just work
testCwd( undefined, 0 );
testCwd( { }, 0 );
testCwd( { cwd: '' }, 0 );
testCwd( { cwd: undefined }, 0 );
testCwd( { cwd: null }, 0 );
// Check whether all tests actually returned
assert.notEqual(0, returns);
process.addListener('exit', function () {
assert.equal(0, returns);
});