diff --git a/__test__/cache.test.ts b/__test__/cache.test.ts index 603a4ec..dbb2902 100644 --- a/__test__/cache.test.ts +++ b/__test__/cache.test.ts @@ -49,16 +49,21 @@ describe("find", () => { } }); - test("finds a tool in the cache", async () => { - expect(await find("chrome", "100.0.1.0", "x64")).toBe("100.0.1.0"); - expect(await find("chrome", "100.1", "x64")).toBe("100.1.1.0"); - expect(await find("chrome", "100", "x64")).toBe("100.2.0.0"); - expect(await find("chrome", "latest", "x64")).toBe("latest"); - expect(await find("chrome", "canary", "x64")).toBe("canary"); - expect(await find("chrome", "123456", "x64")).toBe("123456"); - expect(await find("chrome", "300000", "x64")).toBeUndefined(); - expect(await find("chrome", "200", "x64")).toBeUndefined(); - expect(await find("chrome", "stable", "x64")).toBeUndefined(); + test.each` + version | arch | subdir + ${"100.0.1.0"} | ${"x64"} | ${"100.0.1.0/x64"} + ${"100.1"} | ${"x64"} | ${"100.1.1.0/x64"} + ${"100"} | ${"x64"} | ${"100.2.0.0/x64"} + ${"latest"} | ${"x64"} | ${"latest/x64"} + ${"canary"} | ${"x64"} | ${"canary/x64"} + ${"123456"} | ${"x64"} | ${"123456/x64"} + ${"300000"} | ${"arm64"} | ${"300000/arm64"} + ${"200"} | ${"x64"} | ${undefined} + ${"stable"} | ${"x64"} | ${undefined} + `("finds a tool in the cache", async ({ version, arch, subdir }) => { + expect(await find("chrome", version, arch)).toBe( + subdir && path.join(tempToolCacheDir, "setup-chrome", "chrome", subdir), + ); }); }); @@ -83,7 +88,15 @@ describe("find", () => { }); test("corrupted cache is ignored", async () => { - expect(await find("chrome", "100", "x64")).toBe("100.1.0.0"); + expect(await find("chrome", "100", "x64")).toBe( + path.join( + tempToolCacheDir, + "setup-chrome", + "chrome", + "100.1.0.0", + "x64", + ), + ); }); }); }); diff --git a/src/cache.ts b/src/cache.ts index 2fdd407..b18879f 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -65,28 +65,28 @@ export const find = async ( } const versions = await fs.promises.readdir(toolPath); - let version: string | undefined; + let cachePath: string | undefined; for (const v of versions) { if (!spec.satisfies(v) || spec.lt(v)) { continue; } - const cachePath = path.join(toolPath, v, arch); - const markerPath = `${cachePath}.complete`; - if (!fs.existsSync(cachePath) || !fs.existsSync(markerPath)) { + const p = path.join(toolPath, v, arch); + const markerPath = `${p}.complete`; + if (!fs.existsSync(p) || !fs.existsSync(markerPath)) { continue; } - version = v; + cachePath = p; } - if (version) { - core.debug(`Found tool in cache ${toolName} ${versionSpec} ${arch}`); + if (cachePath) { + core.debug(`Found tool in cache ${cachePath}`); } else { core.debug( `Unable to find tool in cache ${toolName} ${versionSpec} ${arch}`, ); } - return version; + return cachePath; }; async function _createToolPath(