From 8b4f00b1b127817f6935d2cbf46feb81e9bbb0fc Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Sun, 9 Jun 2024 08:56:22 -0400 Subject: [PATCH] Update handling of mixed version numbers --- CHANGELOG.md | 2 ++ lib/reissue/version_updater.rb | 29 ++++++++++++++++++++++------- test/fixtures/mixed_version.rb | 5 +++++ test/fixtures/prerelease_version.rb | 5 +++++ test/test_version_updater.rb | 20 ++++++++++++++++++++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/mixed_version.rb create mode 100644 test/fixtures/prerelease_version.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index f232153..2283ffd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Reissue.finalize returns the version and value as an array +- Documentation on the refined redo method in Gem::Version +- Limit major numbers to Integers ## [0.1.2] - 2024-06-08 diff --git a/lib/reissue/version_updater.rb b/lib/reissue/version_updater.rb index a5a6f68..cc8ca1f 100644 --- a/lib/reissue/version_updater.rb +++ b/lib/reissue/version_updater.rb @@ -10,14 +10,28 @@ module Versioning # Redoes the version based on the specified segment_name. # # @param segment_name [Symbol] The segment_name to redo the version. - # Possible values are :major, :minor, or any other symbol. + # Possible values are :major, :minor, :patch, or :pre. # @return [Gem::Version] The updated version. def redo(segment_name) - ::Gem::Version.create({ - major: [segments[0].next, 0, 0], - minor: [segments[0], segments[1].next, 0], - patch: [segments[0], segments[1], segments[2].next] - }.fetch(segment_name.to_sym).join(".")) + new_segments = case segment_name.to_sym + when :major + [segments[0].next, 0, 0] + when :minor + [segments[0], segments[1].next, 0] + when :patch + segments.slice(0, 2) + segments.slice(2..-1).then { |array| + array[-1] = array[-1].next + [array.map(&:to_s).join] + } + when :pre + segments.slice(0, 3) + segments.slice(3..-1).then { |array| + array[-1] = array[-1].next + [array.map(&:to_s).join] + } + else + raise ArgumentError, "Invalid segment name: #{segment_name}" + end + ::Gem::Version.create(new_segments.join(".")) end end @@ -78,7 +92,8 @@ def update(segment) # Regular expression pattern for matching the version string. # # @return [Regexp] The version regex pattern. - def version_regex = /(?[a-zA-Z\d]+)\.(?[a-zA-Z\d]+)\.(?[a-zA-Z\d]+)/ + VERSION_MATCH = /(?\d+)\.(?[a-zA-Z\d]+)\.(?[a-zA-Z\d]+)(?\.(?
[a-zA-Z\d]+))?/
+    def version_regex = VERSION_MATCH
 
     # Writes the updated version to the specified file.
     #
diff --git a/test/fixtures/mixed_version.rb b/test/fixtures/mixed_version.rb
new file mode 100644
index 0000000..67024ed
--- /dev/null
+++ b/test/fixtures/mixed_version.rb
@@ -0,0 +1,5 @@
+module Reissue
+  module MixedGem
+    VERSION = "2.35.number19"
+  end
+end
diff --git a/test/fixtures/prerelease_version.rb b/test/fixtures/prerelease_version.rb
new file mode 100644
index 0000000..1ea0402
--- /dev/null
+++ b/test/fixtures/prerelease_version.rb
@@ -0,0 +1,5 @@
+module Reissue
+  module PrereleaseGem
+    VERSION = "3.2.1.rc1"
+  end
+end
diff --git a/test/test_version_updater.rb b/test/test_version_updater.rb
index 1970f4f..4db370f 100644
--- a/test/test_version_updater.rb
+++ b/test/test_version_updater.rb
@@ -52,5 +52,25 @@ class TestVersionUpdater < Minitest::Spec
       contents = @version_updater.update("patch")
       assert_equal "2.32.gamma", contents
     end
+
+    it "works with pre-release versions" do
+      @file = File.expand_path("fixtures/prerelease_version.rb", __dir__)
+      @version_updater = Reissue::VersionUpdater.new(@file)
+      contents = @version_updater.update("pre")
+      assert_equal "3.2.1.rc2", contents
+    end
+
+    it "respects mixed version strings for patches" do
+      @file = File.expand_path("fixtures/mixed_version.rb", __dir__)
+      @version_updater = Reissue::VersionUpdater.new(@file)
+      contents = @version_updater.update("patch")
+      assert_equal "2.35.number20", contents
+    end
+
+    it "raises an error for an invalid segment" do
+      assert_raises(ArgumentError) do
+        @version_updater.update("invalid")
+      end
+    end
   end
 end