js-waku/src/test_utils/log_file.ts

65 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-05-10 15:53:05 +10:00
/**
* Utilities to make it help check nim-waku logs.
*
* @hidden
* @module
*/
2022-02-04 14:12:00 +11:00
import { Context } from "mocha";
import pTimeout from "p-timeout";
import { Tail } from "tail";
2022-02-04 14:12:00 +11:00
import { waitForFile } from "./async_fs";
export default async function waitForLine(
filepath: string,
logLine: string,
timeout: number
): Promise<void> {
await pTimeout(waitForFile(filepath), timeout);
const options = {
fromBeginning: true,
follow: true,
};
const tail = new Tail(filepath, options);
await pTimeout(
find(tail, logLine),
60000,
`could not to find '${logLine}' in file '${filepath}'`
);
tail.unwatch();
}
async function find(tail: Tail, line: string): Promise<string> {
return new Promise((resolve, reject) => {
2022-02-04 14:12:00 +11:00
tail.on("line", (data: string) => {
if (data.includes(line)) {
resolve(data);
}
});
2022-02-04 14:12:00 +11:00
tail.on("error", (err) => {
reject(err);
});
});
}
2021-03-25 15:49:07 +11:00
function clean(str: string): string {
2022-02-04 14:12:00 +11:00
return str.replace(/ /g, "_").replace(/[':()/]/g, "");
2021-03-25 15:49:07 +11:00
}
export function makeLogFileName(ctx: Context): string {
2021-04-13 15:22:29 +10:00
const unitTest = ctx?.currentTest ? ctx!.currentTest : ctx.test;
2021-03-25 15:49:07 +11:00
let name = clean(unitTest!.title);
2021-04-13 15:22:29 +10:00
let suite = unitTest?.parent;
2021-03-25 15:49:07 +11:00
while (suite && suite.title) {
2022-02-04 14:12:00 +11:00
name = clean(suite.title) + "_" + name;
2021-03-25 15:49:07 +11:00
suite = suite.parent;
}
return name;
}