Skip to content

Commit

Permalink
feat: add testing with vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Nov 24, 2024
1 parent 707b4f1 commit 3aa727b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
- name: "Run Biome"
run: biome ci

build:
name: "Build"
test:
name: "Build & Test"
runs-on: ubuntu-latest
steps:
- name: "Checkout the repository"
Expand All @@ -40,7 +40,10 @@ jobs:
run: pnpm install

- name: "Run Build"
run: pnpm run build
run: pnpm build

- name: "Run Tests"
run: pnpm test

test-action:
name: "Test Action"
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ lerna-debug.log*
.eslintcache
.turbo
.build_cache
temp
.tmp/
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"dev": "pnpm build --watch",
"build": "tsup src/index.ts --treeshake --minify --clean",
"typecheck": "tsc",
"test": "vitest run",
"test:watch": "vitest",
"lint": "biome lint",
"lint:fix": "biome lint -w",
"format": "biome format --write",
Expand Down
75 changes: 75 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as fs from "node:fs/promises";
import path from "node:path";
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
test,
vi,
} from "vitest";
import { main } from "./main";

const RUNNER_TEMP = path.join(process.cwd(), ".tmp");
const RUNNER_TOOL_CACHE = path.join(RUNNER_TEMP, "./tool-cache");

const verifyBinary = async (binPath: string | undefined, binary: string) => {
if (!binPath) throw new Error("Bin path not found");

const extension = process.platform === "win32" ? ".exe" : "";
const filePath = path.join(binPath, binary + extension);
expect(filePath.startsWith(RUNNER_TOOL_CACHE)).toBe(true);

// check if binary exists
await fs.lstat(filePath);

// try executing the binary
await exec(filePath, ["--version"]);
};

describe("action", () => {
beforeAll(async () => {
await fs.mkdir(RUNNER_TEMP, { recursive: true });
await fs.rm(RUNNER_TEMP, { recursive: true });
});

beforeEach(() => {
// set default values
vi.stubEnv("INPUT_CACHE", "true");
vi.stubEnv("INPUT_VERSION", "latest");
vi.stubEnv("INPUT_VERSION_REGEX", "(?<version>[\\d.]+)");
vi.stubEnv("RUNNER_TOOL_CACHE", RUNNER_TOOL_CACHE);
vi.stubEnv("RUNNER_TEMP", RUNNER_TEMP);
});

afterEach(() => {
vi.restoreAllMocks();
});

test("install infisical-cli", async () => {
vi.stubEnv("INPUT_PRESET", "infisical-cli");
const spy = vi.spyOn(core, "addPath");
await main();
expect(spy).toHaveBeenCalled();
await verifyBinary(spy.mock.lastCall?.[0], "infisical");
}, 10000);

test("install cloud-sql-proxy", async () => {
vi.stubEnv("INPUT_PRESET", "cloud-sql-proxy");
const spy = vi.spyOn(core, "addPath");
await main();
expect(spy).toHaveBeenCalled();
await verifyBinary(spy.mock.lastCall?.[0], "cloud-sql-proxy");
}, 10000);

test("install github-cli", async () => {
vi.stubEnv("INPUT_PRESET", "github-cli");
const spy = vi.spyOn(core, "addPath");
await main();
expect(spy).toHaveBeenCalled();
await verifyBinary(spy.mock.lastCall?.[0], "gh");
}, 10000);
});
File renamed without changes.

0 comments on commit 3aa727b

Please sign in to comment.