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

Add code_coverage_targets getter/setters to TestAction #874

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
[Ben Yohay](https://github.com/byohay)
[#861](https://github.com/CocoaPods/Xcodeproj/pull/861)

* Add `code_coverage_targets` support for `TestAction`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, if possible please add two empty spsaces after this as it helps with markdown formatting.

[Joey Dong](https://github.com/joeydong)
[#874](https://github.com/CocoaPods/Xcodeproj/pull/874)

##### Bug Fixes

* Fix undefined method 'downcase' for `nil`
Expand Down
38 changes: 38 additions & 0 deletions lib/xcodeproj/scheme/test_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,44 @@ def command_line_arguments=(arguments)
arguments
end

# @return [Array<BuildableReference>]
# The list of BuildableReference (code coverage targets) associated with this Test Action
#
def code_coverage_targets
return [] unless @xml_element.elements['CodeCoverageTargets']

@xml_element.elements['CodeCoverageTargets'].get_elements('BuildableReference').map do |node|
BuildableReference.new(node)
end
end

# @param [Array<BuildableReference>] buildable_references
# Sets the list of BuildableReference (code coverage targets) associated with this Test Action
#
def code_coverage_targets=(buildable_references)
@xml_element.attributes['onlyGenerateCoverageForSpecifiedTargets'] = bool_to_string(true)

@xml_element.delete_element('CodeCoverageTargets')
coverage_targets_element = @xml_element.add_element('CodeCoverageTargets')
buildable_references.each do |reference|
coverage_targets_element.add_element(reference.xml_element)
end

code_coverage_targets
end

# @param [BuildableReference] buildable_reference
# Add a BuildableReference (code coverage target) to this Test Action
#
def add_code_coverage_target(buildable_reference)
@xml_element.attributes['onlyGenerateCoverageForSpecifiedTargets'] = bool_to_string(true)

coverage_targets_element = @xml_element.elements['CodeCoverageTargets'] || @xml_element.add_element('CodeCoverageTargets')
coverage_targets_element.add_element(buildable_reference.xml_element)

code_coverage_targets
end

#-------------------------------------------------------------------------#

class TestableReference < XMLElementWrapper
Expand Down
53 changes: 53 additions & 0 deletions spec/scheme/test_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,59 @@ module Xcodeproj
xml_out.should == "<EnvironmentVariables>\n<EnvironmentVariable key='a' value='1' isEnabled='YES'/>\n</EnvironmentVariables>"
end
end

it '#code_coverage_targets' do
project = Xcodeproj::Project.new('/foo/bar/baz.xcodeproj')
@sut.xml_element.add_element('CodeCoverageTargets')

target1 = project.new_target(:application, 'FooApp', :ios)
buildable_ref1 = XCScheme::BuildableReference.new(target1)
@sut.xml_element.elements['CodeCoverageTargets'].add_element(buildable_ref1.xml_element)

target2 = project.new_target(:application, 'FooApp', :ios)
buildable_ref2 = XCScheme::BuildableReference.new(target2)
@sut.xml_element.elements['CodeCoverageTargets'].add_element(buildable_ref2.xml_element)

@sut.code_coverage_targets.count.should == 2
@sut.code_coverage_targets.all? { |t| t.class.should == XCScheme::BuildableReference }
@sut.code_coverage_targets[0].xml_element.should == buildable_ref1.xml_element
@sut.code_coverage_targets[1].xml_element.should == buildable_ref2.xml_element
end

it '#code_coverage_targets=' do
project = Xcodeproj::Project.new('/foo/bar/baz.xcodeproj')

target1 = project.new_target(:application, 'FooApp', :ios)
buildable_ref1 = XCScheme::BuildableReference.new(target1)

target2 = project.new_target(:application, 'FooApp', :ios)
buildable_ref2 = XCScheme::BuildableReference.new(target2)

@sut.code_coverage_targets = [buildable_ref1, buildable_ref2]

@sut.xml_element.attributes['onlyGenerateCoverageForSpecifiedTargets'].should == 'YES'
@sut.code_coverage_targets.count.should == 2
@sut.code_coverage_targets.all? { |t| t.class.should == XCScheme::BuildableReference }
@sut.code_coverage_targets[0].xml_element.should == buildable_ref1.xml_element
@sut.code_coverage_targets[1].xml_element.should == buildable_ref2.xml_element
end

it '#add_code_coverage_target' do
project = Xcodeproj::Project.new('/foo/bar/baz.xcodeproj')

target1 = project.new_target(:application, 'FooApp', :ios)
buildable_ref1 = XCScheme::BuildableReference.new(target1)
@sut.add_code_coverage_target(buildable_ref1)

target2 = project.new_target(:application, 'FooApp', :ios)
buildable_ref2 = XCScheme::BuildableReference.new(target2)
@sut.add_code_coverage_target(buildable_ref2)

@sut.xml_element.attributes['onlyGenerateCoverageForSpecifiedTargets'].should == 'YES'
@sut.xml_element.elements['CodeCoverageTargets'].count.should == 2
@sut.xml_element.elements['CodeCoverageTargets/BuildableReference[1]'].should == buildable_ref1.xml_element
@sut.xml_element.elements['CodeCoverageTargets/BuildableReference[2]'].should == buildable_ref2.xml_element
end
end

describe XCScheme::TestAction::TestableReference do
Expand Down