diff --git a/src/test_utils/async_fs.ts b/src/test_utils/async_fs.ts new file mode 100644 index 0000000000..40f4d3549c --- /dev/null +++ b/src/test_utils/async_fs.ts @@ -0,0 +1,22 @@ +import fs, { promises as asyncFs } from 'fs'; +import { promisify } from 'util'; + +import { delay } from './delay'; +export const existsAsync = (filepath: string) => + asyncFs.access(filepath, fs.constants.F_OK); + +export const openAsync = promisify(fs.open); + +export const mkdirAsync = asyncFs.mkdir; + +export async function waitForFile(path: string) { + let found = false; + do { + try { + await existsAsync(path); + found = true; + } catch (e) { + await delay(500); + } + } while (!found); +} diff --git a/src/test_utils/log_file.ts b/src/test_utils/log_file.ts index 048d5309f3..7bed4f88a7 100644 --- a/src/test_utils/log_file.ts +++ b/src/test_utils/log_file.ts @@ -1,24 +1,7 @@ -import fs, { promises as asyncFs } from 'fs'; - import pTimeout from 'p-timeout'; import { Tail } from 'tail'; -import { delay } from './delay'; - -const existsAsync = (filepath: string) => - asyncFs.access(filepath, fs.constants.F_OK); - -async function waitForFile(path: string) { - let found = false; - do { - try { - await existsAsync(path); - found = true; - } catch (e) { - await delay(500); - } - } while (!found); -} +import { waitForFile } from './async_fs'; export default async function waitForLine(filepath: string, logLine: string) { await pTimeout(waitForFile(filepath), 2000); diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 0e43c38c31..1b1d21f7a6 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -1,7 +1,5 @@ import { ChildProcess, spawn } from 'child_process'; import { randomInt } from 'crypto'; -import * as fs from 'fs'; -import { promisify } from 'util'; import axios from 'axios'; import Multiaddr from 'multiaddr'; @@ -11,14 +9,15 @@ import PeerId from 'peer-id'; import { Message } from '../lib/waku_message'; import { TOPIC } from '../lib/waku_relay'; +import { existsAsync, mkdirAsync, openAsync } from './async_fs'; import waitForLine from './log_file'; -const openAsync = promisify(fs.open); - const NIM_WAKU_DEFAULT_P2P_PORT = 60000; const NIM_WAKU_DEFAULT_RPC_PORT = 8545; const NIM_WAKU_BIN = '/home/froyer/src/status-im/nim-waku/build/wakunode2'; +const LOG_DIR = './log'; + export interface Args { staticnode?: string; nat?: 'none'; @@ -41,10 +40,21 @@ export class NimWaku { const logFilePrefix = testName.replace(/ /g, '_').replace(/[':()]/g, ''); - this.logPath = `./${logFilePrefix}-nim-waku.log`; + this.logPath = `${LOG_DIR}/${logFilePrefix}-nim-waku.log`; } async start(args: Args) { + try { + await existsAsync(LOG_DIR); + } catch (e) { + try { + await mkdirAsync(LOG_DIR); + } catch (e) { + // Looks like 2 tests tried to create the director at the same time, + // it can be ignored + } + } + const logFile = await openAsync(this.logPath, 'w'); const mergedArgs = defaultArgs();