Skip to content

Commit 8d3e353

Browse files
committed
add code coverage option on test_options
Signed-off-by: Lucas Romano <[email protected]>
1 parent f4fa898 commit 8d3e353

18 files changed

+101
-9
lines changed

docs/bazel.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Creates a value for the [`scheme_autogeneration_config`](xcodeproj-scheme_autoge
280280
| Name | Description | Default Value |
281281
| :------------- | :------------- | :------------- |
282282
| <a id="xcschemes.autogeneration_config-scheme_name_exclude_patterns"></a>scheme_name_exclude_patterns | A `list` of regex patterns used to skip creating matching autogenerated schemes.<br><br>Example:<br><br><pre><code class="language-starlark">xcodeproj(&#10; ...&#10; scheme_name_exclude_patterns = xcschemes.autogeneration_config(&#10; scheme_name_exclude_patterns = [&#10; ".*somePattern.*",&#10; "^AnotherPattern.*",&#10; ],&#10; ),&#10;)</code></pre> | `None` |
283-
| <a id="xcschemes.autogeneration_config-test"></a>test | Options to use for the test action.<br><br>Example:<br><br>```starlark xcodeproj( ... scheme_autogeneration_config = xcschemes.autogeneration_config( test = xcschemes.autogeneration.test( options = xcschemes.test_options( app_language = "en", app_region = "US", ) ) ) ) | `None` |
283+
| <a id="xcschemes.autogeneration_config-test"></a>test | Options to use for the test action.<br><br>Example:<br><br>```starlark xcodeproj( ... scheme_autogeneration_config = xcschemes.autogeneration_config( test = xcschemes.autogeneration.test( options = xcschemes.test_options( app_language = "en", app_region = "US", code_coverage = False, ) ) ) ) | `None` |
284284

285285
**RETURNS**
286286

@@ -551,7 +551,7 @@ Defines the Test action.
551551
## xcschemes.test_options
552552

553553
<pre>
554-
xcschemes.test_options(<a href="#xcschemes.test_options-app_language">app_language</a>, <a href="#xcschemes.test_options-app_region">app_region</a>)
554+
xcschemes.test_options(<a href="#xcschemes.test_options-app_language">app_language</a>, <a href="#xcschemes.test_options-app_region">app_region</a>, <a href="#xcschemes.test_options-code_coverage">code_coverage</a>)
555555
</pre>
556556

557557
Defines the test options for a custom scheme.
@@ -563,6 +563,7 @@ Defines the test options for a custom scheme.
563563
| :------------- | :------------- | :------------- |
564564
| <a id="xcschemes.test_options-app_language"></a>app_language | Language to set in scheme.<br><br>Defaults to system settings if not set. | `None` |
565565
| <a id="xcschemes.test_options-app_region"></a>app_region | Region to set in scheme.<br><br>Defaults to system settings if not set. | `None` |
566+
| <a id="xcschemes.test_options-code_coverage"></a>code_coverage | Whether to enable code coverage.<br><br>If `True`, code coverage will be enabled. | `False` |
566567

567568

568569
<a id="xcschemes.test_target"></a>

examples/integration/xcodeproj_targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ SCHEME_AUTOGENERATION_CONFIG = xcschemes.autogeneration_config(
155155
options = xcschemes.test_options(
156156
app_language = "en",
157157
app_region = "US",
158+
code_coverage = False,
158159
),
159160
),
160161
)
@@ -247,6 +248,7 @@ XCSCHEMES = [
247248
test_options = xcschemes.test_options(
248249
app_language = "en",
249250
app_region = "US",
251+
code_coverage = False,
250252
),
251253
test_targets = [
252254
"//iOSApp/Test/SwiftUnitTests:iOSAppSwiftUnitTests",

test/internal/xcschemes/info_constructors_tests.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def info_constructors_test_suite(name):
271271
expected_info = struct(
272272
app_language = "",
273273
app_region = "",
274+
code_coverage = "0",
274275
),
275276
)
276277

@@ -281,12 +282,14 @@ def info_constructors_test_suite(name):
281282
info = xcscheme_infos_testable.make_test_options(
282283
app_language = "en",
283284
app_region = "US",
285+
code_coverage = "0",
284286
),
285287

286288
# Expected
287289
expected_info = struct(
288290
app_language = "en",
289291
app_region = "US",
292+
code_coverage = "0",
290293
),
291294
)
292295

@@ -756,6 +759,7 @@ def info_constructors_test_suite(name):
756759
options = xcscheme_infos_testable.make_test_options(
757760
app_language = "en",
758761
app_region = "US",
762+
code_coverage = "0",
759763
),
760764
test_targets = [
761765
xcscheme_infos_testable.make_test_target("tt 9"),
@@ -793,6 +797,7 @@ def info_constructors_test_suite(name):
793797
options = xcscheme_infos_testable.make_test_options(
794798
app_language = "en",
795799
app_region = "US",
800+
code_coverage = "0",
796801
),
797802
test_targets = [
798803
xcscheme_infos_testable.make_test_target("tt 9"),

test/internal/xcschemes/infos_from_json_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ def infos_from_json_test_suite(name):
956956
options = struct(
957957
app_language = "en",
958958
app_region = "US",
959+
code_coverage = "0",
959960
),
960961
test_targets = [
961962
"tt 1 label",
@@ -1048,6 +1049,7 @@ def infos_from_json_test_suite(name):
10481049
options = xcscheme_infos_testable.make_test_options(
10491050
app_language = "en",
10501051
app_region = "US",
1052+
code_coverage = "0",
10511053
),
10521054
test_targets = [
10531055
xcscheme_infos_testable.make_test_target("sim tt 1"),

test/internal/xcschemes/utils.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def _dict_to_test_options_info(d):
2424
return struct(
2525
app_language = d["app_language"],
2626
app_region = d["app_region"],
27+
code_coverage = d["code_coverage"],
2728
)
2829

2930
def _dict_to_launch_target_info(d):

test/internal/xcschemes/write_schemes_tests.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ def write_schemes_test_suite(name):
614614
options = xcscheme_infos_testable.make_test_options(
615615
app_language = "en",
616616
app_region = "US",
617+
code_coverage = "0",
617618
),
618619
test_targets = [
619620
xcscheme_infos_testable.make_test_target(
@@ -1090,6 +1091,8 @@ def write_schemes_test_suite(name):
10901091
"",
10911092
# - test - app_region
10921093
"",
1094+
# - test - code_coverage
1095+
"0",
10931096
# - test - xcodeConfiguration
10941097
"",
10951098
# - run - buildTargets
@@ -1191,6 +1194,8 @@ def write_schemes_test_suite(name):
11911194
"en",
11921195
# - test - app_region
11931196
"US",
1197+
# - test - code_coverage
1198+
"0",
11941199
# - test - xcodeConfiguration
11951200
"Test",
11961201
# - run - buildTargets
@@ -1320,6 +1325,8 @@ def write_schemes_test_suite(name):
13201325
"",
13211326
# - test - app_region
13221327
"",
1328+
# - test - code_coverage
1329+
"0",
13231330
# - test - xcodeConfiguration
13241331
"",
13251332
# - run - buildTargets

tools/generators/lib/XCScheme/src/CreateTestAction.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct CreateTestAction {
1212
public func callAsFunction(
1313
appLanguage: String?,
1414
appRegion: String?,
15+
codeCoverage: Bool,
1516
buildConfiguration: String,
1617
commandLineArguments: [CommandLineArgument],
1718
enableAddressSanitizer: Bool,
@@ -29,6 +30,7 @@ public struct CreateTestAction {
2930
return callable(
3031
/*appLanguage:*/ appLanguage,
3132
/*appRegion:*/ appRegion,
33+
/*codeCoverage:*/ codeCoverage,
3234
/*buildConfiguration:*/ buildConfiguration,
3335
/*commandLineArguments:*/ commandLineArguments,
3436
/*enableAddressSanitizer:*/ enableAddressSanitizer,
@@ -52,6 +54,7 @@ extension CreateTestAction {
5254
public typealias Callable = (
5355
_ appLanguage: String?,
5456
_ appRegion: String?,
57+
_ codeCoverage: Bool,
5558
_ buildConfiguration: String,
5659
_ commandLineArguments: [CommandLineArgument],
5760
_ enableAddressSanitizer: Bool,
@@ -70,6 +73,7 @@ extension CreateTestAction {
7073
public static func defaultCallable(
7174
appLanguage: String?,
7275
appRegion: String?,
76+
codeCoverage: Bool,
7377
buildConfiguration: String,
7478
commandLineArguments: [CommandLineArgument],
7579
enableAddressSanitizer: Bool,
@@ -118,6 +122,9 @@ buildConfiguration = "\#(buildConfiguration)"
118122
if let appRegion {
119123
components.append("region = \"\(appRegion)\"")
120124
}
125+
if codeCoverage {
126+
components.append("codeCoverageEnabled = \"YES\"")
127+
}
121128

122129
let macroExpansion: String
123130
if let macroReference {

tools/generators/lib/XCScheme/test/CreateTestActionTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,45 @@ final class CreateTestActionTests: XCTestCase {
417417

418418
XCTAssertNoDifference(action, expectedAction)
419419
}
420+
421+
func test_codeCoverageEnable() {
422+
// Arrange
423+
424+
let buildConfiguration = "Release"
425+
let useLaunchSchemeArgsEnv = false
426+
427+
let expectedAction = #"""
428+
<TestAction
429+
buildConfiguration = "Release"
430+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
431+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
432+
shouldUseLaunchSchemeArgsEnv = "NO"
433+
language = "en"
434+
region = "US">
435+
<Testables>
436+
</Testables>
437+
</TestAction>
438+
"""#
439+
440+
// Act
441+
442+
let action = createTestActionWithDefaults(
443+
appLanguage: "en",
444+
appRegion: "US",
445+
buildConfiguration: buildConfiguration,
446+
useLaunchSchemeArgsEnv: useLaunchSchemeArgsEnv
447+
)
448+
449+
// Assert
450+
451+
XCTAssertNoDifference(action, expectedAction)
452+
}
420453
}
421454

422455
private func createTestActionWithDefaults(
423456
appLanguage: String? = nil,
424457
appRegion: String? = nil,
458+
codeCoverage: Bool = false,
425459
buildConfiguration: String,
426460
commandLineArguments: [CommandLineArgument] = [],
427461
enableAddressSanitizer: Bool = false,
@@ -439,6 +473,7 @@ private func createTestActionWithDefaults(
439473
return CreateTestAction.defaultCallable(
440474
appLanguage: appLanguage,
441475
appRegion: appRegion,
476+
codeCoverage: codeCoverage,
442477
buildConfiguration: buildConfiguration,
443478
commandLineArguments: commandLineArguments,
444479
enableAddressSanitizer: enableAddressSanitizer,

tools/generators/xcschemes/src/Generator/AutogenerationConfigArguments.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ToolCommon
44
struct AutogenerationConfigArguments {
55
let appLanguage: String?
66
let appRegion: String?
7+
let codeCoverage: Bool
78
let schemeNameExcludePatterns: [String]
89

910
static func parse(
@@ -21,6 +22,11 @@ struct AutogenerationConfigArguments {
2122
as: String?.self,
2223
in: url
2324
)
25+
let codeCoverage = try rawArgs.consumeArg(
26+
"code-coverage",
27+
as: Bool.self,
28+
in: url
29+
)
2430
let schemeNameExcludePatterns = try rawArgs.consumeArgs(
2531
"scheme-name-exclude-patterns",
2632
in: url
@@ -29,6 +35,7 @@ struct AutogenerationConfigArguments {
2935
return AutogenerationConfigArguments(
3036
appLanguage: appLanguage,
3137
appRegion: appRegion,
38+
codeCoverage: codeCoverage,
3239
schemeNameExcludePatterns: schemeNameExcludePatterns
3340
)
3441
}

tools/generators/xcschemes/src/Generator/CreateCustomSchemeInfos.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ set
639639
as: String?.self,
640640
in: url
641641
)
642+
let codeCoverage = try consumeArg(
643+
"test-code-coverage",
644+
as: Bool.self,
645+
in: url
646+
)
642647
let xcodeConfiguration = try consumeArg(
643648
"test-xcode-configuration",
644649
as: String?.self,
@@ -719,7 +724,8 @@ set
719724
enableThreadPerformanceChecker: enableThreadPerformanceChecker,
720725
environmentVariables: environmentVariables,
721726
options: .init(appLanguage: appLanguage,
722-
appRegion: appRegion),
727+
appRegion: appRegion,
728+
codeCoverage: codeCoverage),
723729
testTargets: testTargets,
724730
useRunArgsAndEnv: useRunArgsAndEnv,
725731
xcodeConfiguration: xcodeConfiguration

tools/generators/xcschemes/src/Generator/CreateScheme.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ extension Generator.CreateScheme {
362362
testAction: createTestAction(
363363
appLanguage: schemeInfo.test.options?.appLanguage,
364364
appRegion: schemeInfo.test.options?.appRegion,
365+
codeCoverage: schemeInfo.test.options?.codeCoverage ?? false,
365366
buildConfiguration: schemeInfo.test.xcodeConfiguration ??
366367
defaultXcodeConfiguration,
367368
commandLineArguments: schemeInfo.test.commandLineArguments,

tools/generators/xcschemes/src/Generator/Generator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ struct Generator {
5858
targetsByID: targetsByID,
5959
targetsByKey: targetsByKey,
6060
testOptions: .init(appLanguage: autogenerationConfigArguments.appLanguage,
61-
appRegion: autogenerationConfigArguments.appRegion)
61+
appRegion: autogenerationConfigArguments.appRegion,
62+
codeCoverage: autogenerationConfigArguments.codeCoverage)
6263
)
6364

6465
let filteredAutomaticSchemeInfos = try automaticSchemeInfos.filter { scheme in

tools/generators/xcschemes/src/Generator/SchemeInfo.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct SchemeInfo: Equatable {
3838
struct Options: Equatable {
3939
let appLanguage: String?
4040
let appRegion: String?
41+
let codeCoverage: Bool
4142
}
4243

4344
let buildTargets: [Target]

tools/generators/xcschemes/test/CreateAutomaticSchemeInfoTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ final class CreateAutomaticSchemeInfoTests: XCTestCase {
727727
enableMainThreadChecker: false,
728728
enableThreadPerformanceChecker: false,
729729
environmentVariables: baseEnvironmentVariables,
730-
options: .init(appLanguage: "en", appRegion: "US"),
730+
options: .init(appLanguage: "en", appRegion: "US", codeCoverage: false),
731731
testTargets: [.init(target: test, isEnabled: true)],
732732
useRunArgsAndEnv: false,
733733
xcodeConfiguration: nil
@@ -761,7 +761,7 @@ final class CreateAutomaticSchemeInfoTests: XCTestCase {
761761

762762
let schemeInfo = try createAutomaticSchemeInfoWithDefaults(
763763
target: test,
764-
testOptions: .init(appLanguage: "en", appRegion: "US")
764+
testOptions: .init(appLanguage: "en", appRegion: "US", codeCoverage: false)
765765
)
766766

767767
// Assert

xcodeproj/internal/xcodeproj_incremental_rule.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _write_autogeneration_config_file(
219219
args = actions.args()
220220
args.set_param_file_format("multiline")
221221

222-
args.add_all(config.get("test_options", ["", ""]))
222+
args.add_all(config.get("test_options", ["", "", False]))
223223
args.add_all(
224224
config.get("scheme_name_exclude_patterns", []),
225225
omit_if_empty = False,

xcodeproj/internal/xcschemes/xcscheme_infos.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def _make_diagnostics(
5252
def _make_test_options(
5353
*,
5454
app_region = EMPTY_STRING,
55-
app_language = EMPTY_STRING):
55+
app_language = EMPTY_STRING,
56+
code_coverage = FALSE_ARG):
5657
return struct(
5758
app_region = app_region,
5859
app_language = app_language,
60+
code_coverage = code_coverage,
5961
)
6062

6163
def _make_launch_target(
@@ -312,6 +314,7 @@ def _options_info_from_dict(options):
312314
return _make_test_options(
313315
app_region = options["app_region"],
314316
app_language = options["app_language"],
317+
code_coverage = options["code_coverage"],
315318
)
316319

317320
def _env_infos_from_dict(env):

0 commit comments

Comments
 (0)