Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Automatically generate test results that do not exist #14592

Merged
merged 3 commits into from May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 20 additions & 14 deletions 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";
Expand Down Expand Up @@ -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.",
);
Expand All @@ -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;
Expand All @@ -119,19 +113,34 @@ 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.
// 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) {
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.
Expand All @@ -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);
Expand Down