Skip to content

Commit

Permalink
fix cache find returned value
Browse files Browse the repository at this point in the history
  • Loading branch information
ueokande committed May 3, 2024
1 parent d158452 commit 8394dd3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
35 changes: 24 additions & 11 deletions __test__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});
});

Expand All @@ -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",
),
);
});
});
});
Expand Down
16 changes: 8 additions & 8 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8394dd3

Please sign in to comment.