Skip to content

Commit

Permalink
Run coverage tests on v3 branch, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Feb 11, 2025
1 parent 2405007 commit cf90b43
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: ci
on:
push:
branches: [main, v2]
branches: [main, v3]
pull_request:
branches: [main, v2]
branches: [main, v3]
jobs:
test:
runs-on: ubuntu-latest
Expand Down
122 changes: 108 additions & 14 deletions packages/cli/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import inquirer from "inquirer";
import { existsSync } from "node:fs";
import { getServerPkgDir, makePathsAbsolute } from "../config.js";
import {
getServerPkgDir,
makePathsAbsolute,
getWorkerAndDatasetName,
readInitialServerConfig,
stageDeployConfig,
} from "../config.js";
import { join } from "node:path";

// Mock external dependencies
Expand Down Expand Up @@ -105,7 +111,7 @@ describe("CLI Functions", () => {
const input = {
path1: "relative/path/file.txt",
path2: "/absolute/path/file.txt",
nonPath: "string-without-slash"
nonPath: "string-without-slash",
};
const baseDir = "/base/dir";

Expand All @@ -114,7 +120,7 @@ describe("CLI Functions", () => {
expect(result).toEqual({
path1: "/base/dir/relative/path/file.txt",
path2: "/absolute/path/file.txt",
nonPath: "string-without-slash"
nonPath: "string-without-slash",
});
});

Expand All @@ -123,10 +129,10 @@ describe("CLI Functions", () => {
nested: {
path: "relative/nested/file.txt",
config: {
location: "another/path/config.json"
}
location: "another/path/config.json",
},
},
topLevel: "/absolute/top/level.txt"
topLevel: "/absolute/top/level.txt",
};
const baseDir = "/root";

Expand All @@ -136,10 +142,10 @@ describe("CLI Functions", () => {
nested: {
path: "/root/relative/nested/file.txt",
config: {
location: "/root/another/path/config.json"
}
location: "/root/another/path/config.json",
},
},
topLevel: "/absolute/top/level.txt"
topLevel: "/absolute/top/level.txt",
});
});

Expand All @@ -148,8 +154,8 @@ describe("CLI Functions", () => {
paths: [
"relative/path1.txt",
"/absolute/path2.txt",
{ nestedPath: "relative/path3.txt" }
]
{ nestedPath: "relative/path3.txt" },
],
};
const baseDir = "/base";

Expand All @@ -159,16 +165,104 @@ describe("CLI Functions", () => {
paths: [
"/base/relative/path1.txt",
"/absolute/path2.txt",
{ nestedPath: "/base/relative/path3.txt" }
]
{ nestedPath: "/base/relative/path3.txt" },
],
});
});

it("should handle non-object inputs", () => {
expect(makePathsAbsolute(null, "/base")).toBeNull();
expect(makePathsAbsolute(undefined, "/base")).toBeUndefined();
expect(makePathsAbsolute("simple/path", "/base")).toBe("simple/path");
expect(makePathsAbsolute("simple/path", "/base")).toBe(
"simple/path",
);
expect(makePathsAbsolute(42, "/base")).toBe(42);
});
});

describe("getWorkerAndDatasetName", () => {
it("should extract worker name and dataset from config", () => {
const config = {
name: "test-worker",
analytics_engine_datasets: [{ dataset: "test-dataset" }],
};

const result = getWorkerAndDatasetName(config);

expect(result).toEqual({
workerName: "test-worker",
analyticsDataset: "test-dataset",
});
});
});

describe("readInitialServerConfig", () => {
it("should read and parse wrangler.json from server package", async () => {
const mockConfig = {
name: "counterscale",
analytics_engine_datasets: [{ dataset: "default-dataset" }],
};

// Mock existsSync to make getServerPkgDir work
const { existsSync } = await import("node:fs");
vi.mocked(existsSync).mockReturnValue(true);

// Mock readFileSync
const { readFileSync } = await import("node:fs");
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockConfig));

const result = readInitialServerConfig();

expect(result).toEqual(mockConfig);
expect(readFileSync).toHaveBeenCalledWith(
expect.stringMatching(/wrangler\.json$/),
"utf8",
);
});
});

