forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C3] Reorganize helper scripts (cloudflare#5218)
* 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
Showing
56 changed files
with
2,641 additions
and
1,701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 0 additions & 192 deletions
192
packages/create-cloudflare/src/__tests__/common.test.ts
This file was deleted.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
packages/create-cloudflare/src/__tests__/deploy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.