Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse umbrella#resolve in affected #91

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 16 additions & 39 deletions lib/cobra_commander/affected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ class Affected
def initialize(umbrella, changes)
@umbrella = umbrella
@changes = changes
run!
end

def names
@names ||= all_affected.map(&:name)
@names ||= all.map(&:name)
end

def all_affected
@all_affected ||= (@directly | @transitively).sort_by(&:name)
def all
@all ||= (directly | transitively).sort_by(&:name)
end

def scripts
@scripts ||= paths.map { |path| File.join(path, "test.sh") }
end

def directly
@directly.map(&method(:affected_component))
@directly ||= @changes.filter_map { |path| @umbrella.resolve(path) }
.uniq.sort_by(&:name)
end

def transitively
@transitively.map(&method(:affected_component))
@transitively ||= directly.flat_map(&:deep_dependents)
.uniq.sort_by(&:name)
end

def json_representation
def to_json(*_args)
{
changed_files: @changes,
directly_affected_components: directly,
transitively_affected_components: transitively,
directly_affected_components: directly.map(&method(:affected_component)),
transitively_affected_components: transitively.map(&method(:affected_component)),
test_scripts: scripts,
component_names: names,
languages: { ruby: contains_ruby?, javascript: contains_js? },
Expand All @@ -42,47 +43,23 @@ def json_representation

private

def run!
@transitively = Set.new
@directly = Set.new
@umbrella.components.each(&method(:add_if_changed))
@transitively = @transitively.sort_by(&:name)
@directly = @directly.sort_by(&:name)
end

def component_changed?(component)
component.root_paths.any? do |component_path|
@changes.any? do |file_path|
file_path.start_with?(component_path)
end
end
end

def add_if_changed(component)
return unless component_changed?(component)

@directly << component
@transitively.merge(component.deep_dependents)
end

def affected_component(component)
{
name: component.name,
path: component.root_paths,
type: component.packages.keys.map(&:to_s).map(&:capitalize).join(" & "),
type: component.packages.keys.map(&:to_s),
}
end

def paths
@paths ||= all_affected.map(&:root_paths).flatten
@paths ||= all.map(&:root_paths).flatten
end

def all_affected_packages
all_affected
.map(&:packages)
.map(&:keys)
.flatten
.uniq
all.map(&:packages)
.map(&:keys)
.flatten
.uniq
end

def contains_ruby?
Expand Down
10 changes: 5 additions & 5 deletions lib/cobra_commander/change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(umbrella, oformat, branch, changes: nil)
def run!
assert_valid_result_choice
if selected_format?("json")
puts affected.json_representation
puts affected.to_json
else
show_full if selected_format?("full")
tests_to_run
Expand Down Expand Up @@ -57,13 +57,13 @@ def changes_since_last_commit

def directly_affected_components
puts "<<< Directly affected components >>>"
affected.directly.each { |component| puts display(**component) }
affected.directly.each { |component| puts display(component) }
puts blank_line
end

def transitively_affected_components
puts "<<< Transitively affected components >>>"
affected.transitively.each { |component| puts display(**component) }
affected.transitively.each { |component| puts display(component) }
puts blank_line
end

Expand All @@ -76,8 +76,8 @@ def tests_to_run
end
end

def display(name:, type:, **)
"#{name} - #{type}"
def display(component)
"#{component.name} - #{component.packages.keys.map(&:to_s).map(&:capitalize).join(' & ')}"
end

def blank_line
Expand Down
2 changes: 1 addition & 1 deletion lib/cobra_commander/cli/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def suggestions(name)

def affected_by_changes(origin_branch)
changes = GitChanged.new(umbrella.path, origin_branch)
Affected.new(umbrella, changes).all_affected
Affected.new(umbrella, changes).all
end

def filter_component(component_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/cobra_commander/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def root_paths
end

def inspect
"#<CobraCommander::Component:#{object_id} #{name} dependencies=#{dependencies.map(&:name)} packages=#{sources}>"
"#<CobraCommander::Component:#{object_id} #{name} dependencies=#{dependencies.map(&:name)} packages=#{packages}>"
end

def deep_dependents
Expand Down
8 changes: 4 additions & 4 deletions lib/cobra_commander/umbrella.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def root
@root_component
end

def resolve(component_root_path)
return root if root.root_paths.include?(component_root_path)

def resolve(path)
components.find do |component|
component.root_paths.include?(component_root_path)
component.root_paths.any? do |component_path|
path.start_with?(component_path.to_s)
end
end
end

Expand Down
116 changes: 31 additions & 85 deletions spec/cobra_commander/affected_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@
end

it "correctly reports directly affected components" do
expect(with_change_to_a.directly).to eq(
[
{
name: "a",
path: ["#{umbrella.path}/components/a"],
type: "Bundler",
},
]
)
expect(with_change_to_a.directly.size).to eql 1
expect(with_change_to_a.directly[0].name).to eql "a"
expect(with_change_to_a.directly[0].packages.keys).to eql %i[bundler]
end

it "correctly reports directly affected components" do
Expand All @@ -64,57 +58,27 @@
end

it "correctly reports directly affected components" do
expect(with_change_to_b.directly).to eq(
[
{
name: "b",
path: ["#{umbrella.path}/components/b"],
type: "Yarn & Bundler",
},
]
)
expect(with_change_to_b.directly.size).to eql 1
expect(with_change_to_b.directly[0].name).to eql "b"
expect(with_change_to_b.directly[0].packages.keys).to eql %i[yarn bundler]
end

it "correctly reports transitively affected components" do
expect(with_change_to_b.transitively).to eq(
[
{
name: "a",
path: ["#{umbrella.path}/components/a"],
type: "Bundler",
},
{
name: "c",
path: ["#{umbrella.path}/components/c"],
type: "Bundler",
},
{
name: "d",
path: ["#{umbrella.path}/components/d"],
type: "Bundler",
},
{
name: "f",
path: ["#{umbrella.path}/components/f"],
type: "Yarn",
},
{
name: "g",
path: ["#{umbrella.path}/components/g"],
type: "Yarn",
},
{
name: "h",
path: ["#{umbrella.path}/components/h"],
type: "Yarn & Bundler",
},
{
name: "node_manifest",
path: ["#{umbrella.path}/node_manifest"],
type: "Yarn",
},
]
)
expect(with_change_to_b.transitively.size).to eql 7
expect(with_change_to_b.transitively[0].name).to eql "a"
expect(with_change_to_b.transitively[0].packages.keys).to eql %i[bundler]
expect(with_change_to_b.transitively[1].name).to eql "c"
expect(with_change_to_b.transitively[1].packages.keys).to eql %i[bundler]
expect(with_change_to_b.transitively[2].name).to eql "d"
expect(with_change_to_b.transitively[2].packages.keys).to eql %i[bundler]
expect(with_change_to_b.transitively[3].name).to eql "f"
expect(with_change_to_b.transitively[3].packages.keys).to eql %i[yarn]
expect(with_change_to_b.transitively[4].name).to eql "g"
expect(with_change_to_b.transitively[4].packages.keys).to eql %i[yarn]
expect(with_change_to_b.transitively[5].name).to eql "h"
expect(with_change_to_b.transitively[5].packages.keys).to eql %i[yarn bundler]
expect(with_change_to_b.transitively[6].name).to eql "node_manifest"
expect(with_change_to_b.transitively[6].packages.keys).to eql %i[yarn]
end

it "correctly reports test scripts" do
Expand All @@ -140,37 +104,19 @@
end

it "correctly reports directly affected components" do
expect(with_change_to_f.directly).to eq(
[
{
name: "f",
path: ["#{umbrella.path}/components/f"],
type: "Yarn",
},
]
)
expect(with_change_to_f.directly.size).to eql 1
expect(with_change_to_f.directly.first.name).to eql "f"
expect(with_change_to_f.directly.first.packages.keys).to eql %i[yarn]
end

it "correctly reports transitively affected components" do
expect(with_change_to_f.transitively).to eq(
[
{
name: "g",
path: ["#{umbrella.path}/components/g"],
type: "Yarn",
},
{
name: "h",
path: ["#{umbrella.path}/components/h"],
type: "Yarn & Bundler",
},
{
name: "node_manifest",
path: ["#{umbrella.path}/node_manifest"],
type: "Yarn",
},
]
)
expect(with_change_to_f.transitively.size).to eql 3
expect(with_change_to_f.transitively[0].name).to eql "g"
expect(with_change_to_f.transitively[0].packages.keys).to eql %i[yarn]
expect(with_change_to_f.transitively[1].name).to eql "h"
expect(with_change_to_f.transitively[1].packages.keys).to eql %i[yarn bundler]
expect(with_change_to_f.transitively[2].name).to eql "node_manifest"
expect(with_change_to_f.transitively[2].packages.keys).to eql %i[yarn]
end

it "correctly reports test scripts" do
Expand Down
4 changes: 2 additions & 2 deletions spec/cobra_commander/change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
change = CobraCommander::Change.new(umbrella, "json", "master", changes: ["#{umbrella.path}/components/a"])

expect { change.run! }.to output(<<~OUTPUT
{"changed_files":["#{umbrella.path}/components/a"],"directly_affected_components":[{"name":"a","path":["#{umbrella.path}/components/a"],"type":"Bundler"}],"transitively_affected_components":[],"test_scripts":["#{umbrella.path}/components/a/test.sh"],"component_names":["a"],"languages":{"ruby":true,"javascript":false}}
{"changed_files":["#{umbrella.path}/components/a"],"directly_affected_components":[{"name":"a","path":["#{umbrella.path}/components/a"],"type":["bundler"]}],"transitively_affected_components":[],"test_scripts":["#{umbrella.path}/components/a/test.sh"],"component_names":["a"],"languages":{"ruby":true,"javascript":false}}
OUTPUT
).to_stdout
end
Expand All @@ -39,7 +39,7 @@
change = CobraCommander::Change.new(umbrella, "json", "master", changes: ["#{umbrella.path}/components/e"])

expect { change.run! }.to output(<<~OUTPUT
{"changed_files":["#{umbrella.path}/components/e"],"directly_affected_components":[{"name":"e","path":["#{umbrella.path}/components/e"],"type":"Yarn"}],"transitively_affected_components":[{"name":"a","path":["#{umbrella.path}/components/a"],"type":"Bundler"},{"name":"b","path":["#{umbrella.path}/components/b"],"type":"Yarn & Bundler"},{"name":"c","path":["#{umbrella.path}/components/c"],"type":"Bundler"},{"name":"d","path":["#{umbrella.path}/components/d"],"type":"Bundler"},{"name":"f","path":["#{umbrella.path}/components/f"],"type":"Yarn"},{"name":"g","path":["#{umbrella.path}/components/g"],"type":"Yarn"},{"name":"h","path":["#{umbrella.path}/components/h"],"type":"Yarn & Bundler"},{"name":"node_manifest","path":["#{umbrella.path}/node_manifest"],"type":"Yarn"}],"test_scripts":["#{umbrella.path}/components/a/test.sh","#{umbrella.path}/components/b/test.sh","#{umbrella.path}/components/c/test.sh","#{umbrella.path}/components/d/test.sh","#{umbrella.path}/components/e/test.sh","#{umbrella.path}/components/f/test.sh","#{umbrella.path}/components/g/test.sh","#{umbrella.path}/components/h/test.sh","#{umbrella.path}/node_manifest/test.sh"],"component_names":["a","b","c","d","e","f","g","h","node_manifest"],"languages":{"ruby":true,"javascript":true}}
{"changed_files":["#{umbrella.path}/components/e"],"directly_affected_components":[{"name":"e","path":["#{umbrella.path}/components/e"],"type":["yarn"]}],"transitively_affected_components":[{"name":"a","path":["#{umbrella.path}/components/a"],"type":["bundler"]},{"name":"b","path":["#{umbrella.path}/components/b"],"type":["yarn","bundler"]},{"name":"c","path":["#{umbrella.path}/components/c"],"type":["bundler"]},{"name":"d","path":["#{umbrella.path}/components/d"],"type":["bundler"]},{"name":"f","path":["#{umbrella.path}/components/f"],"type":["yarn"]},{"name":"g","path":["#{umbrella.path}/components/g"],"type":["yarn"]},{"name":"h","path":["#{umbrella.path}/components/h"],"type":["yarn","bundler"]},{"name":"node_manifest","path":["#{umbrella.path}/node_manifest"],"type":["yarn"]}],"test_scripts":["#{umbrella.path}/components/a/test.sh","#{umbrella.path}/components/b/test.sh","#{umbrella.path}/components/c/test.sh","#{umbrella.path}/components/d/test.sh","#{umbrella.path}/components/e/test.sh","#{umbrella.path}/components/f/test.sh","#{umbrella.path}/components/g/test.sh","#{umbrella.path}/components/h/test.sh","#{umbrella.path}/node_manifest/test.sh"],"component_names":["a","b","c","d","e","f","g","h","node_manifest"],"languages":{"ruby":true,"javascript":true}}
OUTPUT
).to_stdout
end
Expand Down
8 changes: 4 additions & 4 deletions spec/cobra_commander/umbrella_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
end

describe ".resolve(component_root_path)" do
it "resolves the root component" do
expect(subject.resolve("/a/b").name).to eql "App"
end

it "resolves a component given the root path" do
expect(subject.resolve("/a/b/c/y").name).to eql "y"
end

it "resolves child paths" do
expect(subject.resolve("/a/b/c/z/app/models/mode.rb").name).to eql "z"
end

it "is nil when no component was found" do
expect(subject.resolve("/a/b/c/lol")).to be_nil
end
Expand Down