Skip to content

Commit

Permalink
[FIXED] Fix crash in LF for null deps in godep
Browse files Browse the repository at this point in the history
[#168763481]

Signed-off-by: Debbie Chen <dechen@pivotal.io>
  • Loading branch information
xtreme-shane-lattanzio authored and xtreme-debbie-chen committed Sep 25, 2019
1 parent 33e41c1 commit aec335e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 82 deletions.
6 changes: 4 additions & 2 deletions lib/license_finder/package_managers/go_dep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ def workspace_dir

def packages_from_json(json_string)
all_packages = JSON.parse(json_string)['Deps']
packages_grouped_by_revision = all_packages.group_by { |package| package['Rev'] }

return [] unless all_packages

packages_grouped_by_revision = all_packages.group_by { |package| package['Rev'] }
result = []

packages_grouped_by_revision.each do |_sha, packages_in_group|
all_paths_in_group = packages_in_group.map { |p| p['ImportPath'] }
common_paths = CommonPathHelper.longest_common_paths(all_paths_in_group)
Expand All @@ -64,7 +67,6 @@ def packages_from_json(json_string)
@full_version)
end
end

result
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/config/empty_godep.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ImportPath": "github.com/pivotal/foo",
"GoVersion": "go1.4.2",
"Deps": null
}
189 changes: 109 additions & 80 deletions spec/lib/license_finder/package_managers/go_dep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

module LicenseFinder
describe GoDep do
let(:options) { {} }
subject { GoDep.new(options.merge(project_path: Pathname('/fake/path'))) }
let(:options) {{}}
subject {GoDep.new(options.merge(project_path: Pathname('/fake/path')))}

it_behaves_like 'a PackageManager'

Expand All @@ -22,117 +22,146 @@ module LicenseFinder
end
end

describe '#current_packages' do
before do
FakeFS.activate!
FileUtils.mkdir_p '/fake/path/Godeps'
File.write('/fake/path/Godeps/Godeps.json', content)
describe '#current_packages', :focus do
context 'when the godep json is empty' do
let(:content) do
FakeFS.without do
fixture_from('empty_godep.json')
end
end

@orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = '/fake/go/path'
end
before do
FakeFS.activate!
FileUtils.mkdir_p '/fake/path/Godeps'
File.write('/fake/path/Godeps/Godeps.json', content)

after do
FakeFS.deactivate!
ENV['GOPATH'] = @orig_gopath
end
@orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = '/fake/go/path'
end

it 'sets the homepage for packages' do
packages = subject.current_packages
after do
FakeFS.deactivate!
ENV['GOPATH'] = @orig_gopath
end

expect(packages[0].homepage).to eq('github.com/pivotal/foo')
expect(packages[1].homepage).to eq('github.com/pivotal/bar')
expect(packages[2].homepage).to eq('code.google.com/foo/bar')
it 'shows nothing' do
expect(subject.current_packages).to eq([])
end
end

context 'when the GoDep returns entries with same sha and common base path' do
let(:options) { { go_full_version: true } }
let(:all_packages) { subject.current_packages }

context ' when there is a populated godep json' do
before do
File.write('/fake/path/Godeps/Godeps.json', content_with_duplicates)
FakeFS.activate!
FileUtils.mkdir_p '/fake/path/Godeps'
File.write('/fake/path/Godeps/Godeps.json', content)

@orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = '/fake/go/path'
end

it 'filters dependencies based on same shas and common paths' do
expect(all_packages.length).to eq(3)
after do
FakeFS.deactivate!
ENV['GOPATH'] = @orig_gopath
end

it 'removes duplicate entries having same sha with common base path' do
packages_with_common_path = all_packages.select do |package|
package.name == 'github.com/foo/baz'
end

expect(packages_with_common_path.length).to eq(1)
expect(packages_with_common_path.first.name).to eq('github.com/foo/baz')
expect(packages_with_common_path.first.version).to eq('28838aae6e8158e3695cf90e2f0ed2498b68ee1d')
it 'sets the homepage for packages' do
packages = subject.current_packages

expect(packages[0].homepage).to eq('github.com/pivotal/foo')
expect(packages[1].homepage).to eq('github.com/pivotal/bar')
expect(packages[2].homepage).to eq('code.google.com/foo/bar')
end

it 'shows entries having same shas with no common base path' do
packages_with_same_sha = all_packages.select do |package|
package.version == '28838aae6e8158e3695cf90e2f0ed2498b68ee1d'
context 'when the GoDep returns entries with same sha and common base path' do
let(:options) {{go_full_version: true}}
let(:all_packages) {subject.current_packages}

before do
File.write('/fake/path/Godeps/Godeps.json', content_with_duplicates)
end

expect(packages_with_same_sha.length).to eq(2)
expect(packages_with_same_sha[0].name).to eq('github.com/foo/baz')
expect(packages_with_same_sha[1].name).to eq('code.google.com/foo/bar')
end
it 'filters dependencies based on same shas and common paths' do
expect(all_packages.length).to eq(3)
end

