Skip to content

Commit c1816e1

Browse files
authored
Merge pull request #13689 from caugner/npm_and_yarn/engines_field_for_package_manager_with_caret_and_major_version
fix(npm): parse caret constraint with major-only version
2 parents f9775c6 + e7fc150 commit c1816e1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

npm_and_yarn/lib/dependabot/npm_and_yarn/constraint_helper.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,16 @@ def self.to_ruby_constraint_with_version(constraint, dependabot_versions = [])
261261

262262
full_version = Regexp.last_match(1)
263263

264-
# Normalize version: if full_version does not have patch version, add ".0"
264+
# Normalize version: ensure it has major.minor.patch format
265265
version_parts = T.must(full_version).split(".")
266-
full_version = "#{full_version}.0" if version_parts.length == 2
266+
full_version = case version_parts.length
267+
when 1
268+
"#{full_version}.0.0" # major only -> major.0.0
269+
when 2
270+
"#{full_version}.0" # major.minor -> major.minor.0
271+
else
272+
full_version # already major.minor.patch
273+
end
267274

268275
_, major, minor = version_components(full_version)
269276
return nil if major.nil?

npm_and_yarn/spec/dependabot/npm_and_yarn/package_manager_helper_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@
141141
end
142142
end
143143

144+
context "with engines field for package manager with '^' constraint and missing minor/patch version" do
145+
let(:lockfiles) { {} }
146+
let(:package_json) { { "engines" => { "npm" => "^10" } } }
147+
148+
it "returns a NpmPackageManager instance from engines field" do
149+
expect(helper.package_manager).to be_a(Dependabot::NpmAndYarn::NpmPackageManager)
150+
expect(helper.package_manager.detected_version).to eq("10")
151+
end
152+
end
153+
144154
context "with engines field for npm >=11.0.0" do
145155
let(:lockfiles) { {} }
146156
let(:package_json) { { "engines" => { "npm" => ">=11.0.0" } } }

0 commit comments

Comments
 (0)