Skip to content

Commit

Permalink
GoModules: fix compute with vendor mod
Browse files Browse the repository at this point in the history
  • Loading branch information
forelabs committed May 5, 2020
1 parent 6df7ef9 commit 067eb19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/license_finder/package_managers/go_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ def active?
end

def current_packages
info_output, _stderr, _status = Cmd.run("GO111MODULE=on go list -m -mod=vendor -f '{{.Path}},{{.Version}},{{.Dir}}' all")
packages_info = info_output.split("\n")
packages = packages_info.map do |package|
name, version, install_path = package.split(',')
read_package(install_path, name, version)
end
read_package(install_path, name, version) if install_path.to_s != ""
end.compact
packages.reject do |package|
Pathname(package.install_path).cleanpath == Pathname(project_path).cleanpath
end
end

private

def packages_info
info_output, stderr, _status = Cmd.run("GO111MODULE=on go list -m -mod=vendor -f '{{.Path}},{{.Version}},{{.Dir}}' all")
if stderr =~ Regexp.compile("can't compute 'all' using the vendor directory")
info_output, _stderr, _status = Cmd.run("GO111MODULE=on go list -m -f '{{.Path}},{{.Version}},{{.Dir}}' all")
end

info_output.split("\n")
end

def sum_files?
sum_file_paths.any?
end
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/license_finder/package_managers/go_modules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ module LicenseFinder

expect(packages.first.package_manager).to eq 'Go'
end

context 'when compute is not allowed on vendor' do
before do
allow(SharedHelpers::Cmd).to receive(:run).with("GO111MODULE=on go list -m -mod=vendor -f '{{.Path}},{{.Version}},{{.Dir}}' all").and_return(["", "go list -m: can't compute 'all' using the vendor directory\n\t(Use -mod=mod or -mod=readonly to bypass.)\n", 1])
allow(SharedHelpers::Cmd).to receive(:run).with("GO111MODULE=on go list -m -f '{{.Path}},{{.Version}},{{.Dir}}' all").and_return(go_list_string)
end

it 'finds all the packages all go.sum files' do
packages = subject.current_packages

expect(packages.length).to eq 2

expect(packages.first.name).to eq 'gopkg.in/check.v1'
expect(packages.first.version).to eq 'v0.0.0-20161208181325-20d25e280405'

expect(packages.last.name).to eq 'gopkg.in/yaml.v2'
expect(packages.last.version).to eq 'v2.2.1'
end
end
end

describe '.prepare_command' do
Expand Down

0 comments on commit 067eb19

Please sign in to comment.