From 1a337cc7634ea2dc7d52bb096dc7a386879c9524 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Mon, 19 Aug 2024 20:25:30 +0200 Subject: [PATCH] check minimum build version when requesting full compatibility --- src/select-zls-version.ts | 41 ++++++++++++++++++------- test/select-zls-version.test.ts | 53 ++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/select-zls-version.ts b/src/select-zls-version.ts index 37799a2..a377ec2 100644 --- a/src/select-zls-version.ts +++ b/src/select-zls-version.ts @@ -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; } @@ -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 @@ -470,3 +466,26 @@ async function selectOnDevelopmentBuild( return selectedEntry; } + +function selectMinimumZigVersion( + data: D2JsonData, + compatibility: Exclude, +): 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; + } +} diff --git a/test/select-zls-version.test.ts b/test/select-zls-version.test.ts index c0155c1..62a8bc6 100644 --- a/test/select-zls-version.test.ts +++ b/test/select-zls-version.test.ts @@ -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"], @@ -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(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"); + } } }, );