From f75b52cea237aab44b8801d6cde8caf136d2cf12 Mon Sep 17 00:00:00 2001 From: Exaxxion Date: Thu, 6 Jun 2024 02:39:20 -0700 Subject: [PATCH] Fix "v1.18.1" Version-related issues (#10) Revert "Fix tag format for publish workflow": - This reverts commit 28b26aaccdefb1e3aecb29337640daceb8bb75ff. - The preceding 'v' causes version detection to fail in addon mods. Will start using version tags without a preceding 'v' going forward. Fix DataFixers Version parsing: - Fixes #9 - "v1.18.1" erroneously parsed as 0.18.1 due to the preceding 'v'. This could potentially become a problem as Version could erroneously detect a pre-1.10.5 version, triggering DataFixers. - Pattern matching will now ignore a single 'v' before a numeric token. - Add JUnit test checking for desired behaviors with Version::parse - Also refactor GregTechVersion to parse RFG generated version tag instead of hard-coded values, as 1.18.1 was reporting itself as 1.18.0. --- .github/workflows/publish.yml | 4 +- src/main/java/gregtech/GregTechVersion.java | 10 +-- src/main/java/gregtech/api/util/Version.java | 2 +- .../java/gregtech/api/util/VersionTest.java | 67 +++++++++++++++++++ 4 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/test/java/gregtech/api/util/VersionTest.java diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 09318be99..c99d9c40c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,7 +4,7 @@ name: Publish on: push: tags: - - 'v[0-9]+.[0-9]+.[0-9]+' # any semver tag, e.g. 1.2.3 + - '[0-9]+.[0-9]+.[0-9]+' # any semver tag, e.g. 1.2.3 env: # link to the changelog with a format code for the version @@ -63,4 +63,4 @@ jobs: caches jdks notifications - wrapper + wrapper \ No newline at end of file diff --git a/src/main/java/gregtech/GregTechVersion.java b/src/main/java/gregtech/GregTechVersion.java index c8d092045..ffcea871a 100644 --- a/src/main/java/gregtech/GregTechVersion.java +++ b/src/main/java/gregtech/GregTechVersion.java @@ -4,15 +4,7 @@ public final class GregTechVersion { - public static final int MAJOR = 1; - //This number is incremented every major feature update - public static final int MINOR = 18; - //This number is incremented every time the feature is added, or bug is fixed. resets every major version change - public static final int REVISION = 0; - //This number is incremented every build, and never reset. Should always be 0 in the repo code. - public static final int BUILD = 0; - - public static final Version VERSION = new Version(MAJOR, MINOR, REVISION, BUILD); + public static final Version VERSION = Version.parse(GTInternalTags.VERSION); private GregTechVersion() { } diff --git a/src/main/java/gregtech/api/util/Version.java b/src/main/java/gregtech/api/util/Version.java index c00ec4a6e..1bd393b6e 100644 --- a/src/main/java/gregtech/api/util/Version.java +++ b/src/main/java/gregtech/api/util/Version.java @@ -22,7 +22,7 @@ public Version(int... nums) { } public static Version parse(String vStr) { - final Pattern p = Pattern.compile("(\\d+).*"); + final Pattern p = Pattern.compile("v?(\\d+).*"); return new Version(Arrays.stream(vStr.split(Pattern.quote("."))) .map(s -> { Matcher m = p.matcher(s); diff --git a/src/test/java/gregtech/api/util/VersionTest.java b/src/test/java/gregtech/api/util/VersionTest.java new file mode 100644 index 000000000..4451e8df2 --- /dev/null +++ b/src/test/java/gregtech/api/util/VersionTest.java @@ -0,0 +1,67 @@ +package gregtech.api.util; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class VersionTest { + + /** + * Workaround for Nomifactory 1.2.2.x worlds with Bansoukou-patched version string + * so that they are properly remapped on world load + */ + @Test + public void parse_omits_text_after_numbers() { + String version = "1.8.4.419-exa3"; + Version expected = new Version(1, 8, 4, 419); + assertEquals("Failed to omit non-numeric trailing text", expected, Version.parse(version)); + } + + /** + * As a contingency against crashing, map non-numeric tokens to zeroes + */ + @Test public void parse_maps_text_to_zero() { + String version = "lorem.ipsum.foo.bar"; + Version expected = new Version(0, 0, 0, 0); + assertEquals("Failed to map non-numeric text to zero", expected, Version.parse(version)); + } + + /** + * If there's a preceding "v" which started getting added by the build script + * when I switched to using git tags for version numbering, we need to ignore it + * and consider just the number. + */ + @Test + public void parse_ignores_v_before_number() { + // Bug fix confirmation - formerly detected '0.18.1' + String version = "v1.18.1"; + Version expected = new Version(1, 18, 1); + assertEquals("Failed to detect version string with single preceding 'v'", expected, Version.parse(version)); + } + + /** + * Make sure that dev builds properly parse the version string too. + */ + @Test + public void parse_handles_RFG_dirty_tags() { + // RFG-generated version strings look like this when you have uncommitted changes + String version = "v1.18.1.dirty"; + Version expected = new Version(1, 18, 1); + assertEquals("Failed to parse simple dirty tag", expected, Version.parse(version)); + + // RFG-generated version strings look like this when you have local commits plus uncommitted changes + version = "v1.18.1-2-abcdef12.dirty"; + assertEquals("Failed to parse complex dirty tag", expected, Version.parse(version)); + } + + /** + * Ensures that 'v' handling doesn't break other rules. + */ + @Test + public void parse_handles_other_v_conditions() { + // two 'v' counts as text, v followed by non-numbers is still text, single v is not ignored between digits + String version = "vv1.vfoo.1v8"; + Version expected = new Version(0, 0, 1); + assertEquals("Parsing failed to handle normal logic with 'v' related conditions", expected, Version.parse(version)); + } +} \ No newline at end of file