Skip to content

Commit f57745b

Browse files
committed
[Fixed] Fix 'unknown' Yarn 2 licenses
Yarn 2 groups packages by the license, so there shouldn't be any 'unknown's during regular processing. The "incompatible match" condition seems to apply to the version, not the license, so it doesn't need to be set to 'unknown' when this condition is met.
1 parent 46ae9f6 commit f57745b

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

lib/license_finder/package_managers/yarn.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def get_yarn_packages(json_objects)
9696
body = json_object['children']
9797

9898
body.each do |package_name, vendor_info|
99-
valid_match = %r{(?<name>[@,\w,\-,/,.]+)@(?<manager>\D*):\D*(?<version>(\d+\.?)+)} =~ package_name.to_s
100-
valid_match = %r{(?<name>[@,\w,\-,/,.]+)@virtual:.+#(\D*):\D*(?<version>(\d+\.?)+)} =~ package_name.to_s if manager.eql?('virtual')
99+
valid_match = %r{(?<name>@?[\w/.-]+)@(?<manager>\D*):\D*(?<version>(\d+\.?)+)} =~ package_name.to_s
100+
valid_match = %r{(?<name>@?[\w/.-]+)@virtual:.+#(\D*):\D*(?<version>(\d+\.?)+)} =~ package_name.to_s if manager.eql?('virtual')
101101

102102
if valid_match
103103
homepage = vendor_info['children']['vendorUrl']
@@ -112,10 +112,10 @@ def get_yarn_packages(json_objects)
112112
)
113113
packages << package
114114
end
115-
incompatible_match = /(?<name>[\w,-]+)@[a-z]*:(?<version>(\.))/ =~ package_name.to_s
116115

116+
incompatible_match = %r{(?<name>@?[\w/.-]+)@[a-z]*:(?<version>(\.))} =~ package_name.to_s
117117
if incompatible_match
118-
package = YarnPackage.new(name, version, spec_licenses: ['unknown'])
118+
package = YarnPackage.new(name, version, spec_licenses: [license])
119119
incompatible_packages.push(package)
120120
end
121121
end
@@ -126,14 +126,14 @@ def get_yarn_packages(json_objects)
126126

127127
def get_yarn1_packages(json_objects)
128128
packages = []
129-
incompatible_packages = []
130129
if json_objects.last['type'] == 'table'
131130
license_json = json_objects.pop['data']
132131
packages = packages_from_json(license_json)
133132
end
134133

134+
incompatible_packages = []
135135
json_objects.each do |json_object|
136-
match = /(?<name>[\w,-]+)@(?<version>(\d+\.?)+)/ =~ json_object['data'].to_s
136+
match = %r{(?<name>@?[\w/.-]+)@(?<version>(\d+\.?)+)} =~ json_object['data'].to_s
137137
if match
138138
package = YarnPackage.new(name, version, spec_licenses: ['unknown'])
139139
incompatible_packages.push(package)

spec/lib/license_finder/package_managers/yarn_spec.rb

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ module LicenseFinder
1313
{
1414
'type' => 'table',
1515
'data' => {
16-
'body' => [['yn', '2.0.0', 'MIT', 'https://github.com/sindresorhus/yn.git', 'sindresorhus.com', 'Sindre Sorhus']],
16+
'body' => [
17+
['@sindresorhus/is', '0.7.0', 'MIT', 'https://github.com/sindresorhus/is.git', 'sindresorhus.com', 'Sindre Sorhus'],
18+
['yn', '2.0.0', 'MIT', 'https://github.com/sindresorhus/yn.git', 'sindresorhus.com', 'Sindre Sorhus']
19+
],
1720
'head' => %w[Name Version License URL VendorUrl VendorName]
1821
}
1922
}.to_json
@@ -161,6 +164,21 @@ module LicenseFinder
161164
expect(subject.current_packages.last.authors).to eq ''
162165
expect(subject.current_packages.last.install_path).to eq Pathname(root).join('yarn_modules', '@types/jest')
163166
end
167+
168+
it 'displays incompatible packages with correct license type' do
169+
allow(SharedHelpers::Cmd).to receive(:run).with(Yarn::SHELL_COMMAND) do
170+
[
171+
'{"value":"Internal","children":{"@company/package@workspace:.":{"value":{"locator":"@company/package@workspace:.","descriptor":"@company/package@workspace:."},"children":{}}}}',
172+
'',
173+
cmd_success
174+
]
175+
end
176+
177+
expect(subject.current_packages.length).to eq 1
178+
expect(subject.current_packages.last.name).to eq '@company/package'
179+
expect(subject.current_packages.last.version).to eq '.'
180+
expect(subject.current_packages.last.license_names_from_spec).to eq ['Internal']
181+
end
164182
end
165183

166184
it 'displays packages as returned from "yarn list"' do
@@ -171,13 +189,19 @@ module LicenseFinder
171189
[yarn1_shell_command_output, '', cmd_success]
172190
end
173191

174-
expect(subject.current_packages.length).to eq 1
175-
expect(subject.current_packages.first.name).to eq 'yn'
176-
expect(subject.current_packages.first.version).to eq '2.0.0'
192+
expect(subject.current_packages.length).to eq 2
193+
expect(subject.current_packages.first.name).to eq '@sindresorhus/is'
194+
expect(subject.current_packages.first.version).to eq '0.7.0'
177195
expect(subject.current_packages.first.license_names_from_spec).to eq ['MIT']
178196
expect(subject.current_packages.first.homepage).to eq 'sindresorhus.com'
179197
expect(subject.current_packages.first.authors).to eq 'Sindre Sorhus'
180-
expect(subject.current_packages.first.install_path).to eq Pathname(root).join('yarn_modules', 'yn')
198+
expect(subject.current_packages.first.install_path).to eq Pathname(root).join('yarn_modules', '@sindresorhus/is')
199+
expect(subject.current_packages.last.name).to eq 'yn'
200+
expect(subject.current_packages.last.version).to eq '2.0.0'
201+
expect(subject.current_packages.last.license_names_from_spec).to eq ['MIT']
202+
expect(subject.current_packages.last.homepage).to eq 'sindresorhus.com'
203+
expect(subject.current_packages.last.authors).to eq 'Sindre Sorhus'
204+
expect(subject.current_packages.last.install_path).to eq Pathname(root).join('yarn_modules', 'yn')
181205
end
182206

183207
it 'uses node_modules as fallback for install path' do
@@ -188,7 +212,7 @@ module LicenseFinder
188212
[yarn1_shell_command_output, '', cmd_success]
189213
end
190214

191-
expect(subject.current_packages.first.install_path).to eq Pathname(root).join('node_modules', 'yn')
215+
expect(subject.current_packages.last.install_path).to eq Pathname(root).join('node_modules', 'yn')
192216
end
193217

194218
it 'displays incompatible packages with license type unknown' do

0 commit comments

Comments
 (0)