-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Condense gvt paths with identical shas into their common path
[Finish #155382947, #154072994, #155830792] Signed-off-by: Vikram Yadav <[email protected]>
- Loading branch information
Ryan Collins
authored and
Ryan Collins
committed
Mar 21, 2018
1 parent
f2e6238
commit 9e1071d
Showing
4 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module CommonPathHelper | ||
def self.shortest_common_paths(paths) | ||
[].tap do |common_paths| | ||
# organize by matching root paths | ||
paths_with_roots = paths.group_by { |path| path.split('/').first } | ||
paths_with_roots.each do |common_root, full_paths| | ||
# use the shortest path as the 'template' | ||
shortest_path = full_paths.sort_by { |path| path.split('/').length }.first | ||
shortest_common_path = common_root | ||
|
||
# iterate through each subpath of the 'template' | ||
shortest_path.split('/').each_with_index do |subpath, i| | ||
potential_path = i.zero? ? shortest_common_path : [shortest_common_path, subpath].join('/') | ||
|
||
# check each for the existence of the subsequent subpath | ||
mismatch = full_paths.any? { |path| !path.start_with?(potential_path) } | ||
break if mismatch | ||
|
||
shortest_common_path = potential_path | ||
end | ||
common_paths << shortest_common_path | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
spec/lib/license_finder/shared_helpers/common_path_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
require 'license_finder/shared_helpers/common_path' | ||
|
||
describe CommonPathHelper do | ||
context 'when the GVT returns entries with same sha with common base path' do | ||
let(:gvt_output_with_common_paths) do | ||
%w[cloud.google.com/go/bigquery cloud.google.com/go/civil cloud.google.com/go/compute/metadata] | ||
end | ||
let(:gvt_output_without_common_paths) do | ||
%w[cloud.google.com/go/bigquery/adsf cloud.google.com/go/civil cloud.aws.com/go/metadata] | ||
end | ||
|
||
it 'only shows the entry with common base path once' do | ||
paths = CommonPathHelper.shortest_common_paths gvt_output_with_common_paths | ||
expect(paths).to match_array %w[cloud.google.com/go] | ||
end | ||
|
||
it 'shows entries with same sha when they do not have a common base path' do | ||
paths = CommonPathHelper.shortest_common_paths gvt_output_without_common_paths | ||
expect(paths).to match_array %w[cloud.google.com/go cloud.aws.com/go/metadata] | ||
end | ||
end | ||
end |