describe("stageDeployConfig", () => {
it("should write updated config with absolute paths", async () => {
// Mock existsSync to make getServerPkgDir work
const { existsSync, writeFileSync } = await import("node:fs");
vi.mocked(existsSync).mockReturnValue(true);
vi.mocked(writeFileSync);

const initialConfig = {
name: "old-name",
analytics_engine_datasets: [{ dataset: "old-dataset" }],
build: {
command: "npm run build",
cwd: "relative/path",
},
};

await stageDeployConfig(
"/target/wrangler.json",
initialConfig,
"new-worker",
"new-dataset",
);

// Verify writeFileSync was called with the correct arguments
expect(writeFileSync).toHaveBeenCalledWith(
"/target/wrangler.json",
expect.any(String),
);

// Parse the written config to verify its contents
const writtenConfig = JSON.parse(
vi.mocked(writeFileSync).mock.calls[0][1] as string,
);

// Verify worker name and dataset were updated
expect(writtenConfig.name).toBe("new-worker");
expect(writtenConfig.analytics_engine_datasets[0].dataset).toBe(
"new-dataset",
);

// Verify paths were made absolute
expect(writtenConfig.build.cwd).toMatch(/^\//); // Should start with /
});
});
});
16 changes: 9 additions & 7 deletions packages/cli/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path, { dirname } from "path";
import fs, { existsSync } from "node:fs";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { fileURLToPath } from "url";

Expand Down Expand Up @@ -110,7 +110,7 @@ export function getWorkerAndDatasetName(config: ReturnType<typeof JSON.parse>) {
export function readInitialServerConfig() {
const serverPkgDir = getServerPkgDir();
const distConfig = JSON.parse(
fs.readFileSync(path.join(serverPkgDir, "wrangler.json"), "utf8"),
readFileSync(path.join(serverPkgDir, "wrangler.json"), "utf8"),
);

return distConfig;
Expand All @@ -121,7 +121,6 @@ export function readInitialServerConfig() {
* converted to be absolute. This makes it so that the `wrangler deploy` command can be
* run from any directory.
*/

export async function stageDeployConfig(
targetPath: string,
initialDeployConfig: ReturnType<typeof JSON.parse>,
Expand All @@ -130,9 +129,12 @@ export async function stageDeployConfig(
): Promise<void> {
const serverPkgDir = getServerPkgDir();

const updatedConfig = makePathsAbsolute(initialDeployConfig, serverPkgDir);
initialDeployConfig.name = workerName;
initialDeployConfig.analytics_engine_datasets[0].dataset = analyticsDataset;
const outDeployConfig = makePathsAbsolute(
initialDeployConfig,
serverPkgDir,
);
outDeployConfig.name = workerName;
outDeployConfig.analytics_engine_datasets[0].dataset = analyticsDataset;

fs.writeFileSync(targetPath, JSON.stringify(updatedConfig, null, 2));
writeFileSync(targetPath, JSON.stringify(outDeployConfig, null, 2));
}
15 changes: 10 additions & 5 deletions packages/cli/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { defineConfig } from 'vitest/config';
import { defineConfig, coverageConfigDefaults } from "vitest/config";

export default defineConfig({
test: {
globals: true,
environment: 'node',
environment: "node",
coverage: {
provider: 'v8',
reporter: ['text', 'html'],
exclude: ['**/node_modules/**', '**/dist/**', '**/__tests__/**'],
provider: "v8",
reporter: ["text", "html"],
exclude: [
"**/node_modules/**",
"**/dist/**",
"**/__tests__/**",
...coverageConfigDefaults.exclude,
],
},
},
});

0 comments on commit cf90b43

Please sign in to comment.