| 1 |
import { multiple as getFixtures } from "@babel/helper-fixtures"; |
| 2 |
import _checkDuplicateNodes from "@babel/helper-check-duplicate-nodes"; |
| 3 |
import { readFileSync, unlinkSync, writeFileSync, existsSync } from "fs"; |
| 4 |
import { join } from "path"; |
| 5 |
import Difference from "./difference.js"; |
| 6 |
import FixtureError from "./fixture-error.js"; |
| 7 |
import toFuzzedOptions from "./to-fuzzed-options.js"; |
| 8 |
import { serialize, deserialize } from "./serialization.js"; |
| 9 |
import toContextualSyntaxError from "./to-contextual-syntax-error.js"; |
| 10 |
|
| 11 |
const { CI, OVERWRITE } = process.env; |
| 12 |
const { stringify, parse: JSONParse } = JSON; |
| 13 |
const checkDuplicateNodes = |
| 14 |
_checkDuplicateNodes.default || _checkDuplicateNodes; |
| 15 |
|
| 16 |
const writeFileWithNewline = (path, string) => |
| 17 |
writeFileSync(path, `${string}\n`, "utf-8"); |
| 18 |
|
| 19 |
/** |
| 20 |
* run parser on given tests |
| 21 |
* |
| 22 |
* @param {string} fixturesPath A base search path for finding fixtures. |
| 23 |
* @param {*} parseFunction A parser with the same interface of |
| 24 |
* `@babel/parser#parse` |
| 25 |
* @param {boolean} [onlyCompareErrors=false] Whether we should only compare the |
| 26 |
* "errors" of generated ast against the expected AST. Used for tests where an |
| 27 |
* ESTree AST is generated but we want to make sure `@babel/parser` still throws |
| 28 |
* expected recoverable recoverable errors on given code locations. |
| 29 |
* @returns {void} |
| 30 |
*/ |
| 31 |
export default function runFixtureTests( |
| 32 |
fixturesPath, |
| 33 |
parseFunction, |
| 34 |
onlyCompareErrors = false, |
| 35 |
) { |
| 36 |
const fixtures = getFixtures(fixturesPath); |
| 37 |
|
| 38 |
for (const [name, testSuites] of Object.entries(fixtures)) { |
| 39 |
for (const { title, tests } of testSuites) { |
| 40 |
for (const test of tests) { |
| 41 |
runAutogeneratedParseTests( |
| 42 |
parseFunction, |
| 43 |
`${name}/${title}`, |
| 44 |
test, |
| 45 |
onlyCompareErrors, |
| 46 |
); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function runAutogeneratedParseTests( |
| 53 |
parse, |
| 54 |
prefix, |
| 55 |
task, |
| 56 |
onlyCompareErrors = false, |
| 57 |
) { |
| 58 |
const { expect, options } = task; |
| 59 |
const testFn = task.disabled ? it.skip : it; |
| 60 |
|
| 61 |
const expected = deserialize(expect.loc, options, expect.code); |
| 62 |
const title = `${prefix}/${task.title}`; |
| 63 |
const toStartPosition = ({ startLine = 1, startColumn = 0 }) => |
| 64 |
`(${startLine}, ${startColumn})`; |
| 65 |
|
| 66 |
toFuzzedOptions(options) |
| 67 |
.map(([adjust, options], index) => ({ |
| 68 |
...task, |
| 69 |
title: `${title} start = ${toStartPosition(options)}`, |
| 70 |
adjust, |
| 71 |
options, |
| 72 |
expected, |
| 73 |
filename: task.actual.loc, |
| 74 |
source: task.actual.code, |
| 75 |
original: index === 0, |
| 76 |
})) |
| 77 |
.forEach(test => |
| 78 |
testFn(test.title, () => runParseTest(parse, test, onlyCompareErrors)), |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
const toJustErrors = result => ({ |
| 83 |
threw: result.threw, |
| 84 |
ast: result.ast && { errors: result.ast.errors }, |
| 85 |
}); |
| 86 |
|
| 87 |
function runParseTest(parse, test, onlyCompareErrors) { |
| 88 |
const { adjust, expected, source, filename, options } = test; |
| 89 |
|
| 90 |
if (!OVERWRITE && expected.threw && expected.ast) { |
| 91 |
throw Error( |
| 92 |
"File expected.json exists although options specify throws. Remove expected.json.", |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
const actual = parseWithRecovery(parse, source, filename, options); |
| 97 |
const difference = new Difference( |
| 98 |
adjust, |
| 99 |
onlyCompareErrors ? toJustErrors(expected) : expected, |
| 100 |
onlyCompareErrors ? toJustErrors(actual) : actual, |
| 101 |
); |
| 102 |
|
| 103 |
// No differences means we passed and there's nothing left to do. |
| 104 |
if (difference === Difference.None) return; |
| 105 |
|
| 106 |
// We only write the output of the original test, not all it's auto-generated |
| 107 |
// variations. |
| 108 |
if (!test.original) return; |
| 109 |
|
| 110 |
const testLocation = test.taskDir; |
| 111 |
|
| 112 |
// FIXME: We're just maintaining the legacy behavior of storing *just* the |
| 113 |
// error `message` here, which differs from the error's `toString()` that we |
| 114 |
// store for each error in the `errors` array. In both cases, we should |
| 115 |
// serialize the full error to be able to property test locations, |
| 116 |
// reasonCodes, etc. |
| 117 |
const throws = actual.threw ? actual.threw.message : undefined; |
| 118 |
const optionsLocation = join(testLocation, "options.json"); |
| 119 |
|
| 120 |
// We want to throw away the contents of `throws` here. |
| 121 |
// eslint-disable-next-line no-unused-vars |
| 122 |
const { throws: expectedThrows, ...oldOptions } = readJSON(optionsLocation); |
| 123 |
const newOptions = { ...oldOptions, ...(throws && { throws }) }; |
| 124 |
|
| 125 |
const normalLocation = join(testLocation, "output.json"); |
| 126 |
const extendedLocation = join(testLocation, "output.extended.json"); |
| 127 |
|
| 128 |
// If we're not overwriting the current values with whatever we get this time |
| 129 |
// around, then we have a legitimate error that we need to report. |
| 130 |
|
| 131 |
const shouldThrow = |
| 132 |
expectedThrows !== undefined || |
| 133 |
existsSync(normalLocation) || |
| 134 |
existsSync(extendedLocation); |
| 135 |
|
| 136 |
if (CI || (!OVERWRITE && shouldThrow)) { |
| 137 |
const err = new Error(); |
| 138 |
err.message = `Test Failed: ${testLocation}\nFixtureError.fromDifference: ${ |
| 139 |
FixtureError.fromDifference(difference, actual).message |
| 140 |
}`; |
| 141 |
throw err; |
| 142 |
} |
| 143 |
|
| 144 |
// Store (or overwrite) the options file if there's anything to record, |
| 145 |
// otherwise remove it. |
| 146 |
if (Object.keys(newOptions).length <= 0) { |
| 147 |
rmf(optionsLocation); |
| 148 |
} else if (throws !== expectedThrows) { |
| 149 |
// The idea here is that we shouldn't need to change anything if this doesn't |
| 150 |
// throw, and stringify will produce different output than what prettier |
| 151 |
// wants. |
| 152 |
writeFileWithNewline(optionsLocation, stringify(newOptions, null, 2)); |
| 153 |
} |
| 154 |
|
| 155 |
// When only comparing errors, we don't want to overwrite the AST JSON because |
| 156 |
// it belongs to a different test. |
| 157 |
if (onlyCompareErrors) return; |
| 158 |
|
| 159 |
const [extended, serialized] = actual.ast ? serialize(actual.ast) : []; |
| 160 |
const outputLocation = |
| 161 |
serialized && (extended ? extendedLocation : normalLocation); |
| 162 |
|
| 163 |
if (outputLocation) { |
| 164 |
writeFileWithNewline(outputLocation, serialized); |
| 165 |
} |
| 166 |
|
| 167 |
// Remove any previous output files that are no longer valid, either because |
| 168 |
// extension changed, or because we aren't writing it out at all anymore. |
| 169 |
for (const location of [normalLocation, extendedLocation]) { |
| 170 |
if (location !== outputLocation) { |
| 171 |
rmf(location); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
function readJSON(filename) { |
| 177 |
try { |
| 178 |
return JSONParse(readFileSync(filename, "utf-8")); |
| 179 |
} catch (error) { |
| 180 |
return {}; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
function rmf(path) { |
| 185 |
try { |
| 186 |
unlinkSync(path); |
| 187 |
} catch (error) { |
| 188 |
if (error.code !== "ENOENT") throw error; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
function parseWithRecovery(parse, source, filename, options) { |
| 193 |
try { |
| 194 |
const ast = parse(source, { errorRecovery: true, ...options }); |
| 195 |
checkDuplicateNodes(ast); |
| 196 |
|
| 197 |
// Normalize the AST |
| 198 |
// |
| 199 |
// TODO: We should consider doing something more involved here as |
| 200 |
// we may miss bugs where we put unexpected falsey objects in these |
| 201 |
// properties. |
| 202 |
if (ast.comments && !ast.comments.length) delete ast.comments; |
| 203 |
if (ast.errors && !ast.errors.length) delete ast.errors; |
| 204 |
else { |
| 205 |
ast.errors = ast.errors.map(error => |
| 206 |
toContextualSyntaxError(error, source, filename, options), |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
return { threw: false, ast }; |
| 211 |
} catch (error) { |
| 212 |
return { |
| 213 |
threw: toContextualSyntaxError(error, source, filename, options), |
| 214 |
ast: false, |
| 215 |
}; |
| 216 |
} |
| 217 |
} |