Skip to content

Commit

Permalink
[Fixed] Govendor not consolidating common paths from the same SHA
Browse files Browse the repository at this point in the history
[#156361589 finish]

Signed-off-by: Daniil Kouznetsov <dkouznetsov@pivotal.io>
  • Loading branch information
xtreme-shane-lattanzio authored and Vikram Yadav committed Mar 29, 2018
1 parent a49af96 commit bdd23c9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
39 changes: 34 additions & 5 deletions lib/license_finder/package_managers/govendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'license_finder/shared_helpers/common_path'
require 'json'

module LicenseFinder
class Govendor < PackageManager
def possible_package_paths
Expand All @@ -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
Expand All @@ -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
19 changes: 19 additions & 0 deletions spec/fixtures/config/govendor_common_paths.json
Original file line number Diff line number Diff line change
@@ -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"
}
20 changes: 20 additions & 0 deletions spec/lib/license_finder/package_managers/govendor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bdd23c9

Please sign in to comment.