mirror of
https://github.com/shelljs/shelljs.git
synced 2025-12-08 20:35:51 +00:00
138 lines
4.1 KiB
JavaScript
138 lines
4.1 KiB
JavaScript
var shell = require('..');
|
|
|
|
var assert = require('assert'),
|
|
path = require('path'),
|
|
fs = require('fs'),
|
|
child = require('child_process');
|
|
|
|
shell.silent(true);
|
|
|
|
function numLines(str) {
|
|
return typeof str === 'string' ? str.match(/\n/g).length : 0;
|
|
}
|
|
|
|
//
|
|
// Invalids
|
|
//
|
|
|
|
shell.exec();
|
|
assert.ok(shell.error());
|
|
|
|
var result = shell.exec('asdfasdf'); // could not find command
|
|
assert.ok(result.code > 0);
|
|
|
|
|
|
//
|
|
// Valids
|
|
//
|
|
|
|
//
|
|
// sync
|
|
//
|
|
|
|
// check if stdout goes to output
|
|
var result = shell.exec('node -e \"console.log(1234);\"');
|
|
assert.equal(shell.error(), null);
|
|
assert.equal(result.code, 0);
|
|
assert.ok(result.output === '1234\n' || result.output === '1234\nundefined\n'); // 'undefined' for v0.4
|
|
|
|
// check if stderr goes to output
|
|
var result = shell.exec('node -e \"console.error(1234);\"');
|
|
assert.equal(shell.error(), null);
|
|
assert.equal(result.code, 0);
|
|
assert.ok(result.output === '1234\n' || result.output === '1234\nundefined\n'); // 'undefined' for v0.4
|
|
|
|
// check if stdout + stderr go to output
|
|
var result = shell.exec('node -e \"console.error(1234); console.log(666);\"');
|
|
assert.equal(shell.error(), null);
|
|
assert.equal(result.code, 0);
|
|
assert.ok(result.output === '1234\n666\n' || result.output === '1234\n666\nundefined\n'); // 'undefined' for v0.4
|
|
|
|
// check exit code
|
|
var result = shell.exec('node -e \"process.exit(12);\"');
|
|
assert.equal(shell.error(), null);
|
|
assert.equal(result.code, 12);
|
|
|
|
// interaction with cd
|
|
shell.cd('resources/external');
|
|
var result = shell.exec('node node_script.js');
|
|
assert.equal(shell.error(), null);
|
|
assert.equal(result.code, 0);
|
|
assert.equal(result.output, 'node_script_1234\n');
|
|
shell.cd('../..');
|
|
|
|
//
|
|
// async
|
|
//
|
|
|
|
var asyncFlags = [];
|
|
|
|
//
|
|
// callback as 2nd argument
|
|
//
|
|
asyncFlags[0] = false;
|
|
shell.exec('node -e \"console.log(5678);\"', {async:true}, function(code, output) {
|
|
assert.equal(code, 0);
|
|
assert.ok(output === '5678\n' || output === '5678\nundefined\n'); // 'undefined' for v0.4
|
|
asyncFlags[0] = true;
|
|
|
|
|
|
// Most of the following code doesn't really belong here since it tests the sync version.
|
|
// However there seems to be a race condition with the stdout returned by child.exec()
|
|
// that makes the tests fail intermittently. So we're keeping them here in a chain
|
|
// to avoid this race issue
|
|
|
|
|
|
//
|
|
// check if stdout is proxied with default silent options (i.e. silent = false)
|
|
//
|
|
asyncFlags[1] = false;
|
|
shell.mkdir('-p', 'tmp');
|
|
var file = 'tmp/tempscript'+Math.random()+'.js',
|
|
script = 'require(\'../../global.js\'); exec(\'node -e \"console.log(555);\"\')';
|
|
script.to(file);
|
|
child.exec('node '+file, function(err, stdout, stderr) {
|
|
assert.ok(stdout === '555\n' || stdout === '555\nundefined\n'); // 'undefined' for v0.4
|
|
asyncFlags[1] = true;
|
|
|
|
//
|
|
// check if stdout is proxied when: silent(true), {silent:false}
|
|
//
|
|
asyncFlags[2] = false;
|
|
shell.mkdir('-p', 'tmp');
|
|
var file = 'tmp/tempscript'+Math.random()+'.js',
|
|
script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(333);\"\', {silent:false})';
|
|
script.to(file);
|
|
child.exec('node '+file, function(err, stdout, stderr) {
|
|
assert.ok(stdout === '333\n' || stdout === '333\nundefined\n'); // 'undefined' for v0.4
|
|
asyncFlags[2] = true;
|
|
|
|
//
|
|
// check if stdout is proxied when: silent(true), {silent:false} - async
|
|
//
|
|
asyncFlags[3] = false;
|
|
shell.mkdir('-p', 'tmp');
|
|
var file = 'tmp/tempscript'+Math.random()+'.js',
|
|
script = 'require(\'../../global.js\'); silent(true); exec(\'node -e \"console.log(222);\"\', {silent:false, async:true})';
|
|
script.to(file);
|
|
child.exec('node '+file, function(err, stdout, stderr) {
|
|
assert.ok(stdout === '222\n' || stdout === '222\nundefined\n'); // 'undefined' for v0.4
|
|
asyncFlags[3] = true;
|
|
});
|
|
});
|
|
});
|
|
});
|
|
assert.equal(shell.error(), null);
|
|
|
|
// no callback (no need for asyncFlags)
|
|
shell.exec('node -e \"console.log(1234)\"', {async:true});
|
|
assert.equal(shell.error(), null);
|
|
|
|
setTimeout(function() {
|
|
asyncFlags.forEach(function(flag) {
|
|
assert.equal(flag, true);
|
|
});
|
|
|
|
shell.exit(123);
|
|
}, 2000);
|