Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
304 changes: 152 additions & 152 deletions dist/index.mjs

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from "vite-plus/test";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

// The shipped action is the bundled dist/index.mjs. Several bundled deps
// (notably @actions/cache) branch on `error.name === SomeError.name` — e.g.
// ReserveCacheError, which decides whether a benign cache reserve race is
// logged via core.info or core.warning. If the bundler mangles the class
// binding, `SomeError.name` becomes a mangled identifier and never equals the
// instance's preserved `this.name` literal, so benign errors are wrongly
// surfaced as warning annotations in CI.
//
// pack.minify in vite.config.ts is configured with mangle.keepNames so these
// classes stay named. This test guards against a regression to plain
// `minify: true`, which would emit anonymous `class extends Error` bindings.
const distPath = fileURLToPath(new URL("../dist/index.mjs", import.meta.url));
const dist = readFileSync(distPath, "utf8");

describe("bundled dist preserves @actions/cache error class names", () => {
for (const className of ["ReserveCacheError", "ValidationError", "FinalizeCacheError"]) {
it(`keeps the ${className} class name so error.name comparisons work`, () => {
// A named class declaration or named class expression keeps Class.name.
// Minification without keepNames would drop the name to a mangled binding.
const namedClass = new RegExp(`class ${className} extends Error`);
expect(dist).toMatch(namedClass);
});
}
});
80 changes: 80 additions & 0 deletions src/cache-save.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, it, expect, beforeEach, vi } from "vite-plus/test";

// Mock external shells before importing the SUT so the module's in-file
// references resolve to the mocked versions.
vi.mock("@actions/cache", () => ({
saveCache: vi.fn(),
}));
vi.mock("@actions/core", () => ({
getState: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
}));

import { saveCache as saveCacheAction } from "@actions/cache";
import { getState, info, warning } from "@actions/core";
import { saveCache } from "./cache-save.js";
import { State } from "./types.js";

const mockedSaveCacheAction = vi.mocked(saveCacheAction);
const mockedGetState = vi.mocked(getState);
const mockedInfo = vi.mocked(info);
const mockedWarning = vi.mocked(warning);

// Drive getState to return values for the save path:
// a primary key, distinct matched key (so we don't short-circuit on a hit),
// and non-empty cache paths.
function stubSaveState(): void {
mockedGetState.mockImplementation((name: string) => {
switch (name) {
case State.CachePrimaryKey:
return "vite-plus-Linux-x64-pnpm-abc123";
case State.CacheMatchedKey:
return "";
case State.CachePaths:
return JSON.stringify(["/home/runner/.cache/pnpm"]);
default:
return "";
}
});
}

describe("saveCache", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("does not warn when the cache key was already reserved by a concurrent matrix job", async () => {
// In a build matrix, jobs that share a cache key (e.g. same OS/arch/lockfile
// across Node versions) race to save it. The losers get cacheId === -1 from
// @actions/cache. That is expected and benign, not warning-worthy.
stubSaveState();
mockedSaveCacheAction.mockResolvedValue(-1);

await saveCache();

expect(mockedWarning).not.toHaveBeenCalled();
expect(mockedInfo).toHaveBeenCalled();
});

it("logs a success message when the cache is saved", async () => {
stubSaveState();
mockedSaveCacheAction.mockResolvedValue(42);

await saveCache();

expect(mockedWarning).not.toHaveBeenCalled();
expect(mockedInfo).toHaveBeenCalledWith(
expect.stringContaining("vite-plus-Linux-x64-pnpm-abc123"),
);
});

it("warns when saveCache throws an unexpected error", async () => {
stubSaveState();
mockedSaveCacheAction.mockRejectedValue(new Error("boom"));

await saveCache();

expect(mockedWarning).toHaveBeenCalledWith(expect.stringContaining("boom"));
});
});
2 changes: 1 addition & 1 deletion src/cache-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function saveCache(): Promise<void> {
try {
const cacheId = await saveCacheAction(cachePaths, primaryKey);
if (cacheId === -1) {
warning("Cache save failed or was skipped.");
info("Cache not saved (key already reserved by a concurrent job, or save was skipped).");
return;
}
info(`Cache saved with key: ${primaryKey}`);
Expand Down
11 changes: 10 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ export default defineConfig({
onlyBundle: false,
},
clean: true,
minify: true,
// Keep class/function names during minification. Bundled deps such as
// @actions/cache branch on `error.name === SomeError.name` (e.g.
// ReserveCacheError). Plain `minify: true` mangles the class binding, so
// `SomeError.name` becomes the mangled identifier and never matches the
// instance's preserved `this.name` literal — routing benign errors (like a
// cache reserve race in a build matrix) to core.warning instead of core.info.
minify: {
compress: true,
mangle: { keepNames: { function: true, class: true } },
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

},
},
lint: {
ignorePatterns: ["dist/**/*"],
Expand Down
Loading