diff --git a/__tests__/cache/save-cache.test.ts b/__tests__/cache/save-cache.test.ts new file mode 100644 index 0000000..b276c7b --- /dev/null +++ b/__tests__/cache/save-cache.test.ts @@ -0,0 +1,87 @@ +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + afterAll, + beforeEach, + describe, + expect, + it, + jest, +} from "@jest/globals"; +import { CacheLocalSource, type SetupInputs } from "../../src/utils/inputs"; +import { createSetupInputs } from "../helpers/setup-inputs"; + +const mockSaveCache = jest.fn<() => Promise>(); +const mockInfo = jest.fn(); +const mockLoadInputs = jest.fn<() => SetupInputs>(() => + createSetupInputs({ enableCache: false }), +); +const state: Record = {}; + +jest.unstable_mockModule("@actions/cache", () => ({ + saveCache: mockSaveCache, +})); + +jest.unstable_mockModule("@actions/core", () => ({ + getState: jest.fn((name: string) => state[name] ?? ""), + setFailed: jest.fn(), +})); + +jest.unstable_mockModule("../../src/utils/inputs", () => ({ + loadInputs: mockLoadInputs, +})); + +jest.unstable_mockModule("../../src/utils/logging", () => ({ + info: mockInfo, + warning: jest.fn(), +})); + +jest.spyOn(process, "exit").mockImplementation((() => undefined) as never); + +const { run } = await import("../../src/save-cache"); + +const cacheDir = mkdtempSync(join(tmpdir(), "setup-uv-save-cache-")); +const cacheKey = "setup-uv-2-x86_64-unknown-linux-gnu-ubuntu-24.04-3.12-hash"; + +function infoMessages(): string[] { + return mockInfo.mock.calls.map(([msg]) => msg as string); +} + +beforeEach(() => { + delete process.env.UV_CACHE_DIR; + state["cache-key"] = cacheKey; + state["cache-matched-key"] = ""; + mockLoadInputs.mockReturnValue( + createSetupInputs({ + cacheLocalPath: { path: cacheDir, source: CacheLocalSource.Input }, + }), + ); +}); + +afterAll(() => { + rmSync(cacheDir, { force: true, recursive: true }); +}); + +describe("saveCache", () => { + it("reports the saved key when the cache is saved", async () => { + mockSaveCache.mockResolvedValue(42); + + await run(); + + expect(mockSaveCache).toHaveBeenCalledWith([cacheDir], cacheKey); + expect(infoMessages()).toContain(`uv cache saved with key: ${cacheKey}`); + }); + + it("does not report the cache as saved when saving fails", async () => { + // @actions/cache logs the failure itself and returns -1 + mockSaveCache.mockResolvedValue(-1); + + await run(); + + expect(mockSaveCache).toHaveBeenCalledWith([cacheDir], cacheKey); + expect(infoMessages()).not.toContain( + `uv cache saved with key: ${cacheKey}`, + ); + }); +}); diff --git a/dist/save-cache/index.cjs b/dist/save-cache/index.cjs index 49a5299..1d25968 100644 --- a/dist/save-cache/index.cjs +++ b/dist/save-cache/index.cjs @@ -66671,8 +66671,10 @@ async function saveCacheToKey(cacheKey, cachePath, stateKey, cacheName) { return; } info2(`Including ${cacheName} path: ${cachePath}`); - await saveCache2([cachePath], cacheKey); - info2(`${cacheName} saved with key: ${cacheKey}`); + const cacheId = await saveCache2([cachePath], cacheKey); + if (cacheId !== -1) { + info2(`${cacheName} saved with key: ${cacheKey}`); + } } __name(saveCacheToKey, "saveCacheToKey"); run(); diff --git a/src/save-cache.ts b/src/save-cache.ts index 80b15d6..bc75adb 100644 --- a/src/save-cache.ts +++ b/src/save-cache.ts @@ -159,8 +159,10 @@ async function saveCacheToKey( } log.info(`Including ${cacheName} path: ${cachePath}`); - await cache.saveCache([cachePath], cacheKey); - log.info(`${cacheName} saved with key: ${cacheKey}`); + const cacheId = await cache.saveCache([cachePath], cacheKey); + if (cacheId !== -1) { + log.info(`${cacheName} saved with key: ${cacheKey}`); + } } run();