2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
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) {
|
2011-08-10 20:22:58 +02:00
|
|
|
var data = '';
|
|
|
|
|
|
|
|
var child = common.spawnPwd(options);
|
2010-12-05 20:15:30 +01:00
|
|
|
|
2010-08-05 04:15:20 +02:00
|
|
|
child.stdout.setEncoding('utf8');
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
child.stdout.on('data', function(chunk) {
|
2010-08-05 04:15:20 +02:00
|
|
|
data += chunk;
|
|
|
|
});
|
2010-12-05 20:15:30 +01:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
child.on('exit', function(code, signal) {
|
2010-08-05 04:15:20 +02:00
|
|
|
assert.strictEqual(forCode, code);
|
2012-03-14 00:04:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
child.on('close', function () {
|
|
|
|
forData && assert.strictEqual(forData, data.replace(/[\s\r\n]+$/, ''));
|
2010-08-05 04:15:20 +02:00
|
|
|
returns--;
|
|
|
|
});
|
2010-12-05 20:15:30 +01:00
|
|
|
|
2010-08-05 04:15:20 +02:00
|
|
|
returns++;
|
2012-08-21 13:29:53 +02:00
|
|
|
|
|
|
|
return child;
|
2010-08-05 04:15:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assume these exist, and 'pwd' gives us the right directory back
|
2011-10-05 00:08:18 +02:00
|
|
|
if (process.platform == 'win32') {
|
2011-08-02 03:19:57 +02:00
|
|
|
testCwd({cwd: process.env.windir}, 0, process.env.windir);
|
|
|
|
testCwd({cwd: 'c:\\'}, 0, 'c:\\');
|
|
|
|
} else {
|
|
|
|
testCwd({cwd: '/dev'}, 0, '/dev');
|
|
|
|
testCwd({cwd: '/'}, 0, '/');
|
|
|
|
}
|
2010-08-05 04:15:20 +02:00
|
|
|
|
2012-08-21 13:29:53 +02:00
|
|
|
// Assume does-not-exist doesn't exist, expect exitCode=-1 and errno=ENOENT
|
|
|
|
(function() {
|
|
|
|
var errors = 0;
|
|
|
|
|
|
|
|
testCwd({cwd: 'does-not-exist'}, -1).on('error', function(e) {
|
|
|
|
assert.equal(e.code, 'ENOENT');
|
|
|
|
errors++;
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
assert.equal(errors, 1);
|
|
|
|
});
|
|
|
|
})();
|
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);
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-08-05 04:15:20 +02:00
|
|
|
assert.equal(0, returns);
|
2010-11-30 20:53:44 +01:00
|
|
|
});
|