From b08b6927a92e6b5bdd1b26c1d4cb77c2e3978c73 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 25 May 2022 22:57:03 +0800 Subject: [PATCH 1/3] commit --- .../test/helpers/run-fixture-tests.js | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/babel-parser/test/helpers/run-fixture-tests.js b/packages/babel-parser/test/helpers/run-fixture-tests.js index 89057dc3f0e0..345509b87a59 100644 --- a/packages/babel-parser/test/helpers/run-fixture-tests.js +++ b/packages/babel-parser/test/helpers/run-fixture-tests.js @@ -1,6 +1,6 @@ import { multiple as getFixtures } from "@babel/helper-fixtures"; import _checkDuplicateNodes from "@babel/helper-check-duplicate-nodes"; -import { readFileSync, unlinkSync, writeFileSync } from "fs"; +import { readFileSync, unlinkSync, writeFileSync, existsSync } from "fs"; import { join } from "path"; import Difference from "./difference.js"; import FixtureError from "./fixture-error.js"; @@ -102,12 +102,6 @@ function runParseTest(parse, test, onlyCompareErrors) { // No differences means we passed and there's nothing left to do. if (difference === Difference.None) return; - const error = FixtureError.fromDifference(difference, actual); - - // If we're not overwriting the current values with whatever we get this time - // around, then we have a legitimate error that we need to report. - if (CI || !OVERWRITE) throw error; - // We only write the output of the original test, not all it's auto-generated // variations. if (!test.original) return; @@ -124,9 +118,24 @@ function runParseTest(parse, test, onlyCompareErrors) { // We want to throw away the contents of `throws` here. // eslint-disable-next-line no-unused-vars - const { throws: _, ...oldOptions } = readJSON(optionsLocation); + const { throws: expectedThrows, ...oldOptions } = readJSON(optionsLocation); const newOptions = { ...oldOptions, ...(throws && { throws }) }; + const normalLocation = join(testLocation, "output.json"); + const extendedLocation = join(testLocation, "output.extended.json"); + + // If we're not overwriting the current values with whatever we get this time + // around, then we have a legitimate error that we need to report. + + const shouldThrow = + expectedThrows !== undefined || + existsSync(normalLocation) || + existsSync(extendedLocation); + + if (CI || (!OVERWRITE && shouldThrow)) { + throw FixtureError.fromDifference(difference, actual); + } + // Store (or overwrite) the options file if there's anything to record, // otherwise remove it. if (Object.keys(newOptions).length <= 0) { @@ -142,9 +151,6 @@ function runParseTest(parse, test, onlyCompareErrors) { // it belongs to a different test. if (onlyCompareErrors) return; - const normalLocation = join(testLocation, "output.json"); - const extendedLocation = join(testLocation, "output.extended.json"); - const [extended, serialized] = actual.ast ? serialize(actual.ast) : []; const outputLocation = serialized && (extended ? extendedLocation : normalLocation); From d256261ead908c527d0509d047c7062d26fd524d Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Fri, 27 May 2022 17:25:49 +0800 Subject: [PATCH 2/3] improve --- packages/babel-parser/test/helpers/run-fixture-tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/test/helpers/run-fixture-tests.js b/packages/babel-parser/test/helpers/run-fixture-tests.js index 345509b87a59..8d247a5fb54b 100644 --- a/packages/babel-parser/test/helpers/run-fixture-tests.js +++ b/packages/babel-parser/test/helpers/run-fixture-tests.js @@ -86,7 +86,7 @@ const toJustErrors = result => ({ function runParseTest(parse, test, onlyCompareErrors) { const { adjust, expected, source, filename, options } = test; - if (expected.threw && expected.ast) { + if (!OVERWRITE && expected.threw && expected.ast) { throw Error( "File expected.json exists although options specify throws. Remove expected.json.", ); @@ -140,7 +140,7 @@ function runParseTest(parse, test, onlyCompareErrors) { // otherwise remove it. if (Object.keys(newOptions).length <= 0) { rmf(optionsLocation); - } else if (throws) { + } else if (throws !== expectedThrows) { // The idea here is that we shouldn't need to change anything if this doesn't // throw, and stringify will produce different output than what prettier // wants. From 2358cc4c106e2656bea1ee27665680d4ff87efbe Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Wed, 1 Jun 2022 04:04:41 +0800 Subject: [PATCH 3/3] fix --- packages/babel-parser/test/helpers/run-fixture-tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/test/helpers/run-fixture-tests.js b/packages/babel-parser/test/helpers/run-fixture-tests.js index 8d247a5fb54b..9719ba02ad4a 100644 --- a/packages/babel-parser/test/helpers/run-fixture-tests.js +++ b/packages/babel-parser/test/helpers/run-fixture-tests.js @@ -113,7 +113,7 @@ function runParseTest(parse, test, onlyCompareErrors) { // store for each error in the `errors` array. In both cases, we should // serialize the full error to be able to property test locations, // reasonCodes, etc. - const throws = !!actual.threw && actual.threw.message; + const throws = actual.threw ? actual.threw.message : undefined; const optionsLocation = join(testLocation, "options.json"); // We want to throw away the contents of `throws` here.