From a4118a0cfd66c5fa4092818a132d5022bffb4788 Mon Sep 17 00:00:00 2001 From: AlexIIL Date: Tue, 23 Apr 2024 23:29:25 +0100 Subject: [PATCH] Fix "2.x" not appending an empty build version to the resulting predicate. (#425) This used to turn "2.x" or "=2.x" into "^2-", however a while ago this accidently got changed to "^2". Honestly, I'm unsure which way is correct. However this was changed accidently during our Version.Sementic switching to non-null build & pre-release, so it's been changed back. --- .../version/VersionComparisonOperator.java | 4 ++-- .../java/org/quiltmc/loader/api/Version.java | 4 ++-- .../util/version/VersionPredicateParser.java | 2 +- .../fabricmc/test/VersionPredicateTests.java | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/test/java/net/fabricmc/test/VersionPredicateTests.java diff --git a/src/fabric/api/java/net/fabricmc/loader/api/metadata/version/VersionComparisonOperator.java b/src/fabric/api/java/net/fabricmc/loader/api/metadata/version/VersionComparisonOperator.java index 914ae222a..d29e01c85 100644 --- a/src/fabric/api/java/net/fabricmc/loader/api/metadata/version/VersionComparisonOperator.java +++ b/src/fabric/api/java/net/fabricmc/loader/api/metadata/version/VersionComparisonOperator.java @@ -102,7 +102,7 @@ public SemanticVersion minVersion(SemanticVersion version) { public SemanticVersion maxVersion(SemanticVersion version) { int[] components = { version.getVersionComponent(0), version.getVersionComponent(1) + 1 }; try { - return Quilt2FabricSemanticVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(components, "", "")); + return Quilt2FabricSemanticVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(components, "", null)); } catch (VersionFormatException e) { throw new IllegalStateException(e); } @@ -124,7 +124,7 @@ public SemanticVersion minVersion(SemanticVersion version) { public SemanticVersion maxVersion(SemanticVersion version) { int[] components = { version.getVersionComponent(0) + 1 }; try { - return Quilt2FabricSemanticVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(components, "", "")); + return Quilt2FabricSemanticVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(components, "", null)); } catch (VersionFormatException e) { throw new IllegalStateException(e); } diff --git a/src/main/java/org/quiltmc/loader/api/Version.java b/src/main/java/org/quiltmc/loader/api/Version.java index fe469bbbb..1388115fc 100644 --- a/src/main/java/org/quiltmc/loader/api/Version.java +++ b/src/main/java/org/quiltmc/loader/api/Version.java @@ -100,11 +100,11 @@ static Semantic of(int[] components, @Nullable String preRelease, @Nullable Stri StringBuilder raw = new StringBuilder(); raw.append(Arrays.stream(components).mapToObj(Integer::toString).collect(Collectors.joining("."))); // 1.0.0 - if (preRelease != null && !preRelease.isEmpty()) { + if (preRelease != null) { raw.append('-').append(preRelease); } - if (buildMetadata != null && !buildMetadata.isEmpty()) { + if (buildMetadata != null) { raw.append('+').append(buildMetadata); } // HACK: re-building raw makes sure that everything is verified to be valid diff --git a/src/main/java/org/quiltmc/loader/impl/fabric/util/version/VersionPredicateParser.java b/src/main/java/org/quiltmc/loader/impl/fabric/util/version/VersionPredicateParser.java index 7e502711b..50ce8f8a9 100644 --- a/src/main/java/org/quiltmc/loader/impl/fabric/util/version/VersionPredicateParser.java +++ b/src/main/java/org/quiltmc/loader/impl/fabric/util/version/VersionPredicateParser.java @@ -90,7 +90,7 @@ public static VersionPredicate parse(String predicate) throws VersionParsingExce } try { - version = Quilt2FabricVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(newComponents, "", semVer.getBuildKey().orElse(""))); + version = Quilt2FabricVersion.toFabric(org.quiltmc.loader.api.Version.Semantic.of(newComponents, "", semVer.getBuildKey().orElse(null))); } catch (VersionFormatException e) { throw new IllegalStateException("Failed to reconstruct a version from " + version, e); } diff --git a/src/test/java/net/fabricmc/test/VersionPredicateTests.java b/src/test/java/net/fabricmc/test/VersionPredicateTests.java new file mode 100644 index 000000000..cb5716a22 --- /dev/null +++ b/src/test/java/net/fabricmc/test/VersionPredicateTests.java @@ -0,0 +1,17 @@ +package net.fabricmc.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import net.fabricmc.loader.api.Version; +import net.fabricmc.loader.api.VersionParsingException; +import net.fabricmc.loader.api.metadata.version.VersionPredicate; + +public class VersionPredicateTests { + @Test + public void testPredicates() throws VersionParsingException { + VersionPredicate predicate = VersionPredicate.parse("2.x.x"); + Version version = Version.parse("2.0.0-beta3"); + Assertions.assertTrue(predicate.test(version), predicate + " does not allow " + version); + } +}