Skip to content

Commit 63dbb09

Browse files
committed
(PUP-11077) Pass SemVer prerelease and build as an array
Passing SemVer prerelease or build components as a list of arguments or a hash added dots between each character due to confusion between Strings vs Arrays. For example, SemVer(1, 0, 0, "rc0") resulted in prerelease "r.c.0". This isn't an issue when passing the entire version as a string, eg "1.0.0-rc0". Automatically wrapping the prerelease or build components in an array isn't a breaking change, because it wasn't possible to do it previously. That's because the function signature is defined to accept a `SemVerQualifier` which `Array` is not: $ bx puppet apply -e 'notice(SemVer(1, 2, 3, ["rc4"], ["5"]))' Error: Evaluation Error: Error while evaluating a Function Call... rejected: parameter 'prerelease' expects a match for SemVerQualifier
1 parent 2fde3e6 commit 63dbb09

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/puppet/pops/types/p_sem_ver_type.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,22 @@ def from_string(str)
9595
end
9696

9797
def from_args(major, minor, patch, prerelease = nil, build = nil)
98-
SemanticPuppet::Version.new(major, minor, patch, prerelease, build)
98+
SemanticPuppet::Version.new(major, minor, patch, to_array(prerelease), to_array(build))
9999
end
100100

101101
def from_hash(hash)
102-
SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], hash['prerelease'], hash['build'])
102+
SemanticPuppet::Version.new(hash['major'], hash['minor'], hash['patch'], to_array(hash['prerelease']), to_array(hash['build']))
103103
end
104104

105105
def on_error(str)
106106
_("The string '%{str}' cannot be converted to a SemVer") % { str: str }
107107
end
108+
109+
private
110+
111+
def to_array(component)
112+
component ? [component] : nil
113+
end
108114
end
109115
end
110116

spec/unit/pops/types/p_sem_ver_type_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ module Types
125125
expect(eval_and_collect_notices(code)).to eql(['true', 'false'])
126126
end
127127

128+
it 'can be compared to another instance created from arguments' do
129+
code = <<-CODE
130+
$x = SemVer('1.2.3-rc4+5')
131+
$y = SemVer(1, 2, 3, 'rc4', '5')
132+
notice($x == $y)
133+
CODE
134+
expect(eval_and_collect_notices(code)).to eql(['true'])
135+
end
136+
137+
it 'can be compared to another instance created from a hash' do
138+
code = <<-CODE
139+
$x = SemVer('1.2.3-rc4+5')
140+
$y = SemVer(major => 1, minor => 2, patch => 3, prerelease => 'rc4', build => '5')
141+
notice($x == $y)
142+
CODE
143+
expect(eval_and_collect_notices(code)).to eql(['true'])
144+
end
145+
128146
it 'can be compared to another instance for magnitude' do
129147
code = <<-CODE
130148
$x = SemVer('1.1.1')

0 commit comments

Comments
 (0)