Skip to content

Commit

Permalink
Merge pull request #139 from sensu/fix_semvar_normalizer
Browse files Browse the repository at this point in the history
Fix semvar normalizer
  • Loading branch information
pzupan authored Mar 21, 2019
2 parents 3971321 + 900061f commit 0c36da2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 7 additions & 3 deletions lib/semver_normalizer.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
# For example, "v3.4-20181225" gets normalized to "3.4.20181225".
# We have to normalize because +Semverse::Version+ requires version tags to be of the form "x.y.z".

# ("1.2.3-rc.1+build.1") => #<Version: @major=1, @minor=2, @patch=3, @pre_release='rc.1', @build='build.1'>

class SemverNormalizer
def self.call(tag)
return tag unless tag

tag
.to_s
.strip
.sub(/\Av/i, '') # strip off any leading 'v'
.gsub(/[-_]/, '.') # convert hyphens and underscores to dots
.strip # strip off any leading or trailing /t or /n or spaces
.sub(/\Av/i, '') # strip off any leading 'v'
.strip # strip any spaces after removing "v"
# stripping hypends and underscores substantially changes the version string
#.gsub(/[-_]/, '.') # convert hyphens and underscores to dots
end
end
11 changes: 6 additions & 5 deletions spec/lib/semver_normalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@

it 'strips off any leading "v" character' do
expect(subject.call("v790")).to eq '790'
expect(subject.call("V097")).to eq '097'
expect(subject.call("V097.1.0")).to eq '097.1.0'
expect(subject.call("V 097.1.0")).to eq '097.1.0'
end

it 'strips any leading and trailing whitespace' do
expect(subject.call("\t\nv790 ")).to eq '790'
expect(subject.call("\t\nv790.1.0 ")).to eq '790.1.0'
end

it 'replaces pseudo dots with dots' do
expect(subject.call("v7-0_9.0")).to eq '7.0.9.0'
expect(subject.call("V0.9-7_1")).to eq '0.9.7.1'
it 'does not replace underscore or hypen' do
expect(subject.call("v7.1.0-pre")).to eq '7.1.0-pre'
expect(subject.call("V0.9.1-7_1")).to eq '0.9.1-7_1'
end
end
end

0 comments on commit 0c36da2

Please sign in to comment.