From 4f5aff255744f4986228965e3e2e2b78129b0c7e Mon Sep 17 00:00:00 2001 From: Pulkit Gupta Date: Mon, 6 Feb 2023 22:28:51 +0530 Subject: [PATCH] test: fix tap parser fails if a test logs a number PR-URL: https://github.com/nodejs/node/pull/46056 Backport-PR-URL: https://github.com/nodejs/node/pull/46839 Fixes: https://github.com/nodejs/node/issues/46048 Reviewed-By: Moshe Atlow --- lib/internal/test_runner/tap_parser.js | 19 +- .../parallel/test-runner-tap-parser-stream.js | 187 ++++++++++++++++++ test/parallel/test-runner-tap-parser.js | 133 ------------- 3 files changed, 199 insertions(+), 140 deletions(-) diff --git a/lib/internal/test_runner/tap_parser.js b/lib/internal/test_runner/tap_parser.js index c502c05eb8fc14..f4d73bb12b9082 100644 --- a/lib/internal/test_runner/tap_parser.js +++ b/lib/internal/test_runner/tap_parser.js @@ -270,8 +270,19 @@ class TapParser extends Transform { this.#yamlCurrentIndentationLevel = this.#subTestNestingLevel; } + let node; + // Parse current chunk - const node = this.#TAPDocument(chunk); + try { + node = this.#TAPDocument(chunk); + } catch { + node = { + kind: TokenKind.UNKNOWN, + node: { + value: this.#currentChunkAsString, + }, + }; + } // Emit the parsed node to both the stream and the AST this.#emitOrBufferCurrentNode(node); @@ -282,12 +293,6 @@ class TapParser extends Transform { } #error(message) { - if (!this.#isSyncParsingEnabled) { - // When async parsing is enabled, don't throw. - // Unrecognized tokens would be ignored. - return; - } - const token = this.#currentToken || { value: '', kind: '' }; // Escape NewLine characters if (token.value === '\n') { diff --git a/test/parallel/test-runner-tap-parser-stream.js b/test/parallel/test-runner-tap-parser-stream.js index bd10af29d88279..80be92c121b73d 100644 --- a/test/parallel/test-runner-tap-parser-stream.js +++ b/test/parallel/test-runner-tap-parser-stream.js @@ -17,6 +17,193 @@ const cases = [ }, ], }, + { + input: '123', + expected: [ + { + kind: 'Unknown', + node: { value: '123' }, + nesting: 0, + lexeme: '123', + }, + ], + }, + { + input: '# 123', + expected: [ + { + kind: 'Comment', + node: { comment: '123' }, + nesting: 0, + lexeme: '# 123', + }, + ], + }, + { + input: '1..', + expected: [ + { + kind: 'Unknown', + node: { value: '1..' }, + nesting: 0, + lexeme: '1..', + }, + ], + }, + { + input: '1..abc', + expected: [ + { + kind: 'Unknown', + node: { value: '1..abc' }, + nesting: 0, + lexeme: '1..abc', + }, + ], + }, + { + input: '1..-1', + expected: [ + { + kind: 'Unknown', + node: { value: '1..-1' }, + nesting: 0, + lexeme: '1..-1', + }, + ], + }, + { + input: '1.1', + expected: [ + { + kind: 'Unknown', + node: { value: '1.1' }, + nesting: 0, + lexeme: '1.1', + }, + ], + }, + { + input: '1.....4', + expected: [ + { + kind: 'Unknown', + node: { value: '1.....4' }, + nesting: 0, + lexeme: '1.....4', + }, + ], + }, + { + input: 'TAP 12', + expected: [ + { + kind: 'Unknown', + node: { value: 'TAP 12' }, + nesting: 0, + lexeme: 'TAP 12', + }, + ], + }, + { + input: 'TAP version', + expected: [ + { + kind: 'Unknown', + node: { value: 'TAP version' }, + nesting: 0, + lexeme: 'TAP version', + }, + ], + }, + { + input: 'TAP version v14', + expected: [ + { + kind: 'Unknown', + node: { value: 'TAP version v14' }, + nesting: 0, + lexeme: 'TAP version v14', + }, + ], + }, + { + input: 'TAP TAP TAP', + expected: [ + { + kind: 'Unknown', + node: { value: 'TAP TAP TAP' }, + nesting: 0, + lexeme: 'TAP TAP TAP', + }, + ], + }, + { + input: '--- yaml', + expected: [ + { + kind: 'Unknown', + node: { value: '--- yaml' }, + nesting: 0, + lexeme: '--- yaml', + }, + ], + }, + { + input: '... ... yaml', + expected: [ + { + kind: 'Unknown', + node: { value: '... ... yaml' }, + nesting: 0, + lexeme: '... ... yaml', + }, + ], + }, + { + input: 'ook 1', + expected: [ + { + kind: 'Unknown', + node: { value: 'ook 1' }, + nesting: 0, + lexeme: 'ook 1', + }, + ], + }, + { + input: ' ok 98', + expected: [ + { + kind: 'Unknown', + node: { value: ' ok 98' }, + nesting: 0, + lexeme: ' ok 98', + }, + ], + }, + { + input: 'pragma ++++++', + expected: [ + { + kind: 'Unknown', + node: { value: 'pragma ++++++' }, + nesting: 0, + lexeme: 'pragma ++++++', + }, + ], + }, + { + input: 'Bailout!', + expected: [ + { + kind: 'Unknown', + node: { value: 'Bailout!' }, + nesting: 0, + lexeme: 'Bailout!', + }, + ], + }, { input: 'invalid tap', expected: [ diff --git a/test/parallel/test-runner-tap-parser.js b/test/parallel/test-runner-tap-parser.js index 530e56626a4c5b..b14f7a9a6b089b 100644 --- a/test/parallel/test-runner-tap-parser.js +++ b/test/parallel/test-runner-tap-parser.js @@ -73,24 +73,6 @@ function TAPParser(input) { ]); } -{ - assert.throws(() => TAPParser('TAP version'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a version number, received "version" (VersionKeyword) at line 1, column 5 (start 4, end 10)', - }); -} - -{ - assert.throws(() => TAPParser('TAP'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected "version" keyword, received "TAP" (TAPKeyword) at line 1, column 1 (start 0, end 2)', - }); -} - // Test plan { @@ -123,42 +105,6 @@ function TAPParser(input) { ]); } -{ - assert.throws(() => TAPParser('1..'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a plan end count, received "" (EOL) at line 1, column 4 (start 3, end 3)', - }); -} - -{ - assert.throws(() => TAPParser('1..abc'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected ".." symbol, received "..abc" (Literal) at line 1, column 2 (start 1, end 5)', - }); -} - -{ - assert.throws(() => TAPParser('1..-1'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a plan end count, received "-" (Dash) at line 1, column 4 (start 3, end 3)', - }); -} - -{ - assert.throws(() => TAPParser('1.1'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected ".." symbol, received "." (Literal) at line 1, column 2 (start 1, end 1)', - }); -} - // Test point { @@ -914,24 +860,6 @@ ok 6 - nested1 ]); } -{ - assert.throws( - () => - TAPParser( - ` - message: 'description' - property: 'value' - ...` - ), - { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Unexpected YAML end marker, received "..." (YamlEndKeyword) at line 4, column 3 (start 48, end 50)', - } - ); -} - { assert.throws( () => @@ -950,26 +878,6 @@ ok 6 - nested1 ); } -{ - assert.throws( - () => - // Note the leading 3 spaces before --- - TAPParser( - ` - --- - message: 'description' - property: 'value' - ...` - ), - { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected valid YAML indentation (2 spaces), received " " (Whitespace) at line 2, column 3 (start 3, end 3)', - } - ); -} - { assert.throws( () => @@ -995,27 +903,6 @@ ok 6 - nested1 ); } -{ - assert.throws( - () => - // Note the leading 4 spaces before --- - TAPParser( - ` - --- - message: 'description' - property: 'value' - ... - ` - ), - { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a valid token, received "---" (YamlStartKeyword) at line 2, column 5 (start 5, end 7)', - } - ); -} - { assert.throws( () => @@ -1067,26 +954,6 @@ ok 6 - nested1 ]); } -// Non-recognized - -{ - assert.throws(() => TAPParser('abc'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a valid token, received "abc" (Literal) at line 1, column 1 (start 0, end 2)', - }); -} - -{ - assert.throws(() => TAPParser(' abc'), { - name: 'SyntaxError', - code: 'ERR_TAP_PARSER_ERROR', - message: - 'Expected a valid token, received "abc" (Literal) at line 1, column 5 (start 4, end 6)', - }); -} - // TAP document (with diagnostics) {