mirror of
https://github.com/astral-sh/setup-uv.git
synced 2026-12-04 02:16:06 +00:00
Select the manifest source from the action inputs and keep version resolution separate from archive downloads. Fetch the built-in manifest directly from GitHub when the Astral mirror is disabled. Fixes: #1140 Based on the work of @Rudra-G-23 in https://github.com/astral-sh/setup-uv/pull/1152 Co-authored-by: Amp <amp@ampcode.com>
393 lines
13 KiB
TypeScript
393 lines
13 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
import * as semver from "semver";
|
|
import type { ManifestSource } from "../../src/utils/inputs";
|
|
|
|
const customManifest: ManifestSource = {
|
|
custom: true,
|
|
newestFirst: false,
|
|
url: "https://example.com/custom.ndjson",
|
|
};
|
|
|
|
const mockInfo = jest.fn();
|
|
const mockWarning = jest.fn();
|
|
|
|
jest.unstable_mockModule("@actions/core", () => ({
|
|
debug: jest.fn(),
|
|
info: mockInfo,
|
|
warning: mockWarning,
|
|
}));
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockDownloadTool = jest.fn<any>();
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockExtractTar = jest.fn<any>();
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockExtractZip = jest.fn<any>();
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockCacheDir = jest.fn<any>();
|
|
|
|
jest.unstable_mockModule("@actions/tool-cache", () => ({
|
|
cacheDir: mockCacheDir,
|
|
downloadTool: mockDownloadTool,
|
|
evaluateVersions: (versions: string[], range: string) =>
|
|
semver.maxSatisfying(versions, range) ?? "",
|
|
extractTar: mockExtractTar,
|
|
extractZip: mockExtractZip,
|
|
find: () => "",
|
|
findAllVersions: () => [],
|
|
isExplicitVersion: (version: string) => semver.valid(version) !== null,
|
|
}));
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockGetLatestVersion = jest.fn<any>();
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockGetAllVersions = jest.fn<any>();
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockGetFirstMatchingVersion = jest.fn<any>();
|
|
|
|
jest.unstable_mockModule("../../src/download/manifest", () => ({
|
|
getAllVersions: mockGetAllVersions,
|
|
getFirstMatchingVersion: mockGetFirstMatchingVersion,
|
|
getFirstVersion: mockGetLatestVersion,
|
|
}));
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
|
const mockValidateChecksum = jest.fn<any>();
|
|
|
|
jest.unstable_mockModule("../../src/download/checksum/checksum", () => ({
|
|
validateChecksum: mockValidateChecksum,
|
|
}));
|
|
|
|
const mockGetLatestKnownVersion = jest.fn(() => "0.9.25");
|
|
|
|
jest.unstable_mockModule("../../src/download/checksum/known-version", () => ({
|
|
getLatestKnownVersion: mockGetLatestKnownVersion,
|
|
}));
|
|
|
|
const { downloadVersion, rewriteToMirror } = await import(
|
|
"../../src/download/download-version"
|
|
);
|
|
const { resolveVersion } = await import("../../src/version/resolve");
|
|
const { ASTRAL_MANIFEST } = await import("../../src/utils/inputs");
|
|
|
|
describe("download-version", () => {
|
|
beforeEach(() => {
|
|
mockInfo.mockReset();
|
|
mockWarning.mockReset();
|
|
mockDownloadTool.mockReset();
|
|
mockExtractTar.mockReset();
|
|
mockExtractZip.mockReset();
|
|
mockCacheDir.mockReset();
|
|
mockGetLatestVersion.mockReset();
|
|
mockGetAllVersions.mockReset();
|
|
mockGetFirstMatchingVersion.mockReset();
|
|
mockValidateChecksum.mockReset();
|
|
mockGetLatestKnownVersion.mockClear();
|
|
|
|
mockDownloadTool.mockResolvedValue("/tmp/downloaded");
|
|
mockExtractTar.mockResolvedValue("/tmp/extracted");
|
|
mockExtractZip.mockResolvedValue("/tmp/extracted");
|
|
mockCacheDir.mockResolvedValue("/tmp/cached");
|
|
});
|
|
|
|
describe("resolveVersion", () => {
|
|
it("uses the default manifest to resolve latest", async () => {
|
|
mockGetLatestVersion.mockResolvedValue("0.9.26");
|
|
|
|
const version = await resolveVersion("latest", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.9.26");
|
|
expect(mockGetLatestVersion).toHaveBeenCalledTimes(1);
|
|
expect(mockGetLatestVersion).toHaveBeenCalledWith(ASTRAL_MANIFEST);
|
|
});
|
|
|
|
it("resolves latest-known without reading the manifest", async () => {
|
|
const version = await resolveVersion("latest-known", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.9.25");
|
|
expect(mockGetLatestKnownVersion).toHaveBeenCalledTimes(1);
|
|
expect(mockGetLatestVersion).not.toHaveBeenCalled();
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
expect(mockGetFirstMatchingVersion).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stops at the first matching version in the default manifest", async () => {
|
|
mockGetFirstMatchingVersion.mockImplementation(
|
|
(predicate: (version: string) => boolean) =>
|
|
["0.9.26", "0.9.25"].find(predicate),
|
|
);
|
|
|
|
const version = await resolveVersion("^0.9.0", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.9.26");
|
|
expect(mockGetFirstMatchingVersion).toHaveBeenCalledTimes(1);
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses the ordered search for the direct GitHub manifest", async () => {
|
|
mockGetFirstMatchingVersion.mockImplementation(
|
|
(predicate: (version: string) => boolean) =>
|
|
["0.9.26", "0.9.25"].find(predicate),
|
|
);
|
|
const source: ManifestSource = {
|
|
custom: false,
|
|
newestFirst: true,
|
|
url: "https://raw.githubusercontent.com/astral-sh/versions/main/v1/uv.ndjson",
|
|
};
|
|
|
|
const version = await resolveVersion("^0.9.0", source);
|
|
|
|
expect(version).toBe("0.9.26");
|
|
expect(mockGetFirstMatchingVersion).toHaveBeenCalledWith(
|
|
expect.any(Function),
|
|
source,
|
|
);
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("streams PEP 440 ranges from the default manifest", async () => {
|
|
mockGetFirstMatchingVersion.mockImplementation(
|
|
(predicate: (version: string) => boolean) =>
|
|
["0.9.26", "0.9.25"].find(predicate),
|
|
);
|
|
|
|
const version = await resolveVersion("!=0.9.26", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.9.25");
|
|
expect(mockGetFirstMatchingVersion).toHaveBeenCalledTimes(1);
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("resolves ~= with PEP 440 compatible release semantics", async () => {
|
|
mockGetFirstMatchingVersion.mockImplementation(
|
|
(predicate: (version: string) => boolean) =>
|
|
["1.0.0", "0.10.2", "0.9.26"].find(predicate),
|
|
);
|
|
|
|
const version = await resolveVersion("~=0.9", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.10.2");
|
|
});
|
|
|
|
it("resolves ~= with PEP 440 compatible release semantics from manifest-file", async () => {
|
|
mockGetAllVersions.mockResolvedValue(["0.9.26", "1.0.0", "0.10.2"]);
|
|
|
|
const version = await resolveVersion("~=0.9", customManifest);
|
|
|
|
expect(version).toBe("0.10.2");
|
|
});
|
|
|
|
it.each([
|
|
["<=0.9", "0.9.0"],
|
|
["==0.9", "0.9.0"],
|
|
])("resolves %s with PEP 440 semantics", async (specifier, expected) => {
|
|
mockGetFirstMatchingVersion.mockImplementation(
|
|
(predicate: (version: string) => boolean) =>
|
|
["0.10.2", "0.9.26", "0.9.0", "0.8.24"].find(predicate),
|
|
);
|
|
|
|
const version = await resolveVersion(specifier, ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe(expected);
|
|
});
|
|
|
|
it("resolves >0.9 with PEP 440 semantics for the lowest strategy", async () => {
|
|
mockGetAllVersions.mockResolvedValue([
|
|
"0.10.0",
|
|
"0.9.26",
|
|
"0.9.1",
|
|
"0.9.0",
|
|
]);
|
|
|
|
const version = await resolveVersion(">0.9", ASTRAL_MANIFEST, "lowest");
|
|
|
|
expect(version).toBe("0.9.1");
|
|
});
|
|
|
|
it("treats == exact pins as explicit versions", async () => {
|
|
const version = await resolveVersion("==0.9.26", ASTRAL_MANIFEST);
|
|
|
|
expect(version).toBe("0.9.26");
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
expect(mockGetLatestVersion).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses latest for minimum-only ranges when using the highest strategy", async () => {
|
|
mockGetLatestVersion.mockResolvedValue("0.9.26");
|
|
|
|
const version = await resolveVersion(
|
|
">=0.9.0",
|
|
ASTRAL_MANIFEST,
|
|
"highest",
|
|
);
|
|
|
|
expect(version).toBe("0.9.26");
|
|
expect(mockGetLatestVersion).toHaveBeenCalledTimes(1);
|
|
expect(mockGetLatestVersion).toHaveBeenCalledWith(ASTRAL_MANIFEST);
|
|
expect(mockGetAllVersions).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses the lowest compatible version when requested", async () => {
|
|
mockGetAllVersions.mockResolvedValue(["0.9.26", "0.9.25"]);
|
|
|
|
const version = await resolveVersion("^0.9.0", ASTRAL_MANIFEST, "lowest");
|
|
|
|
expect(version).toBe("0.9.25");
|
|
expect(mockGetAllVersions).toHaveBeenCalledTimes(1);
|
|
expect(mockGetAllVersions).toHaveBeenCalledWith(ASTRAL_MANIFEST);
|
|
});
|
|
|
|
it.each(["latest", ">=0.9.0"])(
|
|
"uses the first custom manifest record for %s",
|
|
async (specifier) => {
|
|
mockGetLatestVersion.mockResolvedValue("0.9.7");
|
|
mockGetAllVersions.mockResolvedValue(["0.9.7", "0.9.26"]);
|
|
|
|
await expect(resolveVersion(specifier, customManifest)).resolves.toBe(
|
|
"0.9.7",
|
|
);
|
|
},
|
|
);
|
|
|
|
it("rejects the first custom manifest record if it does not meet the minimum", async () => {
|
|
mockGetLatestVersion.mockResolvedValue("0.9.7");
|
|
mockGetAllVersions.mockResolvedValue(["0.9.7", "0.9.26"]);
|
|
|
|
await expect(resolveVersion(">=0.9.8", customManifest)).rejects.toThrow(
|
|
"No version found for >=0.9.8",
|
|
);
|
|
});
|
|
|
|
it("resolves a range from an unordered custom manifest", async () => {
|
|
mockGetAllVersions.mockResolvedValue(["0.9.7", "0.9.26", "0.9.25"]);
|
|
|
|
await expect(resolveVersion("^0.9.0", customManifest)).resolves.toBe(
|
|
"0.9.26",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("downloadVersion", () => {
|
|
const githubUrl =
|
|
"https://github.com/astral-sh/uv/releases/download/0.9.26/uv-x86_64-unknown-linux-gnu.tar.gz";
|
|
const mirrorUrl =
|
|
"https://releases.astral.sh/github/uv/releases/download/0.9.26/uv-x86_64-unknown-linux-gnu.tar.gz";
|
|
const customUrl = "https://example.com/uv.tar.gz";
|
|
|
|
function install(downloadUrl: string, checksum?: string, useMirror = true) {
|
|
return downloadVersion(
|
|
"unknown-linux-gnu",
|
|
"x86_64",
|
|
"0.9.26",
|
|
{ checksum: "manifest-checksum", downloadUrl },
|
|
checksum,
|
|
"token",
|
|
useMirror,
|
|
);
|
|
}
|
|
|
|
it("passes the explicit and fallback checksums to verification", async () => {
|
|
await install(customUrl, "user-checksum");
|
|
|
|
expect(mockValidateChecksum).toHaveBeenCalledWith(
|
|
"user-checksum",
|
|
"/tmp/downloaded",
|
|
"x86_64",
|
|
"unknown-linux-gnu",
|
|
"0.9.26",
|
|
"manifest-checksum",
|
|
);
|
|
});
|
|
|
|
it("rewrites GitHub Releases URLs to the Astral mirror", async () => {
|
|
await install(githubUrl);
|
|
|
|
expect(mockDownloadTool).toHaveBeenCalledWith(
|
|
mirrorUrl,
|
|
undefined,
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
it.each([customUrl, "https://github.com.evil.test/uv.tar.gz"])(
|
|
"does not send the token to %s",
|
|
async (url) => {
|
|
await install(url);
|
|
expect(mockDownloadTool).toHaveBeenCalledWith(
|
|
url,
|
|
undefined,
|
|
undefined,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("falls back to GitHub Releases when the mirror fails", async () => {
|
|
mockDownloadTool
|
|
.mockRejectedValueOnce(new Error("mirror unavailable"))
|
|
.mockResolvedValueOnce("/tmp/downloaded");
|
|
|
|
await install(githubUrl);
|
|
|
|
expect(mockDownloadTool).toHaveBeenCalledTimes(2);
|
|
expect(mockDownloadTool).toHaveBeenNthCalledWith(
|
|
1,
|
|
mirrorUrl,
|
|
undefined,
|
|
undefined,
|
|
);
|
|
expect(mockDownloadTool).toHaveBeenNthCalledWith(
|
|
2,
|
|
githubUrl,
|
|
undefined,
|
|
"token",
|
|
);
|
|
expect(mockWarning).toHaveBeenCalledWith(
|
|
"Failed to download from mirror, falling back to GitHub Releases: mirror unavailable",
|
|
);
|
|
});
|
|
|
|
it("does not fall back for non-GitHub URLs", async () => {
|
|
mockDownloadTool.mockRejectedValue(new Error("download failed"));
|
|
|
|
await expect(install(customUrl)).rejects.toThrow("download failed");
|
|
|
|
expect(mockDownloadTool).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("skips the Astral mirror when downloadFromAstralMirror is false", async () => {
|
|
await install(githubUrl, undefined, false);
|
|
|
|
expect(mockDownloadTool).toHaveBeenCalledWith(
|
|
githubUrl,
|
|
undefined,
|
|
"token",
|
|
);
|
|
expect(mockDownloadTool).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe("rewriteToMirror", () => {
|
|
it("rewrites a GitHub Releases URL to the Astral mirror", () => {
|
|
expect(
|
|
rewriteToMirror(
|
|
"https://github.com/astral-sh/uv/releases/download/0.9.26/uv-x86_64-unknown-linux-gnu.tar.gz",
|
|
),
|
|
).toBe(
|
|
"https://releases.astral.sh/github/uv/releases/download/0.9.26/uv-x86_64-unknown-linux-gnu.tar.gz",
|
|
);
|
|
});
|
|
|
|
it("returns undefined for non-GitHub URLs", () => {
|
|
expect(rewriteToMirror("https://example.com/uv.tar.gz")).toBeUndefined();
|
|
});
|
|
|
|
it("returns undefined for a different GitHub repo", () => {
|
|
expect(
|
|
rewriteToMirror(
|
|
"https://github.com/other/repo/releases/download/v1.0/file.tar.gz",
|
|
),
|
|
).toBeUndefined();
|
|
});
|
|
});
|
|
});
|