Skip to content

Commit

Permalink
fix(create-cloudflare): Ensure that C3 e2e tests are running against …
Browse files Browse the repository at this point in the history
…local wrangler
  • Loading branch information
CarmenPopoviciu committed Jan 23, 2025
1 parent a7163b3 commit 573796e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
15 changes: 15 additions & 0 deletions packages/create-cloudflare/e2e-tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "assert";
import { execSync } from "child_process";
import {
createWriteStream,
mkdirSync,
Expand Down Expand Up @@ -47,6 +48,7 @@ const testEnv = {
npm_config_cache: "./.npm/cache",
// unset the VITEST env variable as this causes e2e issues with some frameworks
VITEST: undefined,
WRANGLER: packWrangler(),
};

export type PromptHandler = {
Expand Down Expand Up @@ -508,3 +510,16 @@ export function kill(proc: ChildProcess) {
(resolve) => proc.pid && treeKill(proc.pid, "SIGINT", () => resolve()),
);
}

export function packWrangler(): string {
const pathToWrangler = path.resolve(__dirname, "../../wrangler");
execSync("pnpm pack --pack-destination ./.pack", { cwd: pathToWrangler });

const versions = execSync("ls -1 .pack", {
encoding: "utf-8",
cwd: pathToWrangler,
});
const wranglerVersion = versions.trim().split("\n").at(-1); // get last wrangler version
assert(wranglerVersion);
return path.join(pathToWrangler, ".pack", wranglerVersion);
}
30 changes: 23 additions & 7 deletions packages/create-cloudflare/src/helpers/__tests__/packages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe("Package Helpers", () => {
vi.mocked(existsSync).mockImplementation(() => false);
});

afterEach(() => {});
afterEach(() => {
vi.unstubAllEnvs();
});

describe("npmInstall", () => {
test("npm", async () => {
Expand Down Expand Up @@ -87,12 +89,26 @@ describe("Package Helpers", () => {
);
});

test("installWrangler", async () => {
await installWrangler();
describe("installWrangler", async () => {
test("install latest wrangler version if not in CI", async () => {
vi.stubEnv("CI", undefined);
await installWrangler();

expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["npm", "install", "--save-dev", "wrangler@latest"],
expect.anything(),
);
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["npm", "install", "--save-dev", "wrangler@latest"],
expect.anything(),
);
});

test("install local wrangler version if in CI", async () => {
vi.stubEnv("CI", "true");
vi.stubEnv("WRANGLER", "local-wrangler.tar.gz");
await installWrangler();

expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["npm", "install", "--save-dev", "file:local-wrangler.tar.gz"],
expect.anything(),
);
});
});
});
8 changes: 7 additions & 1 deletion packages/create-cloudflare/src/helpers/packages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from "fs";
import path from "path";
import { log } from "@cloudflare/cli";
import { brandColor, dim } from "@cloudflare/cli/colors";
import { fetch } from "undici";
import { runCommand } from "./command";
Expand Down Expand Up @@ -88,9 +89,14 @@ export async function getLatestPackageVersion(packageSpecifier: string) {
*/
export const installWrangler = async () => {
const { npm } = detectPackageManager();
const wrangler = process.env.CI
? `file:${process.env.WRANGLER}`
: "wrangler@latest";

log(`Using wrangler ${wrangler}`);

// Even if Wrangler is already installed, make sure we install the latest version, as some framework CLIs are pinned to an older version
await installPackages([`wrangler@latest`], {
await installPackages([`${wrangler}`], {
dev: true,
startText: `Installing wrangler ${dim(
"A command line tool for building Cloudflare Workers",
Expand Down
3 changes: 2 additions & 1 deletion packages/create-cloudflare/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"E2E_RETRIES",
"E2E_NO_DEPLOY",
"E2E_EXPERIMENTAL",
"TEST_PM"
"TEST_PM",
"WRANGLER"
],
"dependsOn": ["build"],
"outputs": [".e2e-logs", ".e2e-logs-experimental"]
Expand Down

0 comments on commit 573796e

Please sign in to comment.