Skip to content

Commit

Permalink
apply change: Fix adding component with non default simple attributes.
Browse files Browse the repository at this point in the history
When adding a component with simple attributes, these attributes might
have default values that aren't `nil`. Previously, when such a thing
occurred it would result in a `MergeError`, because the removed change
was assumed to be `nil` since the component was just added, but in
practice it would be set to the default value.
The solution is to set the removed change value to the default value.
  • Loading branch information
byohay committed Jun 4, 2022
1 parent f210942 commit 9283ba7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/kintsugi/apply_change_to_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,10 @@ def add_attributes_to_component(component, change, change_path, ignore_keys: [])

attribute_name = attribute_name_from_change_name(change_name)
if simple_attribute?(component, attribute_name)
apply_change_to_simple_attribute(component, attribute_name, {added: change_value})
apply_change_to_simple_attribute(component, attribute_name, {
added: change_value,
removed: simple_attribute_default_value(component, attribute_name)
})
next
end

Expand All @@ -694,6 +697,12 @@ def add_attributes_to_component(component, change, change_path, ignore_keys: [])
end
end

def simple_attribute_default_value(component, attribute_name)
component.simple_attributes.find do |attribute|
attribute.name == attribute_name
end.default_value
end

def find_file(project, file_reference_change, file_filter: ->(_) { true })
file_references = project.files.select do |file_reference|
file_reference.path == file_reference_change["path"] && file_filter.call(file_reference)
Expand Down
14 changes: 14 additions & 0 deletions spec/kintsugi_apply_change_to_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,20 @@
expect(base_project).to be_equivalent_to_project(theirs_project)
end

it "adds build phase with a simple attribute value that has non nil default" do
theirs_project = create_copy_of_project(base_project.path, "theirs")

theirs_project.targets[0].new_shell_script_build_phase("bar")
theirs_project.targets[0].build_phases.last.shell_script = "Other value"

changes_to_apply = get_diff(theirs_project, base_project)

described_class.apply_change_to_project(base_project, changes_to_apply)
base_project.save

expect(base_project).to be_equivalent_to_project(theirs_project)
end

it "removes build phase" do
base_project.targets[0].new_shell_script_build_phase("bar")

Expand Down

0 comments on commit 9283ba7

Please sign in to comment.