js-waku/packages/tests/src/log_file.ts

64 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-10 15:53:05 +10:00
/**
2022-04-01 12:19:51 +11:00
* Utilities to make it help check nwaku logs.
2021-05-10 15:53:05 +10:00
*
* @hidden
* @module
*/
2022-02-04 14:12:00 +11:00
import { Context } from "mocha";
import pTimeout from "p-timeout";
import { Tail } from "tail";
import { waitForFile } from "./async_fs.js";
export default async function waitForLine(
filepath: string,
logLine: string,
timeout: number
): Promise<void> {
await pTimeout(waitForFile(filepath), { milliseconds: timeout });
const options = {
fromBeginning: true,
follow: true,
};
const tail = new Tail(filepath, options);
await pTimeout(find(tail, logLine), {
milliseconds: 60000,
message: `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;
}