diff --git a/lib/license_finder/package_manager.rb b/lib/license_finder/package_manager.rb index 7109b8798..f6e41a914 100644 --- a/lib/license_finder/package_manager.rb +++ b/lib/license_finder/package_manager.rb @@ -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| diff --git a/lib/license_finder/package_managers/go_modules.rb b/lib/license_finder/package_managers/go_modules.rb index 25735b9fa..10e7bf844 100644 --- a/lib/license_finder/package_managers/go_modules.rb +++ b/lib/license_finder/package_managers/go_modules.rb @@ -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 diff --git a/lib/license_finder/packages/go_package.rb b/lib/license_finder/packages/go_package.rb index dee88d901..7344004d9 100644 --- a/lib/license_finder/packages/go_package.rb +++ b/lib/license_finder/packages/go_package.rb @@ -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) diff --git a/spec/lib/license_finder/package_managers/go_modules_spec.rb b/spec/lib/license_finder/package_managers/go_modules_spec.rb index 7c4fb6404..8e7b80420 100644 --- a/spec/lib/license_finder/package_managers/go_modules_spec.rb +++ b/spec/lib/license_finder/package_managers/go_modules_spec.rb @@ -9,7 +9,6 @@ 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)) } @@ -17,7 +16,7 @@ module LicenseFinder before do FakeFS.activate! - FileUtils.mkdir_p(vendor_path) + FileUtils.mkdir_p(src_path) File.write(sum_path, content) end @@ -25,9 +24,6 @@ module LicenseFinder 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') @@ -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