js-waku/src/test_utils/log_file.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-03-25 15:49:07 +11:00
import { Context } from 'mocha';
import pTimeout from 'p-timeout';
import { Tail } from 'tail';
import { waitForFile } from './async_fs';
export default async function waitForLine(filepath: string, logLine: string) {
await pTimeout(waitForFile(filepath), 2000);
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) {
return new Promise((resolve, reject) => {
tail.on('line', (data: string) => {
if (data.includes(line)) {
resolve(data);
}
});
tail.on('error', (err) => {
reject(err);
});
});
}
2021-03-25 15:49:07 +11:00
function clean(str: string): string {
return str.replace(/ /g, '_').replace(/[':()]/g, '');
}
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) {
name = clean(suite.title) + '_' + name;
suite = suite.parent;
}
return name;
}