From ee0c63eba327ee5d4e698e66c7a06ef8736d8206 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 25 Mar 2021 15:49:07 +1100 Subject: [PATCH] Improve helper function for log name --- src/lib/waku.spec.ts | 3 ++- src/lib/waku_relay.spec.ts | 3 ++- src/test_utils/log_file.spec.ts | 21 +++++++++++++++++++++ src/test_utils/log_file.ts | 17 +++++++++++++++++ src/test_utils/nim_waku.ts | 7 ++----- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/test_utils/log_file.spec.ts diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index b476493bc1..89c9421a10 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { NOISE_KEY_1 } from '../test_utils/constants'; +import { makeLogFileName } from '../test_utils/log_file'; import { NimWaku } from '../test_utils/nim_waku'; import Waku from './waku'; @@ -19,7 +20,7 @@ describe('Waku', function () { ); const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(this.test!.title); + const nimWaku = new NimWaku(makeLogFileName(this)); await nimWaku.start({ staticnode: multiAddrWithId }); const nimPeers = await nimWaku.peers(); diff --git a/src/lib/waku_relay.spec.ts b/src/lib/waku_relay.spec.ts index 549b239b61..4a9c569fa7 100644 --- a/src/lib/waku_relay.spec.ts +++ b/src/lib/waku_relay.spec.ts @@ -2,6 +2,7 @@ import { expect } from 'chai'; import Pubsub from 'libp2p-interfaces/src/pubsub'; import { NOISE_KEY_1, NOISE_KEY_2 } from '../test_utils/constants'; +import { makeLogFileName } from '../test_utils/log_file'; import { NimWaku } from '../test_utils/nim_waku'; import Waku from './waku'; @@ -77,7 +78,7 @@ describe('Waku Relay', () => { ); const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - nimWaku = new NimWaku(this.test!.ctx!.currentTest!.title); + nimWaku = new NimWaku(makeLogFileName(this)); await nimWaku.start({ staticnode: multiAddrWithId }); await waku.relay.subscribe(); diff --git a/src/test_utils/log_file.spec.ts b/src/test_utils/log_file.spec.ts new file mode 100644 index 0000000000..8a89b984dd --- /dev/null +++ b/src/test_utils/log_file.spec.ts @@ -0,0 +1,21 @@ +import { expect } from 'chai'; + +import { makeLogFileName } from './log_file'; + +describe('This', function () { + describe('Is', function () { + it('A test', function () { + expect(makeLogFileName(this)).to.equal('This_Is_A_test'); + }); + }); + + describe('Is also', function () { + let testName: string; + beforeEach(function () { + testName = makeLogFileName(this); + }); + it('A test', function () { + expect(testName).to.equal('This_Is_also_A_test'); + }); + }); +}); diff --git a/src/test_utils/log_file.ts b/src/test_utils/log_file.ts index 7bed4f88a7..74dbae5111 100644 --- a/src/test_utils/log_file.ts +++ b/src/test_utils/log_file.ts @@ -1,3 +1,4 @@ +import { Context } from 'mocha'; import pTimeout from 'p-timeout'; import { Tail } from 'tail'; @@ -34,3 +35,19 @@ async function find(tail: Tail, line: string) { }); }); } + +function clean(str: string): string { + return str.replace(/ /g, '_').replace(/[':()]/g, ''); +} + +export function makeLogFileName(ctx: Context): string { + const unitTest = ctx!.currentTest ? ctx!.currentTest : ctx.test; + let name = clean(unitTest!.title); + let suite = unitTest!.parent; + + while (suite && suite.title) { + name = clean(suite.title) + '_' + name; + suite = suite.parent; + } + return name; +} diff --git a/src/test_utils/nim_waku.ts b/src/test_utils/nim_waku.ts index 805be37419..864e7b5162 100644 --- a/src/test_utils/nim_waku.ts +++ b/src/test_utils/nim_waku.ts @@ -37,12 +37,9 @@ export class NimWaku { private peerId?: PeerId; private logPath: string; - constructor(testName: string) { + constructor(logName: string) { this.portsShift = randomInt(0, 5000); - - const logFilePrefix = testName.replace(/ /g, '_').replace(/[':()]/g, ''); - - this.logPath = `${LOG_DIR}/${logFilePrefix}-nim-waku.log`; + this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`; } async start(args: Args) {