Skip to content

Commit

Permalink
[C3] Reorganize helper scripts (cloudflare#5218)
Browse files Browse the repository at this point in the history
* C3: Update SolidStart template to work with create-solid@0.5.3

* Move sleep helper to its own file

* Move package management helpers out of command.ts into dedicated files

* Move the retry helper to its own file

* Consolidate compat date helpers in their own file

* Move installWrangler to packages

* Remove logging helper. This problem was solved by the e2e logging rework

* Improve docstrings

* fixup import statements

* Move wrangler.toml related functions into their own file

* Move wrangler accounts functions into dedicated file. Move wrangler folder out of helpers

* Relocate runFrameworkGenerator

* Add unit tests for runFrameworkGenerator

* Move quoteShellArgs to command

* Move validateProjectDirectory to validators helper

* Move git helpers to dedicated file

* Reorganize remaining bits of common.ts

* Add unit tests for git helpers

* Adding unit tests for deploy helpers

* Make spinner mocking consistent across tests

* Remove runCommands and add tests for gitCommit

* Remove the mockRunCommand helper

* Addressing PR feedback

* changeset
  • Loading branch information
jculvey authored Mar 14, 2024
1 parent 86c5f98 commit 0c535e7
Show file tree
Hide file tree
Showing 56 changed files with 2,641 additions and 1,701 deletions.
8 changes: 8 additions & 0 deletions .changeset/fuzzy-berries-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"create-cloudflare": patch
---

refactor: Refactor C3 internal helpers. Includes a few small changes:

- Drops `--save` from internal `pnpm` and `npm` install invocations
- Switches to `git branch --show-current` for detecting current branch
6 changes: 3 additions & 3 deletions packages/create-cloudflare/e2e-tests/frameworks.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync } from "fs";
import { cp } from "fs/promises";
import { join } from "path";
import { retry } from "helpers/command";
import { sleep } from "helpers/common";
import { detectPackageManager } from "helpers/packages";
import { detectPackageManager } from "helpers/packageManagers";
import { retry } from "helpers/retry";
import { sleep } from "helpers/sleep";
import { fetch } from "undici";
import {
afterEach,
Expand Down
4 changes: 2 additions & 2 deletions packages/create-cloudflare/e2e-tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { tmpdir } from "os";
import { basename, join } from "path";
import { stripAnsi } from "@cloudflare/cli";
import { spawn } from "cross-spawn";
import { retry } from "helpers/command";
import { sleep } from "helpers/common";
import { retry } from "helpers/retry";
import { sleep } from "helpers/sleep";
import { fetch } from "undici";
import { expect } from "vitest";
import { version } from "../package.json";
Expand Down
4 changes: 2 additions & 2 deletions packages/create-cloudflare/e2e-tests/workers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "path";
import { retry } from "helpers/command";
import { sleep } from "helpers/common";
import { readToml } from "helpers/files";
import { retry } from "helpers/retry";
import { sleep } from "helpers/sleep";
import { fetch } from "undici";
import { beforeAll, beforeEach, describe, expect, test } from "vitest";
import { deleteWorker } from "../scripts/common";
Expand Down
192 changes: 0 additions & 192 deletions packages/create-cloudflare/src/__tests__/common.test.ts

This file was deleted.

125 changes: 125 additions & 0 deletions packages/create-cloudflare/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { crash } from "@cloudflare/cli";
import { processArgument } from "@cloudflare/cli/args";
import { mockPackageManager, mockSpinner } from "helpers/__tests__/mocks";
import { runCommand } from "helpers/command";
import { readFile } from "helpers/files";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { offerToDeploy, runDeploy } from "../deploy";
import { chooseAccount, wranglerLogin } from "../wrangler/accounts";
import { createTestContext } from "./helpers";

vi.mock("helpers/command");
vi.mock("../wrangler/accounts");
vi.mock("@cloudflare/cli/args");
vi.mock("@cloudflare/cli/interactive");
vi.mock("which-pm-runs");
vi.mock("@cloudflare/cli");
vi.mock("helpers/files");

const mockInsideGitRepo = (isInside = true) => {
if (isInside) {
vi.mocked(runCommand).mockResolvedValueOnce(
"On branch master\nnothing to commit, working tree clean"
);
} else {
vi.mocked(runCommand).mockRejectedValueOnce(
new Error(
"fatal: not a git repository (or any of the parent directories): .git"
)
);
}
};

describe("deploy helpers", async () => {
beforeEach(() => {
mockPackageManager("npm");

mockSpinner();
});

describe("offerToDeploy", async () => {
test("user selects yes and succeeds", async () => {
const ctx = createTestContext();
ctx.template.platform = "pages";
// mock the user selecting yes when asked to deploy
vi.mocked(processArgument).mockResolvedValueOnce(true);
// mock a successful wrangler login
vi.mocked(wranglerLogin).mockResolvedValueOnce(true);

await expect(offerToDeploy(ctx)).resolves.toBe(true);
});

test("project is undeployable", async () => {
const ctx = createTestContext();
// Can't deploy things with bindings (yet!)
vi.mocked(readFile).mockReturnValue(`binding = "MY_QUEUE"`);

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(processArgument).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(wranglerLogin).not.toHaveBeenCalled();
});

test("--no-deploy from command line", async () => {
const ctx = createTestContext();
ctx.args.deploy = false;
ctx.template.platform = "pages";

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(processArgument).toHaveBeenCalledOnce();
expect(ctx.args.deploy).toBe(false);
expect(wranglerLogin).not.toHaveBeenCalled();
});

test("wrangler login failure", async () => {
const ctx = createTestContext();
ctx.template.platform = "pages";
vi.mocked(processArgument).mockResolvedValueOnce(true);
vi.mocked(wranglerLogin).mockResolvedValueOnce(false);

await expect(offerToDeploy(ctx)).resolves.toBe(false);
expect(chooseAccount).not.toHaveBeenCalled();
});
});

describe("runDeploy", async () => {
const commitMsg = "initial commit";
const deployedUrl = "https://test-project-1234.pages.dev";

test("happy path", async () => {
const ctx = createTestContext();
ctx.account = { id: "test1234", name: "Test Account" };
ctx.template.platform = "pages";
ctx.commitMessage = commitMsg;
mockInsideGitRepo(false);
vi.mocked(runCommand).mockResolvedValueOnce(deployedUrl);

await runDeploy(ctx);
expect(crash).not.toHaveBeenCalled();
expect(runCommand).toHaveBeenCalledWith(
["npm", "run", "deploy", "--", "--commit-message", `"${commitMsg}"`],
expect.any(Object)
);
expect(ctx.deployment.url).toBe(deployedUrl);
});

test("no account in ctx", async () => {
const ctx = createTestContext();
ctx.account = undefined;
await runDeploy(ctx);
expect(crash).toHaveBeenCalledWith("Failed to read Cloudflare account.");
});

test("Failed deployment", async () => {
const ctx = createTestContext();
ctx.account = { id: "test1234", name: "Test Account" };
ctx.template.platform = "pages";
ctx.commitMessage = commitMsg;
mockInsideGitRepo(false);
vi.mocked(runCommand).mockResolvedValueOnce("");

await runDeploy(ctx);
expect(crash).toHaveBeenCalledWith("Failed to find deployment url.");
});
});
});
Loading

0 comments on commit 0c535e7

Please sign in to comment.