ensure exec_wrapper always captures exit code of called process

This commit is contained in:
Nick Fyson 2020-09-07 15:17:19 +01:00
parent dd4e841441
commit 9701a93bf1
3 changed files with 54 additions and 39 deletions

48
lib/exec_wrapper.js generated
View file

@ -23,55 +23,63 @@ async function exec_wrapper(commandLine, args, matchers, options) {
var _a;
let stdout = '';
let stderr = '';
// custom listeners to store stdout and stderr, while also replicating the behaviour of the passed listeners
const originalListener = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners;
let listeners = {
stdout: (data) => {
var _a;
var _a, _b;
stdout += data.toString();
if (((_a = originalListener) === null || _a === void 0 ? void 0 : _a.stdout) !== undefined) {
originalListener.stdout(data);
if (((_b = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners) === null || _b === void 0 ? void 0 : _b.stdout) !== undefined) {
options.listeners.stdout(data);
}
else {
// if no stdout listener was originally defined then match behaviour of exec.exec
// if no stdout listener was originally defined then we match default behavior of exec.exec
process.stdout.write(data);
}
},
stderr: (data) => {
var _a;
var _a, _b;
stderr += data.toString();
if (((_a = originalListener) === null || _a === void 0 ? void 0 : _a.stderr) !== undefined) {
originalListener.stderr(data);
if (((_b = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners) === null || _b === void 0 ? void 0 : _b.stderr) !== undefined) {
options.listeners.stderr(data);
}
else {
// if no stderr listener was originally defined then match behaviour of exec.exec
// if no stderr listener was originally defined then we match default behavior of exec.exec
process.stderr.write(data);
}
}
};
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnValue;
// we capture the original return code or error so that if no match is found we can duplicate the behavior
let returnState;
try {
originalReturnValue = await exec.exec(commandLine, args, {
returnState = await exec.exec(commandLine, args, {
...options,
listeners: listeners,
...options
ignoreReturnCode: true,
});
}
catch (e) {
originalReturnValue = e;
returnState = e;
}
if (matchers && originalReturnValue !== 0) {
// if there is a zero return code then we do not apply the matchers
if (returnState === 0)
return returnState;
if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnValue || regex.test(stderr) || regex.test(stdout)) {
if (customCode === returnState || regex.test(stderr) || regex.test(stdout)) {
throw new Error(message);
}
}
}
if (typeof originalReturnValue === 'number') {
return originalReturnValue;
if (typeof returnState === 'number') {
// only if we were instructed to ignore the return code do we ever return it non-zero
if ((_a = options) === null || _a === void 0 ? void 0 : _a.ignoreReturnCode) {
return returnState;
}
else {
throw new Error(`The process \'${commandLine}\' failed with exit code ${returnState}`);
}
}
else {
throw originalReturnValue;
throw returnState;
}
}
exports.exec_wrapper = exec_wrapper;

View file

@ -1 +1 @@
{"version":3,"file":"exec_wrapper.js","sourceRoot":"","sources":["../src/exec_wrapper.ts"],"names":[],"mappings":";;;;;;;;;AAAA,oDAAsC;AAMtC;;;;;;;;;;GAUG;AACI,KAAK,UAAU,YAAY,CAAC,WAAmB,EAAE,IAAe,EACpC,QAAyB,EACzB,OAAwB;;IAEzD,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,4GAA4G;IAC5G,MAAM,gBAAgB,SAAG,OAAO,0CAAE,SAAS,CAAC;IAC5C,IAAI,SAAS,GAAG;QACd,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;;YACvB,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC1B,IAAI,OAAA,gBAAgB,0CAAE,MAAM,MAAK,SAAS,EAAE;gBAC1C,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;aAC/B;iBAAM;gBACL,iFAAiF;gBACjF,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;aAC5B;QAEH,CAAC;QACD,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;;YACvB,MAAM,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC1B,IAAI,OAAA,gBAAgB,0CAAE,MAAM,MAAK,SAAS,EAAE;gBAC1C,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;aAC/B;iBAAM;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E;QACnC,qFAAqF;QACrF,UAAI,OAAO,0CAAE,gBAAgB,EAAE;YAC7B,OAAO,WAAW,CAAC;SACpB;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,iBAAiB,WAAW,4BAA4B,WAAW,EAAE,CAAC,CAAC;SACxF;KACF;SAAM;QACL,MAAM,WAAW,CAAC;KACnB;AACH,CAAC;AAjED,oCAiEC"}

View file

@ -22,55 +22,62 @@ export async function exec_wrapper(commandLine: string, args?: string[],
let stdout = '';
let stderr = '';
// custom listeners to store stdout and stderr, while also replicating the behaviour of the passed listeners
const originalListener = options?.listeners;
let listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
if (originalListener?.stdout !== undefined) {
originalListener.stdout(data);
if (options?.listeners?.stdout !== undefined) {
options.listeners.stdout(data);
} else {
// if no stdout listener was originally defined then match behaviour of exec.exec
// if no stdout listener was originally defined then we match default behavior of exec.exec
process.stdout.write(data);
}
},
stderr: (data: Buffer) => {
stderr += data.toString();
if (originalListener?.stderr !== undefined) {
originalListener.stderr(data);
if (options?.listeners?.stderr !== undefined) {
options.listeners.stderr(data);
} else {
// if no stderr listener was originally defined then match behaviour of exec.exec
// if no stderr listener was originally defined then we match default behavior of exec.exec
process.stderr.write(data);
}
}
};
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnValue: Error|number;
// we capture the original return code or error so that if no match is found we can duplicate the behavior
let returnState: Error|number;
try {
originalReturnValue = await exec.exec(
returnState = await exec.exec(
commandLine,
args,
{
...options, // pass original options first in order to override below
listeners: listeners,
...options
ignoreReturnCode: true, // so we can check for specific codes using the matchers
});
} catch (e) {
originalReturnValue = e;
returnState = e;
}
if (matchers && originalReturnValue !== 0) {
// if there is a zero return code then we do not apply the matchers
if (returnState === 0) return returnState;
if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnValue || regex.test(stderr) || regex.test(stdout) ) {
if (customCode === returnState || regex.test(stderr) || regex.test(stdout) ) {
throw new Error(message);
}
}
}
if (typeof originalReturnValue === 'number') {
return originalReturnValue;
if (typeof returnState === 'number') {
// only if we were instructed to ignore the return code do we ever return it non-zero
if (options?.ignoreReturnCode) {
return returnState;
} else {
throw new Error(`The process \'${commandLine}\' failed with exit code ${returnState}`);
}
} else {
throw originalReturnValue;
throw returnState;
}
}