Skip to content

Commit

Permalink
[Changed] Raise an error if the yarn licenses list command fails
Browse files Browse the repository at this point in the history
Previously, LicenseFinder::Yarn.new.current_packages silently returned
an empty array when `yarn licenses list` failed, resulting in no license
violations.
This is dangerous as it can lead to thinking there are no license issues
present.

Missing or incorrectly configured environment variables can lead to
`yarn licenses list` failing.
  • Loading branch information
ajesler-hatch authored and ajesler committed Feb 15, 2022
1 parent cfbc7d3 commit 8f9ab6b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/license_finder/package_managers/yarn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def current_packages
suffix = " --cwd #{project_path}" unless project_path.nil?
cmd += suffix unless suffix.nil?

stdout, _stderr, status = Cmd.run(cmd)
return [] unless status.success?
stdout, stderr, status = Cmd.run(cmd)
raise "Command '#{cmd}' failed to execute: #{stderr}" unless status.success?

packages = []
incompatible_packages = []
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/license_finder/package_managers/yarn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ module LicenseFinder
expect(subject.current_packages.first.homepage).to eq 'https://github.com/felixg?/node-stack-trace'
end
end

context "when the shell command raises an error" do
it "raises" do
allow(SharedHelpers::Cmd).to receive(:run).with(Yarn::SHELL_COMMAND + " --cwd #{Pathname(root)}").and_return([nil, 'error', cmd_failure])

expect { subject.current_packages }.to raise_error(%r{Command 'yarn licenses list --no-progress --json --cwd #{Pathname(root)}' failed to execute: error})
end
end
end

describe '.prepare_command' do
Expand Down

0 comments on commit 8f9ab6b

Please sign in to comment.