Skip to content

Commit

Permalink
check minimum build version when requesting full compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Aug 19, 2024
1 parent bbf873b commit 1a337cc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
41 changes: 30 additions & 11 deletions src/select-zls-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,14 +390,12 @@ async function selectOnDevelopmentBuild(
}

const oldestRelease = JSON.parse(releases[0].JsonData) as D2JsonData;
const oldestMinRuntimeZigVersion = SemanticVersion.parse(
oldestRelease.minimumRuntimeZigVersion,
const oldestMinZigVersion = selectMinimumZigVersion(
oldestRelease,
compatibility,
);
assert(oldestMinRuntimeZigVersion !== null);

if (
SemanticVersion.order(zigVersion, oldestMinRuntimeZigVersion) == Order.lt
) {
if (SemanticVersion.order(zigVersion, oldestMinZigVersion) == Order.lt) {
return SelectVersionFailureCode.Unsupported;
}

Expand All @@ -409,13 +407,11 @@ async function selectOnDevelopmentBuild(

for (const entry of releases) {
const data = JSON.parse(entry.JsonData) as D2JsonData;
const minimumRuntimeZigVersion = SemanticVersion.parse(
data.minimumRuntimeZigVersion,
);
assert(minimumRuntimeZigVersion !== null);
assert(data.artifacts.length !== 0);

switch (SemanticVersion.order(zigVersion, minimumRuntimeZigVersion)) {
const minimumZigVersion = selectMinimumZigVersion(data, compatibility);

switch (SemanticVersion.order(zigVersion, minimumZigVersion)) {
case Order.lt:
// the minimum build version may not be monotonically increasing (i.e a
// newer release has lower minimum build version) so keep searching
Expand Down Expand Up @@ -470,3 +466,26 @@ async function selectOnDevelopmentBuild(

return selectedEntry;
}

function selectMinimumZigVersion(
data: D2JsonData,
compatibility: Exclude<VersionCompatibility, VersionCompatibility.None>,
): SemanticVersion {
const minBuildZigVersion = SemanticVersion.parse(data.minimumBuildZigVersion);
assert(minBuildZigVersion !== null);

const minRuntimeZigVersion = SemanticVersion.parse(
data.minimumRuntimeZigVersion,
);
assert(minRuntimeZigVersion !== null);

switch (compatibility) {
case VersionCompatibility.Full:
return SemanticVersion.order(minBuildZigVersion, minRuntimeZigVersion) ==
Order.lt
? minRuntimeZigVersion
: minBuildZigVersion;
case VersionCompatibility.OnlyRuntime:
return minRuntimeZigVersion;
}
}
53 changes: 33 additions & 20 deletions test/select-zls-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,17 @@ describe("/v1/zls/select-version", () => {
});
});

test.each<[string, keyof typeof VersionCompatibility, string | null]>([
test.each<[string, "Both" | "Full" | "OnlyRuntime", string | null]>([
["0.0.0", "Full", null],
["0.0.0-dev.1+aaaaaaaaa", "Full", null],
["0.7.0-dev.5+aaaaaaaaa", "Full", null],
["0.9.0-dev.10+aaaaaaaaa", "Full", null],
["0.9.0-dev.15+aaaaaaaaa", "Full", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.20+aaaaaaaaa", "Full", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.25+aaaaaaaaa", "Full", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.27+aaaaaaaaa", "OnlyRuntime", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.27+aaaaaaaaa", "Full", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.15+aaaaaaaaa", "OnlyRuntime", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.15+aaaaaaaaa", "Full", null],
["0.9.0-dev.20+aaaaaaaaa", "OnlyRuntime", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.20+aaaaaaaaa", "Full", null],
["0.9.0-dev.25+aaaaaaaaa", "Both", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.27+aaaaaaaaa", "Both", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.30+aaaaaaaaa", "OnlyRuntime", "0.9.0-dev.3+aaaaaaaaa"],
["0.9.0-dev.30+aaaaaaaaa", "Full", null],
["0.9.0-dev.35+aaaaaaaaa", "OnlyRuntime", "0.9.0-dev.3+aaaaaaaaa"],
Expand Down Expand Up @@ -478,20 +479,32 @@ describe("/v1/zls/select-version", () => {
])(
"Zig %s, %s -> ZLS %s",
async (zigVersion, compatibility, expectedZLSVersion) => {
const response = await selectZLSVersion(
zigVersion,
VersionCompatibility[compatibility],
);
if (expectedZLSVersion === null) {
expect(response).not.toHaveProperty("version");
expect(response).toHaveProperty("code");
expect(response).toHaveProperty("message");
} else {
expect(response).not.toHaveProperty("code");
assert(!("code" in response));
expect(response).not.toHaveProperty("message");
assert(!("message" in response));
expect(response.version).toBe<string>(expectedZLSVersion);
const cases: VersionCompatibility[] = [];
switch (compatibility) {
case "Full":
cases.push(VersionCompatibility.Full);
break;
case "OnlyRuntime":
cases.push(VersionCompatibility.OnlyRuntime);
break;
case "Both":
cases.push(
VersionCompatibility.OnlyRuntime,
VersionCompatibility.Full,
);
break;
}
for (const compat of cases) {
const response = await selectZLSVersion(zigVersion, compat);
if (expectedZLSVersion === null) {
expect(response).not.toHaveProperty("version");
expect(response).toHaveProperty("code");
expect(response).toHaveProperty("message");
} else {
expect(response).toHaveProperty("version", expectedZLSVersion);
expect(response).not.toHaveProperty("code");
expect(response).not.toHaveProperty("message");
}
}
},
);
Expand Down

0 comments on commit 1a337cc

Please sign in to comment.