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

View file

@ -1 +1 @@
{"version":3,"file":"exec_wrapper.js","sourceRoot":"","sources":["../src/exec_wrapper.ts"],"names":[],"mappings":";;;;;;;;;AAAA,oDAAsC;AAItC;;;;;;;;;;GAUG;AACI,KAAK,UAAU,YAAY,CAAC,WAAmB,EAAE,IAAe,EACpC,QAAqC,EACrC,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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MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,OAAO,CAAC,IAAI,QAAQ,EAAE;YACnD,IAAI,UAAU,KAAK,mBAAmB,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAG;gBACnF,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;aAC1B;SACF;KACF;IAED,IAAI,OAAO,mBAAmB,KAAK,QAAQ,EAAE;QAC3C,OAAO,mBAAmB,CAAC;KAC5B;SAAM;QACL,MAAM,mBAAmB,CAAC;KAC3B;AACH,CAAC;AA1DD,oCA0DC"}

View file

@ -45,10 +45,9 @@ export async function exec_wrapper(commandLine: string, args?: string[],
};
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnCode: number;
let originalError: Error|undefined;
let originalReturnValue: Error|number;
try {
originalReturnCode = await exec.exec(
originalReturnValue = await exec.exec(
commandLine,
args,
{
@ -56,18 +55,20 @@ export async function exec_wrapper(commandLine: string, args?: string[],
...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;
}
}