Skip to content

Commit

Permalink
polish and test update
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed May 2, 2024
1 parent 50fe596 commit 2a54d08
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/splitclient-rb/engine/matchers/in_list_semver_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ def initialize(attribute, list_value, logger, validator)
end

def match?(args)
return false unless verify_semver_arg?(args, 'InListSemverMatcher')
return false if @semver_list.empty? || !verify_semver_arg?(args, 'InListSemverMatcher')

value_to_match = SplitIoClient::Semver.build(args[:attributes][@attribute.to_sym], @logger)
if value_to_match.nil?
@logger.error('whitelistMatcherData is required for IN_LIST_SEMVER matcher type')
return false

end
matches = (@semver_list.map { |item| item.version == value_to_match.version }).any? { |item| item == true }
@logger.debug("[InListSemverMatcher] #{value_to_match} matches -> #{matches}")
Expand Down
2 changes: 1 addition & 1 deletion lib/splitclient-rb/engine/matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def verify_semver_arg?(args, matcher_name)
end

def check_semver_value_to_match(value_to_match, matcher_spec_name)
unless !value_to_match.nil? && !@semver.nil?
if value_to_match.nil? || @semver.nil?
@logger.error("stringMatcherData is required for #{matcher_spec_name} matcher type")
return false

Expand Down
16 changes: 14 additions & 2 deletions lib/splitclient-rb/engine/matchers/semver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def compare(to_compare)
private

def integer?(value)
value.to_i.to_s == value
!!value.match(/^(\d)+$/)
end

#
Expand Down Expand Up @@ -107,10 +107,21 @@ def assign_major_minor_and_patch(version)
@minor = parts[1].to_i
@patch = parts[2].to_i
@version = "#{@major}#{VALUE_DELIMITER}#{@minor}#{VALUE_DELIMITER}#{@patch}"
@version += "#{PRE_RELEASE_DELIMITER}#{@pre_release.join('.')}" unless @pre_release.empty?
@version += parse_pre_release
@version += "#{METADATA_DELIMITER}#{@metadata}" unless @metadata.empty?
end

def parse_pre_release
return '' if @pre_release.empty?

pre_parsed = []
@pre_release.each do |pre_digit|
pre_digit = pre_digit.to_i if integer?(pre_digit)
pre_parsed << pre_digit
end
"#{PRE_RELEASE_DELIMITER}#{pre_parsed.join('.')}"
end

#
# Compare 2 variables and return int as follows:
# 0: if var1 == var2
Expand Down Expand Up @@ -169,6 +180,7 @@ def compare_pre_release(to_compare)

if integer?(@pre_release[i]) && integer?(to_compare.pre_release[i])
return compare_vars(@pre_release[i].to_i, to_compare.pre_release[i].to_i)

end

return compare_vars(@pre_release[i], to_compare.pre_release[i])
Expand Down
7 changes: 7 additions & 0 deletions spec/engine/matchers/semver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
expect(described_class.build(version[0], logger)).to eq(nil)
end
end

it 'verify leading-zero integers are converted' do
semver = described_class.build('1.01.2', logger)
expect(semver.version).to eq('1.1.2')
expect(described_class.build('1.01.2-rc.04', logger).version).to eq('1.1.2-rc.4')
end

end

context 'compare versions' do
Expand Down

0 comments on commit 2a54d08

Please sign in to comment.