Skip to content

Commit

Permalink
Use stderr/stdout, and add a validateLogs test option
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 13, 2019
1 parent 69af38f commit f8d0a46
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 26 deletions.
16 changes: 11 additions & 5 deletions packages/babel-helper-fixtures/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export default function get(entryLoc): Array<Suite> {
const expectLoc =
findFile(taskDir + "/output", true /* allowJSON */) ||
taskDir + "/output.js";
const stdoutLoc = taskDir + "/stdout.txt";
const stderrLoc = taskDir + "/stderr.txt";

const actualLocAlias =
suiteName + "/" + taskName + "/" + path.basename(actualLoc);
Expand All @@ -141,14 +143,14 @@ export default function get(entryLoc): Array<Suite> {
const taskOptsLoc = resolve(taskDir + "/options");
if (taskOptsLoc) extend(taskOpts, require(taskOptsLoc));

const taskLogLoc = resolve(taskDir + "/expected-log.txt");

const test = {
optionsDir: taskOptsLoc ? path.dirname(taskOptsLoc) : null,
title: humanize(taskName, true),
disabled: taskName[0] === ".",
options: taskOpts,
expectedLog: taskLogLoc ? readFile(taskLogLoc) : null,
validateLogs: taskOpts.validateLogs,
stdout: { loc: stdoutLoc, code: readFile(stdoutLoc) },
stderr: { loc: stderrLoc, code: readFile(stderrLoc) },
exec: {
loc: execLoc,
code: readFile(execLoc),
Expand Down Expand Up @@ -226,11 +228,15 @@ export default function get(entryLoc): Array<Suite> {
}
}

if (test.exec.code && test.expectedLog) {
if (!test.validateLogs && (test.stdout.code || test.stderr.code)) {
throw new Error(
"Test cannot have logs and also be executed: " + taskLogLoc,
"stdout.txt and stderr.txt are only allowed when the 'validateLogs' option is enabled: " +
(test.stdout.code ? stdoutLoc : stderrLoc),
);
}

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

Expand Down
45 changes: 27 additions & 18 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ function run(task) {
exec,
options: opts,
optionsDir,
expectedLog,
validateLogs,
stdout,
stderr,
} = task;

function getOpts(self) {
Expand Down Expand Up @@ -197,13 +199,13 @@ function run(task) {
let actualCode = actual.code;
const expectCode = expected.code;
if (!execCode || actualCode) {
let actualLogs = "";
if (expectedLog !== null) {
const actualLogs = { stdout: "", stderr: "" };
if (validateLogs) {
jest.spyOn(console, "log").mockImplementation(msg => {
actualLogs += `>>>>>> [console.log] <<<<<<\n${msg}\n\n`;
actualLogs.stdout += `${msg}\n`;
});
jest.spyOn(console, "warn").mockImplementation(msg => {
actualLogs += `>>>>>> [console.warn] <<<<<<\n${msg}\n\n`;
actualLogs.stderr += `${msg}\n`;
});
}

Expand All @@ -214,8 +216,6 @@ function run(task) {
"<CWD>",
);

if (expectedLog !== null) expect(actualLogs.trim()).toBe(expectedLog);

checkDuplicatedNodes(babel, result.ast);
if (
!expected.code &&
Expand All @@ -239,24 +239,19 @@ function run(task) {
}
} else {
actualCode = expectedCode.trim();
try {
expect(actualCode).toEqualFile({
filename: expected.loc,
code: expectCode,
});
} catch (e) {
if (!process.env.OVERWRITE) throw e;

console.log(`Updated test file: ${expected.loc}`);
fs.writeFileSync(expected.loc, `${expectedCode}\n`);
}
validateFile(actualCode, expected.loc, expectCode);

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

if (validateLogs) {
validateFile(actualLogs.stdout.trim(), stdout.loc, stdout.code);
validateFile(actualLogs.stderr.trim(), stderr.loc, stderr.code);
}
}

if (task.sourceMap) {
Expand All @@ -279,6 +274,20 @@ function run(task) {
}
}

function validateFile(actualCode, expectedLoc, expectedCode) {
try {
expect(actualCode).toEqualFile({
filename: expectedLoc,
code: expectedCode,
});
} catch (e) {
if (!process.env.OVERWRITE) throw e;

console.log(`Updated test file: ${expectedLoc}`);
fs.writeFileSync(expectedLoc, `${actualCode}\n`);
}
}

const toEqualFile = () => ({
compare: (actual, { filename, code }) => {
const pass = actual === code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"validateLogs": true,
"plugins": [
"syntax-dynamic-import",
"transform-modules-systemjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
>>>>>> [console.warn] <<<<<<
WARNING: Dynamic import() transformation must be enabled using the
@babel/plugin-proposal-dynamic-import plugin. Babel 8 will
no longer transform import() without using that plugin.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"validateLogs": true,
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"validateLogs": true,
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
>>>>>> [console.warn] <<<<<<
Dynamic import can only be supported when transforming ES modules to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"validateLogs": true,
"caller": {
"name": "test-fixture",
"supportsStaticESM": false,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"validateLogs": true,
"presets": [["env", { "modules": "amd" }]]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"validateLogs": true,
"presets": [["env", { "modules": "cjs" }]]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"validateLogs": true,
"presets": [["env", { "modules": false }]]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"validateLogs": true,
"presets": [["env", { "modules": "systemjs" }]]
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"validateLogs": true,
"presets": [["env", { "modules": "umd" }]]
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
>>>>>> [console.warn] <<<<<<
Dynamic import can only be supported when transforming ES modules to AMD, CommonJS or SystemJS. Only the parser plugin will be enabled.

0 comments on commit f8d0a46

Please sign in to comment.