Skip to content

Commit

Permalink
[FIXED] Fix install_paths for go mod now accurately report dependency…
Browse files Browse the repository at this point in the history
… installation directories [#161943322 finish]

Signed-off-by: Li Tai <ltai@pivotal.io>
  • Loading branch information
xtreme-shane-lattanzio authored and Your Name committed Nov 21, 2018
1 parent 611ef73 commit ea28c06
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
4 changes: 1 addition & 3 deletions features/features/package_managers/go_modules_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# frozen_string_literal: true

require_relative '../../support/feature_helper'

describe 'Go Modules Dependencies' do
let(:go_developer) { LicenseFinder::TestingDSL::User.new }

specify 'are shown in reports for a project' do
LicenseFinder::TestingDSL::GoModulesProject.create
go_developer.run_license_finder('go_modules')

expect(go_developer).to be_seeing_line 'gopkg.in/check.v1, v0.0.0-20161208181325-20d25e280405, "Apache 2.0, MIT"'
expect(go_developer).to be_seeing_line 'gopkg.in/check.v1, v0.0.0-20161208181325-20d25e280405, unknown'
expect(go_developer).to be_seeing_line 'gopkg.in/yaml.v2, v2.2.1, "Apache 2.0, MIT"'
end
end
5 changes: 4 additions & 1 deletion features/fixtures/go_modules/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module foo

require gopkg.in/yaml.v2 v2.2.1
require (
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
gopkg.in/yaml.v2 v2.2.1
)
1 change: 1 addition & 0 deletions features/fixtures/go_modules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package foo

import (
"gopkg.in/yaml.v2"
"gopkg.in/check.v1"
)
28 changes: 11 additions & 17 deletions lib/license_finder/package_managers/go_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ def active?
end

def current_packages
sum_file_paths.uniq.map do |file_path|
read_sum(file_path)
end.flatten
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
packages.reject do |package|
Pathname(package.install_path).cleanpath == Pathname(project_path).cleanpath
end
# binding.pry
end

private
Expand All @@ -36,20 +43,7 @@ def sum_file_paths
Dir[project_path.join(PACKAGES_FILE)]
end

def read_sum(file_path)
contents = File.read(file_path)
contents.each_line.map do |line|
line.include?('go.mod') ? nil : read_package(file_path, line)
end.compact
end

def read_package(file_path, line)
parts = line.split(' ')
install_path = File.dirname(file_path)

name = parts[0]
version = parts[1]

def read_package(install_path, name, version)
info = {
'ImportPath' => name,
'InstallPath' => install_path,
Expand Down
7 changes: 5 additions & 2 deletions lib/license_finder/package_managers/go_workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

require 'json'
module LicenseFinder
class GoWorkspacePackageManagerError < ::StandardError
end

class GoWorkspace < PackageManager
Submodule = Struct.new :install_path, :revision
ENVRC_REGEXP = /GOPATH|GO15VENDOREXPERIMENT/
Expand Down Expand Up @@ -77,9 +80,9 @@ def go_list
# with status code 1. Setting GOPATH to nil removes those warnings.
orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = nil
val, _stderr, status = Cmd.run('go list -f "{{join .Deps \"\n\"}}" ./...')
val, stderr, status = Cmd.run('go list -f "{{join .Deps \"\n\"}}" ./...')
ENV['GOPATH'] = orig_gopath
raise 'go list failed' unless status.success?
raise GoWorkspacePackageManagerError, "go list failed:\n#{stderr}" unless status.success?

# Select non-standard packages. `go list std` returns the list of standard
# dependencies. We then filter those dependencies out of the full list of
Expand Down
13 changes: 8 additions & 5 deletions spec/lib/license_finder/package_managers/go_modules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ module LicenseFinder
let(:src_path) { '/workspace/code' }
let(:sum_path) { "#{src_path}/go.sum" }
let(:vendor_path) { "#{src_path}/vendor" }

let(:go_list_string) do
"foo,,/workspace/code/\ngopkg.in/check.v1,v0.0.0-20161208181325-20d25e280405,"\
"/workspace/LicenseFinder/features/fixtures/go_modules/vendor/gopkg.in/check.v1\n"\
'gopkg.in/yaml.v2,v2.2.1,/workspace/LicenseFinder/features/fixtures/go_modules/vendor/gopkg.in/yaml.v2'
end
subject { GoModules.new(project_path: Pathname(src_path), logger: double(:logger, active: nil)) }

describe '#current_packages' do
Expand All @@ -19,22 +23,21 @@ module LicenseFinder

FileUtils.mkdir_p(vendor_path)
File.write(sum_path, content)

allow(SharedHelpers::Cmd).to receive(:run).with("GO111MODULE=on go list -m -mod=vendor -f '{{.Path}},{{.Version}},{{.Dir}}' all").and_return(go_list_string)
end

after do
FakeFS.deactivate!
end

let(:src_path) { '/workspace/code' }
let(:sum_path) { "#{src_path}/go.sum" }

let(:content) do
FakeFS.without do
fixture_from('go.sum')
end
end

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

expect(packages.length).to eq 2
Expand Down

0 comments on commit ea28c06

Please sign in to comment.