it 'shows entries with different shas' do
expect(all_packages.last.name).to eq('github.com/foo/baz/sub3')
expect(all_packages.last.version).to eq('28838aae6e8158e3695cf90e2f0ed2498b68ee1e')
end
end
it 'removes duplicate entries having same sha with common base path' do
packages_with_common_path = all_packages.select do |package|
package.name == 'github.com/foo/baz'
end

context 'when dependencies are vendored' do
before do
allow(FileTest).to receive(:directory?).with('/fake/path/Godeps/_workspace').and_return(true)
end
expect(packages_with_common_path.length).to eq(1)
expect(packages_with_common_path.first.name).to eq('github.com/foo/baz')
expect(packages_with_common_path.first.version).to eq('28838aae6e8158e3695cf90e2f0ed2498b68ee1d')
end

it 'should return an array of packages' do
packages = subject.current_packages
expect(packages.map(&:name)).to include('github.com/pivotal/foo', 'github.com/pivotal/bar')
expect(packages.map(&:version)).to include('61164e4', '3245708')
end
it 'shows entries having same shas with no common base path' do
packages_with_same_sha = all_packages.select do |package|
package.version == '28838aae6e8158e3695cf90e2f0ed2498b68ee1d'
end

it 'should set the install_path to the vendored directory' do
packages = subject.current_packages
expect(packages[0].install_path).to eq('/fake/path/Godeps/_workspace/src/github.com/pivotal/foo')
expect(packages[1].install_path).to eq('/fake/path/Godeps/_workspace/src/github.com/pivotal/bar')
expect(packages_with_same_sha.length).to eq(2)
expect(packages_with_same_sha[0].name).to eq('github.com/foo/baz')
expect(packages_with_same_sha[1].name).to eq('code.google.com/foo/bar')
end

it 'shows entries with different shas' do
expect(all_packages.last.name).to eq('github.com/foo/baz/sub3')
expect(all_packages.last.version).to eq('28838aae6e8158e3695cf90e2f0ed2498b68ee1e')
end
end

context 'when requesting the full version' do
let(:options) { { go_full_version: true } }
context 'when dependencies are vendored' do
before do
allow(FileTest).to receive(:directory?).with('/fake/path/Godeps/_workspace').and_return(true)
end

it 'should return an array of packages' do
packages = subject.current_packages
expect(packages.map(&:name)).to include('github.com/pivotal/foo', 'github.com/pivotal/bar')
expect(packages.map(&:version)).to include('61164e4', '3245708')
end

it 'list the dependencies with full version' do
expect(subject.current_packages.map(&:version)).to eq %w[
it 'should set the install_path to the vendored directory' do
packages = subject.current_packages
expect(packages[0].install_path).to eq('/fake/path/Godeps/_workspace/src/github.com/pivotal/foo')
expect(packages[1].install_path).to eq('/fake/path/Godeps/_workspace/src/github.com/pivotal/bar')
end

context 'when requesting the full version' do
let(:options) {{go_full_version: true}}

it 'list the dependencies with full version' do
expect(subject.current_packages.map(&:version)).to eq %w[
61164e49940b423ba1f12ddbdf01632ac793e5e9
3245708abcdef234589450649872346783298736
3245708abcdef234589450649872346783298735
]
end
end
end
end

context 'when dependencies are not vendored' do
before do
@orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = '/fake/go/path'
end
context 'when dependencies are not vendored' do
before do
@orig_gopath = ENV['GOPATH']
ENV['GOPATH'] = '/fake/go/path'
end

after do
ENV['GOPATH'] = @orig_gopath
end
after do
ENV['GOPATH'] = @orig_gopath
end

it 'should return an array of packages' do
packages = subject.current_packages
expect(packages.map(&:name)).to include('github.com/pivotal/foo', 'github.com/pivotal/bar')
expect(packages.map(&:version)).to include('61164e4', '3245708')
end
it 'should return an array of packages' do
packages = subject.current_packages
expect(packages.map(&:name)).to include('github.com/pivotal/foo', 'github.com/pivotal/bar')
expect(packages.map(&:version)).to include('61164e4', '3245708')
end

it 'should set the install_path to the GOPATH' do
packages = subject.current_packages
expect(packages[0].install_path).to eq('/fake/go/path/src/github.com/pivotal/foo')
expect(packages[1].install_path).to eq('/fake/go/path/src/github.com/pivotal/bar')
it 'should set the install_path to the GOPATH' do
packages = subject.current_packages
expect(packages[0].install_path).to eq('/fake/go/path/src/github.com/pivotal/foo')
expect(packages[1].install_path).to eq('/fake/go/path/src/github.com/pivotal/bar')
end
end
end
end
Expand Down

0 comments on commit aec335e

Please sign in to comment.