Skip to content

Commit

Permalink
Normalize CWD in logs and rename vars
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 13, 2019
1 parent baa8ea5 commit 64ba603
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -196,9 +196,9 @@ function run(task) {
}
}

let actualCode = actual.code;
const expectCode = expected.code;
if (!execCode || actualCode) {
const inputCode = actual.code;
const expectedCode = expected.code;
if (!execCode || inputCode) {
const actualLogs = { stdout: "", stderr: "" };
if (validateLogs) {
jest.spyOn(console, "log").mockImplementation(msg => {
Expand All @@ -209,17 +209,14 @@ function run(task) {
});
}

result = babel.transform(actualCode, getOpts(actual));
result = babel.transform(inputCode, getOpts(actual));

const expectedCode = result.code.replace(
escapeRegExp(path.resolve(__dirname, "../../../")),
"<CWD>",
);
const outputCode = normalizeOutput(result.code);

checkDuplicatedNodes(babel, result.ast);
if (
!expected.code &&
expectedCode &&
outputCode &&
!opts.throws &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
Expand All @@ -230,27 +227,26 @@ function run(task) {
);

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

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

if (actualCode) {
if (inputCode) {
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);
validateFile(normalizeOutput(actualLogs.stdout), stdout.loc, stdout.code);
validateFile(normalizeOutput(actualLogs.stderr), stderr.loc, stderr.code);
}
}

Expand Down Expand Up @@ -288,6 +284,12 @@ function validateFile(actualCode, expectedLoc, expectedCode) {
}
}

function normalizeOutput(code) {
return code
.trim()
.replace(escapeRegExp(path.resolve(__dirname, "../../../")), "<CWD>");
}

const toEqualFile = () => ({
compare: (actual, { filename, code }) => {
const pass = actual === code;
Expand Down

0 comments on commit 64ba603

Please sign in to comment.