chore: import upstream snapshot with attribution
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
import os from "node:os";
|
||||
import ci from "ci-info";
|
||||
import { beforeEach, describe, it, vi } from "vitest";
|
||||
import { checkMacOSVersion } from "../check-macos-version";
|
||||
|
||||
vi.mock("node:os");
|
||||
vi.mock("ci-info", () => ({
|
||||
default: { isCI: false },
|
||||
isCI: false,
|
||||
}));
|
||||
|
||||
const mockOs = vi.mocked(os);
|
||||
|
||||
describe("checkMacOSVersion", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(ci).isCI = false;
|
||||
});
|
||||
|
||||
it("should not throw on non-macOS platforms", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should not throw on macOS 13.5.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("22.6.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should not throw on macOS 14.0.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("23.0.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should not throw on macOS 13.6.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("22.7.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should throw error on macOS 12.7.6", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("21.6.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).toThrow(
|
||||
"Unsupported macOS version: The Cloudflare Workers runtime cannot run on the current version of macOS (12.6.0)"
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error on macOS 13.4.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("22.4.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).toThrow(
|
||||
"Unsupported macOS version: The Cloudflare Workers runtime cannot run on the current version of macOS (13.4.0)"
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle invalid Darwin version format gracefully", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("invalid-version");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should handle very old Darwin versions gracefully", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("19.6.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
|
||||
it("should not throw when running in CI", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
vi.mocked(ci).isCI = true;
|
||||
mockOs.release.mockReturnValue("21.6.0");
|
||||
|
||||
expect(() => checkMacOSVersion({ shouldThrow: true })).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkMacOSVersion with shouldThrow=false", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(ci).isCI = false;
|
||||
});
|
||||
|
||||
it("should not warn on non-macOS platforms", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("linux");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not warn on macOS 13.5.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("22.6.0");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should warn on macOS 12.7.6", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("21.6.0");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining(
|
||||
"⚠️ Warning: Unsupported macOS version detected (12.6.0)"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should warn on macOS 13.4.0", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("22.4.0");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining(
|
||||
"⚠️ Warning: Unsupported macOS version detected (13.4.0)"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should not warn when running in CI", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
vi.mocked(ci).isCI = true;
|
||||
mockOs.release.mockReturnValue("21.6.0");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not warn on invalid Darwin version format", ({ expect }) => {
|
||||
vi.spyOn(process, "platform", "get").mockReturnValue("darwin");
|
||||
mockOs.release.mockReturnValue("invalid-version");
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
checkMacOSVersion({ shouldThrow: false });
|
||||
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
import { describe, test } from "vitest";
|
||||
import { space } from "..";
|
||||
|
||||
describe("cli", () => {
|
||||
test("test spaces", ({ expect }) => {
|
||||
expect(space(300)).toMatchInlineSnapshot(
|
||||
'" "'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { spawn } from "cross-spawn";
|
||||
import { afterEach, beforeEach, describe, test, vi } from "vitest";
|
||||
import { quoteShellArgs, runCommand } from "../command";
|
||||
import type { ChildProcess } from "node:child_process";
|
||||
|
||||
// We can change how the mock spawn works by setting these variables
|
||||
let spawnResultCode = 0;
|
||||
let spawnStdout: string | undefined = undefined;
|
||||
let spawnStderr: string | undefined = undefined;
|
||||
|
||||
vi.mock("cross-spawn");
|
||||
|
||||
describe("Command Helpers", () => {
|
||||
afterEach(() => {
|
||||
spawnResultCode = 0;
|
||||
spawnStdout = undefined;
|
||||
spawnStderr = undefined;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(spawn).mockImplementation(() => {
|
||||
return {
|
||||
on: vi.fn().mockImplementation((event, cb) => {
|
||||
if (event === "close") {
|
||||
cb(spawnResultCode);
|
||||
}
|
||||
}),
|
||||
stdout: {
|
||||
on(_event: "data", cb: (data: string) => void) {
|
||||
if (spawnStdout !== undefined) {
|
||||
cb(spawnStdout);
|
||||
}
|
||||
},
|
||||
},
|
||||
stderr: {
|
||||
on(_event: "data", cb: (data: string) => void) {
|
||||
if (spawnStderr !== undefined) {
|
||||
cb(spawnStderr);
|
||||
}
|
||||
},
|
||||
},
|
||||
} as unknown as ChildProcess;
|
||||
});
|
||||
});
|
||||
|
||||
test("runCommand", async ({ expect }) => {
|
||||
await runCommand(["ls", "-l"]);
|
||||
expect(spawn).toHaveBeenCalledWith("ls", ["-l"], {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
signal: expect.any(AbortSignal),
|
||||
});
|
||||
});
|
||||
|
||||
describe("quoteShellArgs", () => {
|
||||
test.runIf(process.platform !== "win32")("mac", async ({ expect }) => {
|
||||
expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev");
|
||||
expect(quoteShellArgs([`24.02 foo-bar`])).toEqual(`'24.02 foo-bar'`);
|
||||
expect(quoteShellArgs([`foo/10 bar/20-baz/`])).toEqual(
|
||||
`'foo/10 bar/20-baz/'`
|
||||
);
|
||||
});
|
||||
|
||||
test.runIf(process.platform === "win32")("windows", async ({ expect }) => {
|
||||
expect(quoteShellArgs([`pages:dev`])).toEqual("pages:dev");
|
||||
expect(quoteShellArgs([`24.02 foo-bar`])).toEqual(`"24.02 foo-bar"`);
|
||||
expect(quoteShellArgs([`foo/10 bar/20-baz/`])).toEqual(
|
||||
`"foo/10 bar/20-baz/"`
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,445 @@
|
||||
import { appendFileSync, existsSync, statSync, writeFileSync } from "node:fs";
|
||||
import { readFileSync } from "@cloudflare/workers-utils";
|
||||
import { beforeEach, describe, test, vi } from "vitest";
|
||||
import {
|
||||
maybeAppendWranglerToGitIgnore,
|
||||
maybeAppendWranglerToGitIgnoreLikeFile,
|
||||
} from "../gitignore";
|
||||
import type { PathLike } from "node:fs";
|
||||
import type { Mock } from "vitest";
|
||||
|
||||
vi.mock("node:fs");
|
||||
vi.mock("@cloudflare/workers-utils");
|
||||
vi.mock("../interactive", () => ({
|
||||
spinner: () => ({
|
||||
start: vi.fn(),
|
||||
stop: vi.fn(),
|
||||
update: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
describe("maybeAppendWranglerToGitIgnoreLikeFile", () => {
|
||||
let writeFileSyncMock: Mock;
|
||||
let appendFileSyncMock: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
|
||||
writeFileSyncMock = vi.mocked(writeFileSync);
|
||||
appendFileSyncMock = vi.mocked(appendFileSync);
|
||||
});
|
||||
|
||||
test("should append the wrangler section to a standard gitignore file", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.vscode`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
|
||||
# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should not touch the file if it already contains all wrangler files", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
.vscode
|
||||
.wrangler
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should not touch the file if it contains all wrangler files (and can cope with comments)", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.wrangler # This is for wrangler
|
||||
.dev.vars* # this is for wrangler and getPlatformProxy
|
||||
!.dev.vars.example # more comments
|
||||
.env* # even more
|
||||
!.env.example # and a final one
|
||||
.vscode
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should append to the file the missing wrangler files when some are already present (should add the section heading if including .wrangler and some others)", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.dev.vars*
|
||||
.vscode`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
|
||||
# wrangler files
|
||||
.wrangler
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should append to the file the missing wrangler files when some are already present (should not add the section heading if .wrangler already exists)", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
.vscode`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should append to the file the missing wrangler files when some are already present (should not add the section heading if only adding .wrangler)", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
.vscode`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
|
||||
.wrangler
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("when it appends to the file it doesn't include an empty line only if there was one already", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
.vscode
|
||||
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
.wrangler
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should create the file if it didn't exist already", ({ expect }) => {
|
||||
mockIgnoreFile("my-project/.gitignore", "");
|
||||
// pretend that the file doesn't exist
|
||||
vi.mocked(existsSync).mockImplementation(() => false);
|
||||
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
// writeFile wrote the (empty) file
|
||||
expect(writeFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"",
|
||||
],
|
||||
]
|
||||
`);
|
||||
|
||||
// and the correct lines were then added to it
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should add the wildcard .dev.vars* entry even if a .dev.vars is already included", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.dev.vars
|
||||
.vscode
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should not add the .env entries if some form of .env entries are already included", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should not add the .wrangler entry if a .wrangler/ is already included", ({
|
||||
expect,
|
||||
}) => {
|
||||
mockIgnoreFile(
|
||||
"my-project/.gitignore",
|
||||
`
|
||||
node_modules
|
||||
.wrangler/ # This is for wrangler
|
||||
.vscode
|
||||
`
|
||||
);
|
||||
maybeAppendWranglerToGitIgnoreLikeFile("my-project/.gitignore");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
function mockIgnoreFile(path: string, content: string) {
|
||||
vi.mocked(existsSync).mockImplementation(
|
||||
(filePath: PathLike) => filePath === path
|
||||
);
|
||||
vi.mocked(readFileSync).mockImplementation((filePath: string) =>
|
||||
filePath === path ? content.replace(/\n\s*/g, "\n") : ""
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
describe("maybeAppendWranglerToGitIgnore", () => {
|
||||
let appendFileSyncMock: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
|
||||
appendFileSyncMock = vi.mocked(appendFileSync);
|
||||
|
||||
vi.mocked(statSync).mockImplementation(((path: string) => ({
|
||||
isDirectory() {
|
||||
return path.endsWith(".git");
|
||||
},
|
||||
})) as typeof statSync);
|
||||
});
|
||||
|
||||
test("should not create the gitignore file if neither the .git directory not the .gitingore file exist", ({
|
||||
expect,
|
||||
}) => {
|
||||
// no .gitignore file exists
|
||||
vi.mocked(existsSync).mockImplementation(() => false);
|
||||
// neither a .git directory does
|
||||
vi.mocked(statSync).mockImplementation(() => {
|
||||
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
|
||||
});
|
||||
|
||||
maybeAppendWranglerToGitIgnore("my-project");
|
||||
|
||||
expect(vi.mocked(writeFileSync)).not.toHaveBeenCalled();
|
||||
expect(appendFileSyncMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("should create a .gitignore file when .git directory exists", ({
|
||||
expect,
|
||||
}) => {
|
||||
// no .gitignore file exists
|
||||
vi.mocked(existsSync).mockImplementation(() => false);
|
||||
vi.mocked(readFileSync).mockImplementation(() => "");
|
||||
|
||||
maybeAppendWranglerToGitIgnore("my-project");
|
||||
|
||||
// writeFileSync created the (empty) file
|
||||
expect(vi.mocked(writeFileSync).mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"",
|
||||
],
|
||||
]
|
||||
`);
|
||||
|
||||
// and the correct wrangler lines were appended to it
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test("should append wrangler entries when the .gitignore file exists but the .git directory does not", ({
|
||||
expect,
|
||||
}) => {
|
||||
// .gitignore exists
|
||||
vi.mocked(existsSync).mockImplementation(
|
||||
(filePath: PathLike) => filePath === "my-project/.gitignore"
|
||||
);
|
||||
vi.mocked(readFileSync).mockImplementation((filePath: string) =>
|
||||
filePath === "my-project/.gitignore" ? "\nnode_modules\n.vscode\n" : ""
|
||||
);
|
||||
// .git directory does NOT exist
|
||||
vi.mocked(statSync).mockImplementation(() => {
|
||||
throw Object.assign(new Error("ENOENT"), { code: "ENOENT" });
|
||||
});
|
||||
|
||||
maybeAppendWranglerToGitIgnore("my-project");
|
||||
|
||||
expect(appendFileSyncMock.mock.calls).toMatchInlineSnapshot(`
|
||||
[
|
||||
[
|
||||
"my-project/.gitignore",
|
||||
"
|
||||
# wrangler files
|
||||
.wrangler
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
",
|
||||
],
|
||||
]
|
||||
`);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,154 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { parsePackageJSON, readFileSync } from "@cloudflare/workers-utils";
|
||||
import { afterEach, describe, test, vi } from "vitest";
|
||||
import { runCommand } from "../command";
|
||||
import { installPackages, installWrangler } from "../packages";
|
||||
|
||||
vi.mock("../command");
|
||||
vi.mock("@cloudflare/workers-utils", () => ({
|
||||
readFileSync: vi.fn(),
|
||||
parsePackageJSON: vi.fn(),
|
||||
}));
|
||||
vi.mock("node:fs/promises", () => ({
|
||||
writeFile: vi.fn(),
|
||||
}));
|
||||
|
||||
describe("Package Helpers", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("installPackages", async () => {
|
||||
type TestCase = {
|
||||
pm: "npm" | "pnpm" | "yarn" | "bun";
|
||||
initialArgs: string[];
|
||||
additionalArgs?: string[];
|
||||
};
|
||||
|
||||
const cases: TestCase[] = [
|
||||
{ pm: "npm", initialArgs: ["npm", "install"] },
|
||||
{ pm: "pnpm", initialArgs: ["pnpm", "install"] },
|
||||
{ pm: "bun", initialArgs: ["bun", "add"] },
|
||||
{ pm: "yarn", initialArgs: ["yarn", "add"] },
|
||||
];
|
||||
|
||||
test.for(cases)(
|
||||
"with $pm",
|
||||
async ({ pm, initialArgs, additionalArgs }, { expect }) => {
|
||||
const mockPkgJson = {
|
||||
dependencies: {
|
||||
foo: "^1.0.0",
|
||||
bar: "^2.0.0",
|
||||
baz: "^1.2.3",
|
||||
},
|
||||
};
|
||||
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson));
|
||||
vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson);
|
||||
|
||||
const packages = ["foo", "bar@latest", "baz@1.2.3"];
|
||||
await installPackages(pm, packages);
|
||||
|
||||
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
|
||||
[...initialArgs, ...packages, ...(additionalArgs ?? [])],
|
||||
expect.anything()
|
||||
);
|
||||
|
||||
if (pm === "npm") {
|
||||
const writeFileCall = vi.mocked(writeFile).mock.calls[0];
|
||||
expect(writeFileCall[0]).toBe(resolve(process.cwd(), "package.json"));
|
||||
expect(JSON.parse(writeFileCall[1] as string)).toMatchObject({
|
||||
dependencies: {
|
||||
foo: "^1.0.0",
|
||||
bar: "^2.0.0",
|
||||
baz: "1.2.3",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const devCases: TestCase[] = [
|
||||
{ pm: "npm", initialArgs: ["npm", "install", "--save-dev"] },
|
||||
{ pm: "pnpm", initialArgs: ["pnpm", "install", "--save-dev"] },
|
||||
{ pm: "bun", initialArgs: ["bun", "add", "-d"] },
|
||||
{ pm: "yarn", initialArgs: ["yarn", "add", "-D"] },
|
||||
];
|
||||
|
||||
test.for(devCases)(
|
||||
"with $pm (dev = true)",
|
||||
async ({ pm, initialArgs, additionalArgs }, { expect }) => {
|
||||
const mockPkgJson = {
|
||||
devDependencies: {
|
||||
foo: "^1.0.0",
|
||||
bar: "^2.0.0",
|
||||
baz: "^1.2.3",
|
||||
},
|
||||
};
|
||||
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson));
|
||||
vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson);
|
||||
|
||||
const packages = ["foo", "bar@latest", "baz@1.2.3"];
|
||||
await installPackages(pm, packages, { dev: true });
|
||||
|
||||
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
|
||||
[...initialArgs, ...packages, ...(additionalArgs ?? [])],
|
||||
expect.anything()
|
||||
);
|
||||
|
||||
if (pm === "npm") {
|
||||
const writeFileCall = vi.mocked(writeFile).mock.calls[0];
|
||||
expect(writeFileCall[0]).toBe(resolve(process.cwd(), "package.json"));
|
||||
expect(JSON.parse(writeFileCall[1] as string)).toMatchObject({
|
||||
devDependencies: {
|
||||
foo: "^1.0.0",
|
||||
bar: "^2.0.0",
|
||||
baz: "1.2.3",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
test("installWrangler", async ({ expect }) => {
|
||||
const mockPkgJson = {
|
||||
devDependencies: {
|
||||
wrangler: "^4.0.0",
|
||||
},
|
||||
};
|
||||
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson));
|
||||
vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson);
|
||||
|
||||
await installWrangler("npm", false);
|
||||
|
||||
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
|
||||
["npm", "install", "--save-dev", "wrangler@latest"],
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
|
||||
test("installWrangler updates existing workers-types", async ({ expect }) => {
|
||||
const mockPkgJson = {
|
||||
devDependencies: {
|
||||
"@cloudflare/workers-types": "^4.20241011.0",
|
||||
wrangler: "^3.60.3",
|
||||
},
|
||||
};
|
||||
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockPkgJson));
|
||||
vi.mocked(parsePackageJSON).mockReturnValue(mockPkgJson);
|
||||
|
||||
await installWrangler("npm", false);
|
||||
|
||||
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
|
||||
[
|
||||
"npm",
|
||||
"install",
|
||||
"--save-dev",
|
||||
"wrangler@latest",
|
||||
"@cloudflare/workers-types@latest",
|
||||
],
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user