Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b5bfccb

Browse files
author
Daniel Mikusa
authoredOct 18, 2021
Version matcher now prefers a specific match over a partial match (#907)
Previous to this commit, if you have two versions say `3.1.1` and `3.1.1_BETA` the version matcher would incorrectly select `3.1.1_BETA`. It would behave as if a wildcard was present even though it's not present. This commit ensures that the same scenario will select version `3.1.1`, which is an exact match for the version requested. This may result in a difference of behavior for some users. If you are impacted by this, you need to switch your requested version to `3.1.1_+`. Using the scenario above, this will return `3.1.1_BETA`. Signed-off-by: Daniel Mikusa <dmikusa@vmware.com>
1 parent ba24874 commit b5bfccb

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed
 

‎lib/java_buildpack/repository/version_resolver.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,25 @@ def safe_candidate_version(candidate_version)
7676
end
7777

7878
def matches?(tokenized_candidate_version, tokenized_version)
79+
wildcard_matched = false
7980
(0..3).all? do |i|
80-
tokenized_candidate_version[i].nil? || as_regex(tokenized_candidate_version[i]) =~ tokenized_version[i]
81-
end
82-
end
81+
next true if wildcard_matched || tokenized_candidate_version[i].nil? && tokenized_version[i].nil?
8382

84-
def as_regex(version)
85-
/^#{version.gsub(JavaBuildpack::Util::TokenizedVersion::WILDCARD, '.*')}/
86-
end
83+
next false if tokenized_candidate_version[i].nil? && !tokenized_version[i].nil?
8784

88-
end
85+
if tokenized_candidate_version[i] == JavaBuildpack::Util::TokenizedVersion::WILDCARD
86+
wildcard_matched = true
87+
next true
88+
end
8989

90-
end
90+
if tokenized_candidate_version[i].end_with?(JavaBuildpack::Util::TokenizedVersion::WILDCARD)
91+
next !tokenized_version[i].nil? && tokenized_version[i].start_with?(tokenized_candidate_version[i][0..-2])
92+
end
9193

94+
tokenized_candidate_version[i] == tokenized_version[i]
95+
end
96+
end
97+
end
98+
end
9299
end
93100
end

‎spec/java_buildpack/repository/version_resolver_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
expect(described_class.resolve(tokenized_version('2.0.+'), versions)).to eq(tokenized_version('2.0.0'))
7070
end
7171

72+
it 'picks an exact match over a partial match' do
73+
versions = %w[3.1.1 3.1.1_BETA 3.1.1_BETA.2 3.1.2 3.2.0]
74+
expect(described_class.resolve(tokenized_version('3.1.1'), versions)).to eq(tokenized_version('3.1.1'))
75+
expect(described_class.resolve(tokenized_version('3.1.1_BETA'), versions)).to eq(tokenized_version('3.1.1_BETA'))
76+
end
77+
78+
it 'picks the latest including qualifiers' do
79+
versions = %w[3.1.1 3.1.1_BETA 3.1.1_BETA.2 3.1.2 3.2.0]
80+
expect(described_class.resolve(tokenized_version('3.1.1_+'), versions)).to eq(tokenized_version('3.1.1_BETA.2'))
81+
expect(described_class.resolve(tokenized_version('3.1.1_BE+'), versions)).to eq(tokenized_version('3.1.1_BETA.2'))
82+
end
83+
7284
def tokenized_version(s)
7385
JavaBuildpack::Util::TokenizedVersion.new(s)
7486
end

0 commit comments

Comments
 (0)
Please sign in to comment.