Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19c35a2

Browse files
ahangarhaG-Rath
andauthored
fix: only use Yarn Classic when major version is 1 (#7)
* Ensure yarn classic is detected only with version 1 * Add support for range version using ~ and ^ for yarn * Limit regex repetition wild card * Add explanatory comment for regex * Fix rubocop issue * Simplify regex * Move tests under .read tests * Remove redundant variable and improve comments Co-authored-by: Gareth Jones <[email protected]> * Improve test titles * Remove redundant test * Fix regex for version constraints * Add test for exact version * test: update example * chore: address typo --------- Co-authored-by: Gareth Jones <[email protected]>
1 parent c2bc567 commit 19c35a2

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

lib/package_json.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,21 @@ def determine_package_manager(fallback_manager)
7979

8080
name, version = package_manager.split("@")
8181

82-
if name == "yarn"
83-
raise Error, "a major version must be present for Yarn" if version.nil? || version.empty?
84-
return :yarn_classic if version.start_with?("1")
85-
86-
return :yarn_berry
87-
end
82+
return determin_yarn_version(version) if name == "yarn"
8883

8984
name.to_sym
9085
end
9186

87+
def determin_yarn_version(version)
88+
raise Error, "a major version must be present for Yarn" if version.nil? || version.empty?
89+
90+
# check to see if we're meant to be using Yarn v1 based on the versions major component,
91+
# and accounting for the presence of version constraints like ^, ~, and =
92+
return :yarn_classic if version.match?(/^[~=^]?1(\.|$)/)
93+
94+
:yarn_berry
95+
end
96+
9297
def new_package_manager(package_manager_name)
9398
case package_manager_name
9499
when :npm

spec/package_json_spec.rb

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,55 @@
5555
end
5656
end
5757

58-
it "supports yarn classic" do
58+
it "supports yarn classic with version 1.2.3" do
5959
with_package_json_file({ "version" => "1.0.0", "packageManager" => "[email protected]" }) do
6060
package_json = described_class.read
6161

6262
expect(package_json.manager).to be_a PackageJson::Managers::YarnClassicLike
6363
end
6464
end
6565

66-
it "supports yarn berry" do
66+
it "supports yarn classic with just a major version" do
67+
with_package_json_file({ "version" => "1.0.0", "packageManager" => "yarn@1" }) do
68+
package_json = described_class.read
69+
70+
expect(package_json.manager).to be_a PackageJson::Managers::YarnClassicLike
71+
end
72+
end
73+
74+
it "supports yarn classic with a caret constraint" do
75+
with_package_json_file({ "version" => "1.0.0", "packageManager" => "yarn@^1.2" }) do
76+
package_json = described_class.read
77+
78+
expect(package_json.manager).to be_a PackageJson::Managers::YarnClassicLike
79+
end
80+
end
81+
82+
it "supports yarn classic with tilde constraint" do
83+
with_package_json_file({ "version" => "1.0.0", "packageManager" => "yarn@~1.2" }) do
84+
package_json = described_class.read
85+
86+
expect(package_json.manager).to be_a PackageJson::Managers::YarnClassicLike
87+
end
88+
end
89+
90+
it "supports yarn classic with exact version" do
91+
with_package_json_file({ "version" => "1.0.0", "packageManager" => "yarn@=1.2" }) do
92+
package_json = described_class.read
93+
94+
expect(package_json.manager).to be_a PackageJson::Managers::YarnClassicLike
95+
end
96+
end
97+
98+
it "does not return yarn classic if the major version is 11" do
99+
with_package_json_file({ "version" => "1.0.0", "packageManager" => "[email protected]" }) do
100+
package_json = described_class.read
101+
102+
expect(package_json.manager).not_to be_a PackageJson::Managers::YarnClassicLike
103+
end
104+
end
105+
106+
it "supports yarn berry with version 2.3.2" do
67107
with_package_json_file({ "version" => "1.0.0", "packageManager" => "[email protected]" }) do
68108
package_json = described_class.read
69109

0 commit comments

Comments
 (0)