|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + codes: { |
| 4 | + ERR_TEST_FAILURE, |
| 5 | + } |
| 6 | +} = require('internal/errors'); |
| 7 | +const AssertionError = require('internal/assert/assertion_error'); |
| 8 | +const { |
| 9 | + ArrayPrototypeJoin, |
| 10 | + ArrayPrototypePush, |
| 11 | + Error, |
| 12 | + Number, |
| 13 | + NumberIsNaN, |
| 14 | + RegExpPrototypeExec, |
| 15 | + StringPrototypeEndsWith, |
| 16 | + StringPrototypeRepeat, |
| 17 | + StringPrototypeSlice, |
| 18 | + StringPrototypeStartsWith, |
| 19 | + StringPrototypeSubstring, |
| 20 | +} = primordials; |
| 21 | + |
| 22 | +const kYamlKeyRegex = /^(\s+)?(\w+):(\s)+([>|][-+])?(.*)$/; |
| 23 | +const kStackDelimiter = ' at '; |
| 24 | + |
| 25 | +function reConstructError(parsedYaml) { |
| 26 | + if (!('error' in parsedYaml)) { |
| 27 | + return parsedYaml; |
| 28 | + } |
| 29 | + const isAssertionError = parsedYaml.code === 'ERR_ASSERTION' || |
| 30 | + 'actual' in parsedYaml || 'expected' in parsedYaml || 'operator' in parsedYaml; |
| 31 | + const isTestFailure = parsedYaml.code === 'ERR_TEST_FAILURE' || 'failureType' in parsedYaml; |
| 32 | + const stack = parsedYaml.stack ? kStackDelimiter + ArrayPrototypeJoin(parsedYaml.stack, `\n${kStackDelimiter}`) : ''; |
| 33 | + let error, cause; |
| 34 | + |
| 35 | + if (isAssertionError) { |
| 36 | + cause = new AssertionError({ |
| 37 | + message: parsedYaml.error, |
| 38 | + actual: parsedYaml.actual, |
| 39 | + expected: parsedYaml.expected, |
| 40 | + operator: parsedYaml.operator |
| 41 | + }); |
| 42 | + } else { |
| 43 | + // eslint-disable-next-line no-restricted-syntax |
| 44 | + cause = new Error(parsedYaml.error); |
| 45 | + cause.code = parsedYaml.code; |
| 46 | + } |
| 47 | + cause.stack = stack; |
| 48 | + |
| 49 | + if (isTestFailure) { |
| 50 | + error = new ERR_TEST_FAILURE(cause, parsedYaml.failureType); |
| 51 | + error.stack = stack; |
| 52 | + } |
| 53 | + |
| 54 | + parsedYaml.error = error ?? cause; |
| 55 | + delete parsedYaml.stack; |
| 56 | + delete parsedYaml.code; |
| 57 | + delete parsedYaml.failureType; |
| 58 | + delete parsedYaml.actual; |
| 59 | + delete parsedYaml.expected; |
| 60 | + delete parsedYaml.operator; |
| 61 | + |
| 62 | + return parsedYaml; |
| 63 | +} |
| 64 | + |
| 65 | +function getYamlValue(value) { |
| 66 | + if (StringPrototypeStartsWith(value, "'") && StringPrototypeEndsWith(value, "'")) { |
| 67 | + return StringPrototypeSlice(value, 1, -1); |
| 68 | + } |
| 69 | + if (value === 'true') { |
| 70 | + return true; |
| 71 | + } |
| 72 | + if (value === 'false') { |
| 73 | + return false; |
| 74 | + } |
| 75 | + if (value !== '') { |
| 76 | + const valueAsNumber = Number(value); |
| 77 | + return NumberIsNaN(valueAsNumber) ? value : valueAsNumber; |
| 78 | + } |
| 79 | + return value; |
| 80 | +} |
| 81 | + |
| 82 | +// This parses the YAML generated by the built-in TAP reporter, |
| 83 | +// which is a subset of the full YAML spec. There are some |
| 84 | +// YAML features that won't be parsed here. This function should not be exposed publicly. |
| 85 | +function YAMLToJs(lines) { |
| 86 | + if (lines == null) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + const result = { __proto__: null }; |
| 90 | + let isInYamlBlock = false; |
| 91 | + for (let i = 0; i < lines.length; i++) { |
| 92 | + const line = lines[i]; |
| 93 | + if (isInYamlBlock && !StringPrototypeStartsWith(line, StringPrototypeRepeat(' ', isInYamlBlock.indent))) { |
| 94 | + result[isInYamlBlock.key] = isInYamlBlock.key === 'stack' ? |
| 95 | + result[isInYamlBlock.key] : ArrayPrototypeJoin(result[isInYamlBlock.key], '\n'); |
| 96 | + isInYamlBlock = false; |
| 97 | + } |
| 98 | + if (isInYamlBlock) { |
| 99 | + const blockLine = StringPrototypeSubstring(line, isInYamlBlock.indent); |
| 100 | + ArrayPrototypePush(result[isInYamlBlock.key], blockLine); |
| 101 | + continue; |
| 102 | + } |
| 103 | + const match = RegExpPrototypeExec(kYamlKeyRegex, line); |
| 104 | + if (match !== null) { |
| 105 | + const { 1: leadingSpaces, 2: key, 4: block, 5: value } = match; |
| 106 | + if (block) { |
| 107 | + isInYamlBlock = { key, indent: (leadingSpaces?.length ?? 0) + 2 }; |
| 108 | + result[key] = []; |
| 109 | + } else { |
| 110 | + result[key] = getYamlValue(value); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return reConstructError(result); |
| 115 | +} |
| 116 | + |
| 117 | +module.exports = { |
| 118 | + YAMLToJs, |
| 119 | +}; |
0 commit comments