From 73ee48e13fbbf08dae378f6b2c97c1b7dc72b322 Mon Sep 17 00:00:00 2001 From: Aldwin Vlasblom Date: Tue, 16 Jun 2020 16:24:00 +0200 Subject: [PATCH] Update parser to only acknowledge logs on known channels Fixes backwards compatibility for users who happened to have comments that look like log lines. Since these users do not supply any --log-function parameters, their code won't break when they update. --- lib/doctest.js | 23 +++++++++++++---------- test/index.js | 25 +++++++++++-------------- test/shared/logging.js | 9 +++++++++ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/doctest.js b/lib/doctest.js index a75981bf..a689207f 100644 --- a/lib/doctest.js +++ b/lib/doctest.js @@ -202,7 +202,8 @@ function normalizeTest($test) { function processLine( options, // :: { prefix :: String // , openingDelimiter :: Nullable String - // , closingDelimiter :: Nullable String } + // , closingDelimiter :: Nullable String + // , logFunctionNames :: Array String } accum, // :: { state :: State, tests :: Array Test } line, // :: String input, // :: Test -> Undefined @@ -237,15 +238,17 @@ function processLine( (last ($test[OUTPUT])).value += '\n' + value; appendToOutput ($test); - } else if ((match = /^\[([A-Za-z]+)\]:(.*)$/.exec (trimmedLine)) != null) { - accum.state = LOG; - value = stripLeading (1, ' ', match[2]); - $test = last (accum.tests); - $test[OUTPUT].push ({ - channel: match[1], - value: value - }); - output ($test); + } else if ((match = /^\[(.+?)\]:(.*)$/.exec (trimmedLine)) != null) { + if (options.logFunctionNames.includes (match[1])) { + accum.state = LOG; + value = stripLeading (1, ' ', match[2]); + $test = last (accum.tests); + $test[OUTPUT].push ({ + channel: match[1], + value: value + }); + output ($test); + } } else if (accum.state === INPUT || accum.state === LOG) { $test = last (accum.tests); if (!$test[OUTPUT].some (isStandardOutput)) { diff --git a/test/index.js b/test/index.js index 08516db0..17a948f2 100644 --- a/test/index.js +++ b/test/index.js @@ -234,19 +234,19 @@ testCommand ('bin/doctest --log-function stdout --log-function stderr test/share stderr: '' }); -testCommand ('bin/doctest --log-function stdout test/shared/logging.js', { +testCommand ('bin/doctest --log-function stdout --log-function stderr test/shared/logging.js', { status: 1, stdout: unlines ([ 'running doctests in test/shared/logging.js...', - '.........xx.xxxx..x..x..', - 'FAIL: expected [stdout]: 2 on line 33 (got 3)', - 'FAIL: expected 3 on line 34 (got no output)', - 'FAIL: expected 3 on line 40 (got [stdout]: 2)', + '...........xx.xxxx..x..x..', + 'FAIL: expected [stdout]: 2 on line 42 (got 3)', + 'FAIL: expected 3 on line 43 (got no output)', + 'FAIL: expected 3 on line 49 (got [stdout]: 2)', 'FAIL: expected no output on line - (got 3)', - 'FAIL: expected [stdout]: 2 on line 45 (got [stdout]: 1)', - 'FAIL: expected [stdout]: 1 on line 46 (got [stdout]: 2)', - 'FAIL: expected [stderr]: 2 on line 53 (got [stdout]: 2)', - 'FAIL: expected [stdout]: 1 on line 60 (got no output fast enough)' + 'FAIL: expected [stdout]: 2 on line 54 (got [stdout]: 1)', + 'FAIL: expected [stdout]: 1 on line 55 (got [stdout]: 2)', + 'FAIL: expected [stderr]: 2 on line 62 (got [stdout]: 2)', + 'FAIL: expected [stdout]: 1 on line 69 (got no output fast enough)' ]), stderr: '' }); @@ -293,11 +293,8 @@ if (esmSupported) { status: 1, stdout: unlines ([ 'running doctests in test/esm/async.mjs...', - 'xxxx', - 'FAIL: expected [stdout]: 1 on line 4 (got ! ReferenceError: stdout is not defined)', - 'FAIL: expected [stderr]: 3 on line 5 (got no output fast enough)', - 'FAIL: expected undefined on line 6 (got no output)', - 'FAIL: expected [stdout]: 2 on line 7 (got no output fast enough)' + 'x', + 'FAIL: expected undefined on line 6 (got ! ReferenceError: stdout is not defined)' ]), stderr: '' }); diff --git a/test/shared/logging.js b/test/shared/logging.js index 4370c505..d6b29577 100644 --- a/test/shared/logging.js +++ b/test/shared/logging.js @@ -26,6 +26,15 @@ function crash(){ // ! Error // [stdout]: 1 +// Output on non-existent channels is ignored +// +// > (stdout (1), 2) +// [spam]: "hi" +// [stdout]: 1 +// [spam]: "lalala" +// 2 +// [spam]: "whatever" + // Failure due to not enough output // // > (stdout (1), 3)