Skip to content

Commit

Permalink
Update handling of mixed version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jun 9, 2024
1 parent f17899f commit 8b4f00b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 22 additions & 7 deletions lib/reissue/version_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -78,7 +92,8 @@ def update(segment)
# Regular expression pattern for matching the version string.
#
# @return [Regexp] The version regex pattern.
def version_regex = /(?<major>[a-zA-Z\d]+)\.(?<minor>[a-zA-Z\d]+)\.(?<patch>[a-zA-Z\d]+)/
VERSION_MATCH = /(?<major>\d+)\.(?<minor>[a-zA-Z\d]+)\.(?<patch>[a-zA-Z\d]+)(?<add>\.(?<pre>[a-zA-Z\d]+))?/
def version_regex = VERSION_MATCH

# Writes the updated version to the specified file.
#
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/mixed_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Reissue
module MixedGem
VERSION = "2.35.number19"
end
end
5 changes: 5 additions & 0 deletions test/fixtures/prerelease_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Reissue
module PrereleaseGem
VERSION = "3.2.1.rc1"
end
end
20 changes: 20 additions & 0 deletions test/test_version_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8b4f00b

Please sign in to comment.