Skip to content

Commit

Permalink
fix(interop): use more descriptive log names (#756)
Browse files Browse the repository at this point in the history
This makes them easier to work with in the CI artifacts.
  • Loading branch information
Thomaash authored Mar 12, 2022
1 parent 7c54248 commit 5ad9148
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/test-e2e-interop/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync, spawn } from "child_process";
import { join } from "path";
import { createWriteStream, readFile } from "fs-extra";
import { basename, join } from "path";
import { createWriteStream, readFile, rename } from "fs-extra";

export class ProjectState {
/**
Expand Down Expand Up @@ -111,7 +111,15 @@ export function createSpawner(logDir: string, getState: () => string[]): Spawn {
logInfo("Start", getLogMessage());

return new Promise((resolve, reject): void => {
const commandLogPath = join(logDir, ("" + id).padStart(3, "0") + ".log");
const commandLogPath = join(
logDir,
[
basename(cwd).replace(/[^a-zA-Z0-9]+/g, "-"),
cmd.join("-").replace(/[^a-zA-Z0-9]+/g, "-"),
"id-" + ("" + id).padStart(3, "0"),
"log",
].join(".")
);
const logStream = createWriteStream(commandLogPath, {
flags: "a",
});
Expand All @@ -127,6 +135,20 @@ export function createSpawner(logDir: string, getState: () => string[]): Spawn {
child.stdout.pipe(logStream);
child.stderr.pipe(logStream);

/**
* Add status to the file name just before the .log extension on a best
* effort basis (no errors reported)..
*
* @param code - The code or other kind of status to append.
*/
function addStatusCodeToFilename(code: string | number): void {
const newPath =
commandLogPath.slice(0, -4) + ".status-" + code + ".log";
rename(commandLogPath, newPath).catch((error): void => {
console.error(`Failed to rename log file to ${newPath}.`, error);
});
}

let failed = false;
child.on("close", (code): void => {
if (failed) {
Expand All @@ -150,9 +172,11 @@ export function createSpawner(logDir: string, getState: () => string[]): Spawn {
})();
});

addStatusCodeToFilename(code ?? "null");
reject(new Error(errorMessage));
} else {
logInfo("Okay", getLogMessage());
addStatusCodeToFilename(code);
resolve();
}
});
Expand All @@ -163,6 +187,7 @@ export function createSpawner(logDir: string, getState: () => string[]): Spawn {

execFail(cwd, failCommand, error);

addStatusCodeToFilename("error");
reject(error);
});
});
Expand Down

0 comments on commit 5ad9148

Please sign in to comment.