Skip to content

Commit

Permalink
Use "validateLogs" for preset-env's debug fixtures (#10401)
Browse files Browse the repository at this point in the history
* Add ability to ignore output to the test runner

* Use normal fixtures instead of custom debug-fixtures in preset-env
  • Loading branch information
nicolo-ribaudo committed Sep 6, 2019
1 parent 8769903 commit 3e8a5c5
Show file tree
Hide file tree
Showing 269 changed files with 1,354 additions and 555 deletions.
15 changes: 15 additions & 0 deletions packages/babel-helper-fixtures/src/index.js
Expand Up @@ -149,6 +149,7 @@ export default function get(entryLoc): Array<Suite> {
disabled: taskName[0] === ".",
options: taskOpts,
validateLogs: taskOpts.validateLogs,
ignoreOutput: taskOpts.ignoreOutput,
stdout: { loc: stdoutLoc, code: readFile(stdoutLoc) },
stderr: { loc: stderrLoc, code: readFile(stderrLoc) },
exec: {
Expand Down Expand Up @@ -234,9 +235,23 @@ export default function get(entryLoc): Array<Suite> {
(test.stdout.code ? stdoutLoc : stderrLoc),
);
}
if (test.options.ignoreOutput) {
if (test.expect.code) {
throw new Error(
"Test cannot ignore its output and also validate it: " + expectLoc,
);
}
if (!test.validateLogs) {
throw new Error(
"ignoreOutput can only be used when validateLogs is true: " +
taskOptsLoc,
);
}
}

// Delete to avoid option validation error
delete test.options.validateLogs;
delete test.options.ignoreOutput;
}
}

Expand Down
60 changes: 33 additions & 27 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -138,6 +138,7 @@ function run(task) {
options: opts,
optionsDir,
validateLogs,
ignoreOutput,
stdout,
stderr,
} = task;
Expand Down Expand Up @@ -221,33 +222,35 @@ function run(task) {
const outputCode = normalizeOutput(result.code);

checkDuplicatedNodes(babel, result.ast);
if (
!expected.code &&
outputCode &&
!opts.throws &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
const expectedFile = expected.loc.replace(
/\.m?js$/,
result.sourceType === "module" ? ".mjs" : ".js",
);

console.log(`New test file created: ${expectedFile}`);
fs.writeFileSync(expectedFile, `${outputCode}\n`);

if (expected.loc !== expectedFile) {
try {
fs.unlinkSync(expected.loc);
} catch (e) {}
}
} else {
validateFile(outputCode, expected.loc, expectedCode);

if (inputCode) {
expect(expected.loc).toMatch(
result.sourceType === "module" ? /\.mjs$/ : /\.js$/,
if (!ignoreOutput) {
if (
!expected.code &&
outputCode &&
!opts.throws &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
const expectedFile = expected.loc.replace(
/\.m?js$/,
result.sourceType === "module" ? ".mjs" : ".js",
);

console.log(`New test file created: ${expectedFile}`);
fs.writeFileSync(expectedFile, `${outputCode}\n`);

if (expected.loc !== expectedFile) {
try {
fs.unlinkSync(expected.loc);
} catch (e) {}
}
} else {
validateFile(outputCode, expected.loc, expectedCode);

if (inputCode) {
expect(expected.loc).toMatch(
result.sourceType === "module" ? /\.mjs$/ : /\.js$/,
);
}
}
}

Expand Down Expand Up @@ -294,7 +297,10 @@ function validateFile(actualCode, expectedLoc, expectedCode) {
function normalizeOutput(code) {
return code
.trim()
.replace(escapeRegExp(path.resolve(__dirname, "../../../")), "<CWD>");
.replace(
new RegExp(escapeRegExp(path.resolve(__dirname, "../../../")), "g"),
"<CWD>",
);
}

const toEqualFile = () => ({
Expand Down
148 changes: 0 additions & 148 deletions packages/babel-preset-env/test/debug-fixtures.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,6 +1,8 @@
{
"validateLogs": true,
"ignoreOutput": true,
"presets": [
["../../lib", {
["env", {
"debug": true,
"corejs": 3
}]
Expand Down
Expand Up @@ -44,4 +44,3 @@ Using plugins:
proposal-dynamic-import {}

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
Successfully compiled 1 file with Babel.

0 comments on commit 3e8a5c5

Please sign in to comment.