diff --git a/lib/license_finder/package_managers/govendor.rb b/lib/license_finder/package_managers/govendor.rb index 387abcf47..9b3bc3fa7 100644 --- a/lib/license_finder/package_managers/govendor.rb +++ b/lib/license_finder/package_managers/govendor.rb @@ -1,3 +1,6 @@ +require 'license_finder/shared_helpers/common_path' +require 'json' + module LicenseFinder class Govendor < PackageManager def possible_package_paths @@ -6,13 +9,12 @@ def possible_package_paths def current_packages file = File.read(detected_package_path) - json = JSON.parse(file) - packages = json['package'] + packages = packages_from_json(file) packages.map do |package| GoPackage.from_dependency({ - 'ImportPath' => package['path'], - 'InstallPath' => project_path.join('vendor', package['path']), - 'Rev' => package['revision'] + 'ImportPath' => package[:path], + 'InstallPath' => project_path.join('vendor', package[:path]), + 'Rev' => package[:sha] }, nil, true) end end @@ -28,5 +30,32 @@ def self.package_management_command def self.prepare_command 'govendor sync' end + + private + + def packages_from_json(json_string) + data = JSON.parse(json_string) + packages = data['package'] + + packages_by_sha = {} + + packages.each do |package| + package_path = package['path'] + package_revision = package['revision'] + if packages_by_sha[package_revision].nil? + packages_by_sha[package_revision] = [package_path] + else + packages_by_sha[package_revision] << package_path + end + end + + result = [] + packages_by_sha.each do |sha, paths| + common_paths = CommonPathHelper.shortest_common_paths(paths) + common_paths.each { |cp| result << { sha: sha, path: cp } } + end + + result + end end end diff --git a/spec/fixtures/config/govendor_common_paths.json b/spec/fixtures/config/govendor_common_paths.json new file mode 100644 index 000000000..1c62bb268 --- /dev/null +++ b/spec/fixtures/config/govendor_common_paths.json @@ -0,0 +1,19 @@ +{ + "comment": "", + "ignore": "test", + "package": [ + { + "checksumSHA1": "4Tc07iR3HloUYC4HNT4xc0875WY=", + "path": "foo/Bowery/prompt", + "revision": "0f1139e9a1c74b57ccce6bdb3cd2f7cd04dd3449", + "revisionTime": "2017-02-19T07:16:37Z" + }, + { + "checksumSHA1": "6VGFARaK8zd23IAiDf7a+gglC8k=", + "path": "foo/Bowery/safefile", + "revision": "0f1139e9a1c74b57ccce6bdb3cd2f7cd04dd3449", + "revisionTime": "2015-10-22T12:31:44+02:00" + } + ], + "rootPath": "foo/kardianos/govendor" +} \ No newline at end of file diff --git a/spec/lib/license_finder/package_managers/govendor_spec.rb b/spec/lib/license_finder/package_managers/govendor_spec.rb index e13e421bb..1c2cadbd8 100644 --- a/spec/lib/license_finder/package_managers/govendor_spec.rb +++ b/spec/lib/license_finder/package_managers/govendor_spec.rb @@ -27,6 +27,26 @@ module LicenseFinder expect(subject.current_packages.last.version).to eq '855e8d98f1852d48dde521e0522408d1fe7e836a' end end + + context 'when there are common paths' do + let(:content) do + FakeFS.without do + fixture_from('govendor_common_paths.json') + end + end + + it 'returns the packages described by vendor/vendor.json with commmon paths consolidated' do + FakeFS do + FileUtils.mkdir_p '/app/vendor' + File.write('/app/vendor/vendor.json', content) + + expect(subject.current_packages.length).to eq 1 + + expect(subject.current_packages.first.name).to eq 'foo/Bowery' + expect(subject.current_packages.first.version).to eq '0f1139e9a1c74b57ccce6bdb3cd2f7cd04dd3449' + end + end + end end describe '.prepare_command' do