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