8000 refactor: explicit handling for execa errors by nfischer · Pull Request #1220 · shelljs/shelljs · GitHub
[go: up one dir, main page]

Skip to content

refactor: explicit handling for execa errors #1220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,20 @@ function _cmd(options, command, commandArgs, userOptions) {
stdout = '';
stderr = "'" + command + "': command not found";
code = COMMAND_NOT_FOUND_ERROR_CODE;
} else {
} else if (typeof result.stdout === 'string' &&
typeof result.stderr === 'string' &&
typeof result.code === 'number') {
// Normal exit: execa was able to execute `command` and get a return value.
stdout = result.stdout.toString();
stderr = result.stderr.toString();
code = result.code;
} else {
// Catch-all: execa tried to run `command` but it encountered some error
// (ex. maxBuffer, timeout).
stdout = result.stdout || '';
stderr = result.stderr ||
`'${command}' encountered an error during execution`;
code = result.code > 0 ? result.code : 1;
}

// Pass `continue: true` so we can specify a value for stdout.
Expand Down
14 changes: 10 additions & 4 deletions test/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,26 @@ test('set cwd', t => {
});

test('set maxBuffer (very small)', t => {
const result = shell.cmd('shx', 'echo', '1234567890'); // default maxBuffer is ok
let result = shell.cmd('shx', 'echo', '1234567890'); // default maxBuffer is ok
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '1234567890\n');
shell.cmd('shx', 'echo', '1234567890', { maxBuffer: 6 });
result = shell.cmd('shx', 'echo', '1234567890', { maxBuffer: 6 });
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stdout, '1234567890\n');
});

test('set timeout option', t => {
const result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '100'); // default timeout is ok
let result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '100'); // default timeout is ok
t.falsy(shell.error());
t.is(result.stdout, 'fast\nslow\n');
t.is(result.code, 0);
shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '2000', { timeout: 1000 }); // times out
result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '2000', { timeout: 1000 }); // times out
t.truthy(shell.error());
t.is(result.stdout, 'fast\n');
t.truthy(result.stderr);
t.is(result.code, 1);
});

test('check process.env works', t => {
Expand Down
7 changes: 5 additions & 2 deletions test/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,14 @@ test('set maxBuffer (very small)', t => {
});

test('set timeout option', t => {
const result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 100`); // default timeout is ok
let result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 100`); // default timeout is ok
t.falsy(shell.error());
t.is(result.code, 0);
shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 2000`, { timeout: 1000 }); // times out
t.is(result.stdout, 'fast\nslow\n');
result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 2000`, { timeout: 1000 }); // times out
t.truthy(shell.error());
t.is(result.code, 1);
t.is(result.stdout, 'fast\n');
});

test('check process.env works', t => {
Expand Down
1 change: 1 addition & 0 deletions test/resources/exec/slow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
console.log('fast');
setTimeout(function() {
console.log('slow');
}, parseInt(process.argv[2], 10));
0