Skip to content

Commit

Permalink
[Fixed] Go modules reports incorrect install paths
Browse files Browse the repository at this point in the history
Signed-off-by: Li Tai <ltai@pivotal.io>
  • Loading branch information
Serafima Ostrovskaya authored and xtreme-lisheng-tai committed Nov 15, 2018
1 parent 5a6f911 commit 9ab5aa9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def log_to_file(contents)
FileUtils.mkdir_p @log_directory

# replace whitespace with underscores and remove slashes
log_file_name = self.class.package_management_command&.gsub(/\s/, '_')&.gsub(/\//, '')
log_file_name = self.class.package_management_command&.gsub(/\s/, '_')&.gsub(%r{\/}, '')
log_file = File.join(@log_directory, "prepare_#{log_file_name || 'errors'}.log")

File.open(log_file, 'w') do |f|
Expand Down
32 changes: 19 additions & 13 deletions lib/license_finder/package_managers/go_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,52 @@ def prepare_command
end

def active?
sum_files?
sum_file?
end

def current_packages
sum_file_paths.uniq.map do |file_path|
read_sum(file_path)
end.flatten
[read_sum(sum_file_path)].flatten
end

private

def sum_files?
sum_file_paths.any?
def sum_file?
!sum_file_path.nil?
end

def sum_file_paths
Dir[project_path.join(PACKAGES_FILE)]
def sum_file_path
Dir[project_path.join(PACKAGES_FILE)].first
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)
line.include?('go.mod') ? nil : read_package(line)
end.compact
end

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

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

info = {
'ImportPath' => name,
'InstallPath' => install_path,
'Rev' => version
}

GoPackage.from_dependency(info, nil, true)
GoPackage.from_dependency(info, install_prefix(name), true)
end

def install_prefix(name)
return vendor_dir if Dir.exist?(File.join(vendor_dir, name))

Pathname(ENV['GOPATH'] || ENV['HOME'] + '/go').join('src')
end

def vendor_dir
File.join(project_path, 'vendor')
end
end
end
2 changes: 1 addition & 1 deletion lib/license_finder/packages/go_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class << self
def from_dependency(hash, prefix, full_version)
name = hash['ImportPath']
install_path = hash['InstallPath']
install_path ||= install_path(prefix.join(name))
install_path ||= install_path(File.join(prefix, name))
version = full_version ? hash['Rev'] : hash['Rev'][0..6]
homepage = hash['Homepage']
new(name, version, install_path: install_path, package_manager: 'Go', homepage: homepage)
Expand Down
65 changes: 60 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 @@ -9,25 +9,21 @@ module LicenseFinder

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

subject { GoModules.new(project_path: Pathname(src_path), logger: double(:logger, active: nil)) }

describe '#current_packages' do
before do
FakeFS.activate!

FileUtils.mkdir_p(vendor_path)
FileUtils.mkdir_p(src_path)
File.write(sum_path, content)
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')
Expand All @@ -51,6 +47,65 @@ module LicenseFinder

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

context 'when there are vendored dependencies' do
let(:vendor_path) { "#{src_path}/vendor" }
let(:vendored_dep1) { File.join(vendor_path, 'gopkg.in/check.v1') }
let(:vendored_dep2) { File.join(vendor_path, 'gopkg.in/yaml.v2') }

before do
FakeFS.activate!

FileUtils.mkdir_p(vendored_dep1)
FileUtils.mkdir_p(vendored_dep2)
end

after do
FileUtils.rm_r(vendored_dep1)
FileUtils.rm_r(vendored_dep2)

FakeFS.deactivate!
end

it 'sets the packages\' install_paths to the vendored directories' do
packages = subject.current_packages

expect(packages.first.install_path).to eq File.join(vendor_path, 'gopkg.in/check.v1')
expect(packages.last.install_path).to eq File.join(vendor_path, 'gopkg.in/yaml.v2')
end
end

context 'when there are no vendored dependencies' do
context 'when GOPATH is configured' do
let(:gopath) { '/some/path' }

before do
stub_const('ENV', 'GOPATH' => gopath)
end

it 'set the packages\' install_paths to the dependencies\' location in the GOPATH' do
packages = subject.current_packages

expect(packages.first.install_path).to eq File.join(gopath, 'src', 'gopkg.in/check.v1')
expect(packages.last.install_path).to eq File.join(gopath, 'src', 'gopkg.in/yaml.v2')
end
end

context 'when GOPATH is not configured' do
let(:home_path) { '/some/path' }

before do
stub_const('ENV', 'HOME' => home_path)
end

it 'set the packages\' install_paths to the dependencies\' location in $HOME/go' do
packages = subject.current_packages

expect(packages.first.install_path).to eq File.join(home_path, 'go', 'src', 'gopkg.in/check.v1')
expect(packages.last.install_path).to eq File.join(home_path, 'go', 'src', 'gopkg.in/yaml.v2')
end
end
end
end

describe '.prepare_command' do
Expand Down

0 comments on commit 9ab5aa9

Please sign in to comment.