refactor to handle exec.exec return values together

This commit is contained in:
Nick Fyson 2020-09-02 16:32:28 +01:00
parent 6ef533485e
commit 231537b08e
3 changed files with 20 additions and 18 deletions

19
lib/exec_wrapper.js generated
View file

@ -50,28 +50,29 @@ async function exec_wrapper(commandLine, args, matchers, options) {
}
};
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnCode;
let originalError;
let originalReturnValue;
try {
originalReturnCode = await exec.exec(commandLine, args, {
originalReturnValue = await exec.exec(commandLine, args, {
listeners: listeners,
...options
});
}
catch (e) {
originalError = e;
originalReturnCode = 1; // TODO linter insists, but presumably there's a better way to do _all_ this...
originalReturnValue = e;
}
if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnCode || regex.test(stderr) || regex.test(stdout)) {
if (customCode === originalReturnValue || regex.test(stderr) || regex.test(stdout)) {
throw new Error(message);
}
}
}
if (originalError)
throw originalError;
return originalReturnCode;
if (typeof originalReturnValue === 'number') {
return originalReturnValue;
}
else {
throw originalReturnValue;
}
}
exports.exec_wrapper = exec_wrapper;
//# sourceMappingURL=exec_wrapper.js.map