Skip to content

Commit

Permalink
Merge pull request #874 from joeydong/joeydong/code-coverage-targets
Browse files Browse the repository at this point in the history
Add `code_coverage_targets` getter/setters to TestAction
  • Loading branch information
dnkoutso committed Mar 15, 2022
2 parents 14c3954 + db1e5ca commit e9a88df
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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`.
[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

0 comments on commit e9a88df

Please sign in to comment.