Skip to content

Commit

Permalink
Restructure text fixtures directories, add test-utils.ts and add msw dep
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximo-Guk committed Nov 18, 2024
1 parent c7edd46 commit d96ce63
Show file tree
Hide file tree
Showing 56 changed files with 695 additions and 174 deletions.
596 changes: 596 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/semver": "^7.5.8",
"@vercel/ncc": "^0.38.2",
"mock-fs": "^5.4.1",
"msw": "^2.6.4",
"prettier": "^3.3.3",
"semver": "^7.6.3",
"typescript": "^5.6.3",
Expand Down
78 changes: 0 additions & 78 deletions src/github.ts

This file was deleted.

20 changes: 10 additions & 10 deletions src/packageManagers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getPackageManager } from "./packageManagers";

describe("getPackageManager", () => {
test("should use provided value instead of inferring from lockfile", () => {
expect(getPackageManager("npm", { workingDirectory: "test/npm" }))
expect(getPackageManager("npm", { workingDirectory: "src/test/fixtures/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "npx",
Expand All @@ -12,7 +12,7 @@ describe("getPackageManager", () => {
}
`);

expect(getPackageManager("yarn", { workingDirectory: "test/npm" }))
expect(getPackageManager("yarn", { workingDirectory: "src/test/fixtures/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "yarn",
Expand All @@ -21,7 +21,7 @@ describe("getPackageManager", () => {
}
`);

expect(getPackageManager("pnpm", { workingDirectory: "test/npm" }))
expect(getPackageManager("pnpm", { workingDirectory: "src/test/fixtures/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "pnpm exec",
Expand All @@ -30,7 +30,7 @@ describe("getPackageManager", () => {
}
`);

expect(getPackageManager("bun", { workingDirectory: "test/bun" }))
expect(getPackageManager("bun", { workingDirectory: "src/test/fixtures/bun" }))
.toMatchInlineSnapshot(`
{
"exec": "bunx",
Expand All @@ -41,7 +41,7 @@ describe("getPackageManager", () => {
});

test("should use npm if no value provided and package-lock.json exists", () => {
expect(getPackageManager("", { workingDirectory: "test/npm" }))
expect(getPackageManager("", { workingDirectory: "src/test/fixtures/npm" }))
.toMatchInlineSnapshot(`
{
"exec": "npx",
Expand All @@ -52,7 +52,7 @@ describe("getPackageManager", () => {
});

test("should use yarn if no value provided and yarn.lock exists", () => {
expect(getPackageManager("", { workingDirectory: "test/yarn" }))
expect(getPackageManager("", { workingDirectory: "src/test/fixtures/yarn" }))
.toMatchInlineSnapshot(`
{
"exec": "yarn",
Expand All @@ -63,7 +63,7 @@ describe("getPackageManager", () => {
});

test("should use pnpm if no value provided and pnpm-lock.yaml exists", () => {
expect(getPackageManager("", { workingDirectory: "test/pnpm" }))
expect(getPackageManager("", { workingDirectory: "src/test/fixtures/pnpm" }))
.toMatchInlineSnapshot(`
{
"exec": "pnpm exec",
Expand All @@ -74,7 +74,7 @@ describe("getPackageManager", () => {
});

test("should use bun if no value provided and bun.lockb exists", () => {
expect(getPackageManager("", { workingDirectory: "test/bun" }))
expect(getPackageManager("", { workingDirectory: "src/test/fixtures/bun" }))
.toMatchInlineSnapshot(`
{
"exec": "bunx",
Expand All @@ -85,7 +85,7 @@ describe("getPackageManager", () => {
});

test("should use npm if no value provided and no lockfile is present", () => {
expect(getPackageManager("", { workingDirectory: "test/empty" }))
expect(getPackageManager("", { workingDirectory: "src/test/fixtures/empty" }))
.toMatchInlineSnapshot(`
{
"exec": "npx",
Expand All @@ -97,7 +97,7 @@ describe("getPackageManager", () => {

test("should throw if an invalid value is provided", () => {
expect(() =>
getPackageManager("cargo", { workingDirectory: "test/npm" }),
getPackageManager("cargo", { workingDirectory: "src/test/fixtures/npm" }),
).toThrowError();
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions src/test/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { http, HttpResponse } from "msw";

export function mockGithubDeployments({
githubUser,
githubRepoName,
}: {
githubUser: string;
githubRepoName: string;
}) {
return {
handlers: [
http.post(
`https://api.github.com/repos/${githubUser}/${githubRepoName}/deployments`,
async ({ request }) => {
if (request.headers.get("Authorization") === null) {
return HttpResponse.text("error: no auth token", { status: 400 });
}

return HttpResponse.json(null);
},
),
],
};
}
26 changes: 26 additions & 0 deletions src/test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { WranglerActionConfig } from "../wranglerAction";

export function getTestConfig({
config = {},
}: {
config?: Partial<WranglerActionConfig>;
} = {}): WranglerActionConfig {
return Object.assign(
{
WRANGLER_VERSION: "3.81.0",
didUserProvideWranglerVersion: false,
secrets: [],
workingDirectory: "/src/test/fixtures",
CLOUDFLARE_API_TOKEN: "foo",
CLOUDFLARE_ACCOUNT_ID: "bar",
ENVIRONMENT: "dev",
VARS: [],
COMMANDS: [],
QUIET_MODE: false,
PACKAGE_MANAGER: "npm",
WRANGLER_OUTPUT_DIR: "/tmp/wranglerArtifacts",
GITHUB_TOKEN: "xxxxyy23213123132131",
} as const satisfies WranglerActionConfig,
config,
);
}
117 changes: 35 additions & 82 deletions src/wranglerAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "@actions/core";
import * as exec from "@actions/exec";
import { describe, expect, it, vi } from "vitest";
import { installWrangler } from "./wranglerAction";
import { getTestConfig } from "./test/test-utils";

describe("installWrangler", () => {
const testPackageManager = {
Expand All @@ -11,20 +12,7 @@ describe("installWrangler", () => {
};

it("Errors on unsupported wrangler version", async () => {
const testConfig = {
WRANGLER_VERSION: "1",
didUserProvideWranglerVersion: false,
secrets: [],
workingDirectory: "/test",
CLOUDFLARE_API_TOKEN: "foo",
CLOUDFLARE_ACCOUNT_ID: "bar",
ENVIRONMENT: "dev",
VARS: [],
COMMANDS: [],
QUIET_MODE: false,
PACKAGE_MANAGER: "npm",
WRANGLER_OUTPUT_DIR: "/tmp/wranglerArtifacts",
};
const testConfig = getTestConfig({ config: { WRANGLER_VERSION: "1" } });
await expect(
installWrangler(testConfig, testPackageManager),
).rejects.toThrowError(
Expand All @@ -33,29 +21,14 @@ describe("installWrangler", () => {
});

it("Does nothing if no wrangler version is specified and wrangler is already installed", async () => {
const testConfig = {
WRANGLER_VERSION: "3.81.0",
didUserProvideWranglerVersion: false,
secrets: [],
workingDirectory: "/test",
CLOUDFLARE_API_TOKEN: "foo",
CLOUDFLARE_ACCOUNT_ID: "bar",
ENVIRONMENT: "dev",
VARS: [],
COMMANDS: [],
QUIET_MODE: false,
PACKAGE_MANAGER: "npm",
WRANGLER_OUTPUT_DIR: "/tmp/wranglerArtifacts",
};
vi.spyOn(exec, "getExecOutput").mockImplementation(
async (commandLine: string, args?: string[]) => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.48.0 (update available 3.53.1)`,
};
},
);
const testConfig = getTestConfig();
vi.spyOn(exec, "getExecOutput").mockImplementation(async () => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.48.0 (update available 3.53.1)`,
};
});
const infoSpy = vi.spyOn(core, "info");
await installWrangler(testConfig, testPackageManager);
expect(infoSpy).toBeCalledWith(
Expand All @@ -64,58 +37,38 @@ describe("installWrangler", () => {
});

it("Does nothing if the wrangler version specified is the same as the one installed", async () => {
const testConfig = {
WRANGLER_VERSION: "3.48.0",
didUserProvideWranglerVersion: true,
secrets: [],
workingDirectory: "/test",
CLOUDFLARE_API_TOKEN: "foo",
CLOUDFLARE_ACCOUNT_ID: "bar",
ENVIRONMENT: "dev",
VARS: [],
COMMANDS: [],
QUIET_MODE: false,
PACKAGE_MANAGER: "npm",
WRANGLER_OUTPUT_DIR: "/tmp/wranglerArtifacts",
};
vi.spyOn(exec, "getExecOutput").mockImplementation(
async (commandLine: string, args?: string[]) => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.48.0 (update available 3.53.1)`,
};
const testConfig = getTestConfig({
config: {
WRANGLER_VERSION: "3.48.0",
didUserProvideWranglerVersion: true,
},
);
});
vi.spyOn(exec, "getExecOutput").mockImplementation(async () => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.48.0 (update available 3.53.1)`,
};
});
const infoSpy = vi.spyOn(core, "info");
await installWrangler(testConfig, testPackageManager);
expect(infoSpy).toBeCalledWith("✅ Using Wrangler 3.48.0");
});
it("Should install wrangler if the version specified is not already available", async () => {
const testConfig = {
WRANGLER_VERSION: "3.48.0",
didUserProvideWranglerVersion: true,
secrets: [],
workingDirectory: "/test",
CLOUDFLARE_API_TOKEN: "foo",
CLOUDFLARE_ACCOUNT_ID: "bar",
ENVIRONMENT: "dev",
VARS: [],
COMMANDS: [],
QUIET_MODE: false,
PACKAGE_MANAGER: "npm",
WRANGLER_OUTPUT_DIR: "/tmp/wranglerArtifacts",
};
vi.spyOn(exec, "getExecOutput").mockImplementation(
async (commandLine: string, args?: string[]) => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.20.0 (update available 3.53.1)`,
};
const testConfig = getTestConfig({
config: {
WRANGLER_VERSION: "3.48.0",
didUserProvideWranglerVersion: true,
},
);
vi.spyOn(exec, "exec").mockImplementation(async (commandLine: string) => {
});
vi.spyOn(exec, "getExecOutput").mockImplementation(async () => {
return {
exitCode: 0,
stderr: "",
stdout: ` ⛅️ wrangler 3.20.0 (update available 3.53.1)`,
};
});
vi.spyOn(exec, "exec").mockImplementation(async () => {
return 0;
});
const infoSpy = vi.spyOn(core, "info");
Expand Down
5 changes: 2 additions & 3 deletions src/wranglerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { exec, execShell } from "./exec";
import { PackageManager } from "./packageManagers";
import { semverCompare } from "./utils";
import { getDetailedPagesDeployOutput } from "./wranglerArtifactManager";
import { createGitHubDeployment, createJobSummary } from "./github";
import { createGitHubDeployment, createJobSummary } from "./service/github";
import { getOctokit } from "@actions/github";

export type WranglerActionConfig = z.infer<typeof wranglerActionConfig>;
Expand Down Expand Up @@ -404,8 +404,7 @@ async function wranglerCommands(

// Check if this command is a workers deployment
if (command.startsWith("deploy") || command.startsWith("publish")) {
const { deploymentUrl } =
extractDeploymentUrlsFromStdout(stdOut);
const { deploymentUrl } = extractDeploymentUrlsFromStdout(stdOut);
setOutput("deployment-url", deploymentUrl);
}
// Check if this command is a pages deployment
Expand Down
Loading

0 comments on commit d96ce63

Please sign in to comment.