Skip to content

Commit

Permalink
test: log the C3 e2e log file to the console when a test fails (#7982)
Browse files Browse the repository at this point in the history
* test: log the C3 e2e log file to the console when a test fails

Hopefully this will help with debugging C3 e2e tests without having to download the log artifact files.
  • Loading branch information
petebacondarwin authored Feb 3, 2025
1 parent 59c7c8e commit b995680
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/create-cloudflare/e2e-tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "./helpers";
import type { Suite } from "vitest";

const experimental = Boolean(process.env.E2E_EXPERIMENTAL);
const experimental = process.env.E2E_EXPERIMENTAL === "true";
const frameworkToTest = getFrameworkToTest({ experimental: false });

// Note: skipIf(frameworkToTest) makes it so that all the basic C3 functionality
Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/e2e-tests/frameworks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ function getFrameworkTests(opts: {
}
}

const experimental = Boolean(process.env.E2E_EXPERIMENTAL);
const experimental = process.env.E2E_EXPERIMENTAL === "true";
const frameworkMap = getFrameworkMap({ experimental });
const frameworkTests = getFrameworkTests({ experimental });

Expand Down
28 changes: 23 additions & 5 deletions packages/create-cloudflare/e2e-tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
rmSync,
} from "fs";
import crypto from "node:crypto";
import { readFileSync } from "node:fs";
import { tmpdir } from "os";
import path from "path";
import { setTimeout } from "timers/promises";
Expand Down Expand Up @@ -323,16 +324,21 @@ export const waitForExit = async (
};
};

export const createTestLogStream = (
const createTestLogStream = (
opts: { experimental: boolean },
task: RunnerTestCase,
) => {
// The .ansi extension allows for editor extensions that format ansi terminal codes
const fileName = `${normalizeTestName(task)}.ansi`;
assert(task.suite, "Expected task.suite to be defined");
return createWriteStream(path.join(getLogPath(opts, task.suite), fileName), {
const logPath = path.join(getLogPath(opts, task.suite), fileName);
const logStream = createWriteStream(logPath, {
flags: "a",
});
return {
logPath,
logStream,
};
};

export const recreateLogFolder = (
Expand Down Expand Up @@ -374,7 +380,7 @@ const normalizeTestName = (task: Test) => {
return baseName + suffix;
};

export const testProjectDir = (suite: string, test: string) => {
const testProjectDir = (suite: string, test: string) => {
const tmpDirPath =
process.env.E2E_PROJECT_PATH ??
realpathSync(mkdtempSync(path.join(tmpdir(), `c3-tests-${suite}`)));
Expand Down Expand Up @@ -496,9 +502,21 @@ export const test = (opts: { experimental: boolean }) =>
await use({ path: getPath(), name: getName() });
clean();
},
async logStream({ task }, use) {
const logStream = createTestLogStream(opts, task);
async logStream({ task, onTestFailed }, use) {
const { logPath, logStream } = createTestLogStream(opts, task);

onTestFailed(() => {
console.error("##[group]Logs from failed test:", logPath);
try {
console.error(readFileSync(logPath, "utf8"));
} catch {
console.error("Unable to read log file");
}
console.error("##[endgroup]");
});

await use(logStream);

logStream.close();
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/e2e-tests/workers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function getWorkerTests(opts: { experimental: boolean }): WorkerTestConfig[] {
}
}

const experimental = Boolean(process.env.E2E_EXPERIMENTAL);
const experimental = process.env.E2E_EXPERIMENTAL === "true";
const workerTests = getWorkerTests({ experimental });

describe
Expand Down

0 comments on commit b995680

Please sign in to comment.