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 support for Enable Code Coverage for all targets in scheme test action options #3111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions docs/bazel.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Creates a value for the [`scheme_autogeneration_config`](xcodeproj-scheme_autoge
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <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` |
| <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` |
| <a id="xcschemes.autogeneration_config-test"></a>test | Options to use for the test action.<br><br>Example:<br><br><pre><code class="language-starlark">xcodeproj(&#10; ...&#10; scheme_autogeneration_config = xcschemes.autogeneration_config(&#10; test = xcschemes.autogeneration.test(&#10; options = xcschemes.test_options(&#10; app_language = "en",&#10; app_region = "US",&#10; code_coverage = False,&#10; )&#10; )&#10; )&#10;)</code></pre> | `None` |

**RETURNS**

Expand Down Expand Up @@ -551,7 +551,7 @@ Defines the Test action.
## xcschemes.test_options

<pre>
xcschemes.test_options(<a href="#xcschemes.test_options-app_language">app_language</a>, <a href="#xcschemes.test_options-app_region">app_region</a>)
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>)
</pre>

Defines the test options for a custom scheme.
Expand All @@ -563,6 +563,7 @@ Defines the test options for a custom scheme.
| :------------- | :------------- | :------------- |
| <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` |
| <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` |
| <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` |


<a id="xcschemes.test_target"></a>
Expand Down
2 changes: 2 additions & 0 deletions examples/integration/xcodeproj_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ SCHEME_AUTOGENERATION_CONFIG = xcschemes.autogeneration_config(
options = xcschemes.test_options(
app_language = "en",
app_region = "US",
code_coverage = False,
),
),
)
Expand Down Expand Up @@ -247,6 +248,7 @@ XCSCHEMES = [
test_options = xcschemes.test_options(
app_language = "en",
app_region = "US",
code_coverage = False,
),
test_targets = [
"//iOSApp/Test/SwiftUnitTests:iOSAppSwiftUnitTests",
Expand Down
5 changes: 5 additions & 0 deletions test/internal/xcschemes/info_constructors_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def info_constructors_test_suite(name):
expected_info = struct(
app_language = "",
app_region = "",
code_coverage = "0",
),
)

Expand All @@ -281,12 +282,14 @@ def info_constructors_test_suite(name):
info = xcscheme_infos_testable.make_test_options(
app_language = "en",
app_region = "US",
code_coverage = "0",
),

# Expected
expected_info = struct(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
)

Expand Down Expand Up @@ -756,6 +759,7 @@ def info_constructors_test_suite(name):
options = xcscheme_infos_testable.make_test_options(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
test_targets = [
xcscheme_infos_testable.make_test_target("tt 9"),
Expand Down Expand Up @@ -793,6 +797,7 @@ def info_constructors_test_suite(name):
options = xcscheme_infos_testable.make_test_options(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
test_targets = [
xcscheme_infos_testable.make_test_target("tt 9"),
Expand Down
2 changes: 2 additions & 0 deletions test/internal/xcschemes/infos_from_json_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ def infos_from_json_test_suite(name):
options = struct(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
test_targets = [
"tt 1 label",
Expand Down Expand Up @@ -1048,6 +1049,7 @@ def infos_from_json_test_suite(name):
options = xcscheme_infos_testable.make_test_options(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
test_targets = [
xcscheme_infos_testable.make_test_target("sim tt 1"),
Expand Down
1 change: 1 addition & 0 deletions test/internal/xcschemes/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def _dict_to_test_options_info(d):
return struct(
app_language = d["app_language"],
app_region = d["app_region"],
code_coverage = d["code_coverage"],
)

def _dict_to_launch_target_info(d):
Expand Down
7 changes: 7 additions & 0 deletions test/internal/xcschemes/write_schemes_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ def write_schemes_test_suite(name):
options = xcscheme_infos_testable.make_test_options(
app_language = "en",
app_region = "US",
code_coverage = "0",
),
test_targets = [
xcscheme_infos_testable.make_test_target(
Expand Down Expand Up @@ -1090,6 +1091,8 @@ def write_schemes_test_suite(name):
"",
# - test - app_region
"",
# - test - code_coverage
"0",
# - test - xcodeConfiguration
"",
# - run - buildTargets
Expand Down Expand Up @@ -1191,6 +1194,8 @@ def write_schemes_test_suite(name):
"en",
# - test - app_region
"US",
# - test - code_coverage
"0",
# - test - xcodeConfiguration
"Test",
# - run - buildTargets
Expand Down Expand Up @@ -1320,6 +1325,8 @@ def write_schemes_test_suite(name):
"",
# - test - app_region
"",
# - test - code_coverage
"0",
# - test - xcodeConfiguration
"",
# - run - buildTargets
Expand Down
7 changes: 7 additions & 0 deletions tools/generators/lib/XCScheme/src/CreateTestAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public struct CreateTestAction {
public func callAsFunction(
appLanguage: String?,
appRegion: String?,
codeCoverage: Bool,
buildConfiguration: String,
commandLineArguments: [CommandLineArgument],
enableAddressSanitizer: Bool,
Expand All @@ -29,6 +30,7 @@ public struct CreateTestAction {
return callable(
/*appLanguage:*/ appLanguage,
/*appRegion:*/ appRegion,
/*codeCoverage:*/ codeCoverage,
/*buildConfiguration:*/ buildConfiguration,
/*commandLineArguments:*/ commandLineArguments,
/*enableAddressSanitizer:*/ enableAddressSanitizer,
Expand All @@ -52,6 +54,7 @@ extension CreateTestAction {
public typealias Callable = (
_ appLanguage: String?,
_ appRegion: String?,
_ codeCoverage: Bool,
_ buildConfiguration: String,
_ commandLineArguments: [CommandLineArgument],
_ enableAddressSanitizer: Bool,
Expand All @@ -70,6 +73,7 @@ extension CreateTestAction {
public static func defaultCallable(
appLanguage: String?,
appRegion: String?,
codeCoverage: Bool,
buildConfiguration: String,
commandLineArguments: [CommandLineArgument],
enableAddressSanitizer: Bool,
Expand Down Expand Up @@ -118,6 +122,9 @@ buildConfiguration = "\#(buildConfiguration)"
if let appRegion {
components.append("region = \"\(appRegion)\"")
}
if codeCoverage {
components.append("codeCoverageEnabled = \"YES\"")
}

let macroExpansion: String
if let macroReference {
Expand Down
35 changes: 35 additions & 0 deletions tools/generators/lib/XCScheme/test/CreateTestActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,45 @@ final class CreateTestActionTests: XCTestCase {

XCTAssertNoDifference(action, expectedAction)
}

func test_codeCoverageEnable() {
// Arrange

let buildConfiguration = "Release"
let useLaunchSchemeArgsEnv = false

let expectedAction = #"""
<TestAction
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "NO"
language = "en"
region = "US">
<Testables>
</Testables>
</TestAction>
"""#

// Act

let action = createTestActionWithDefaults(
appLanguage: "en",
appRegion: "US",
buildConfiguration: buildConfiguration,
useLaunchSchemeArgsEnv: useLaunchSchemeArgsEnv
)

// Assert

XCTAssertNoDifference(action, expectedAction)
}
}

private func createTestActionWithDefaults(
appLanguage: String? = nil,
appRegion: String? = nil,
codeCoverage: Bool = false,
buildConfiguration: String,
commandLineArguments: [CommandLineArgument] = [],
enableAddressSanitizer: Bool = false,
Expand All @@ -439,6 +473,7 @@ private func createTestActionWithDefaults(
return CreateTestAction.defaultCallable(
appLanguage: appLanguage,
appRegion: appRegion,
codeCoverage: codeCoverage,
buildConfiguration: buildConfiguration,
commandLineArguments: commandLineArguments,
enableAddressSanitizer: enableAddressSanitizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ToolCommon
struct AutogenerationConfigArguments {
let appLanguage: String?
let appRegion: String?
let codeCoverage: Bool
let schemeNameExcludePatterns: [String]

static func parse(
Expand All @@ -21,6 +22,11 @@ struct AutogenerationConfigArguments {
as: String?.self,
in: url
)
let codeCoverage = try rawArgs.consumeArg(
"code-coverage",
as: Bool.self,
in: url
)
let schemeNameExcludePatterns = try rawArgs.consumeArgs(
"scheme-name-exclude-patterns",
in: url
Expand All @@ -29,6 +35,7 @@ struct AutogenerationConfigArguments {
return AutogenerationConfigArguments(
appLanguage: appLanguage,
appRegion: appRegion,
codeCoverage: codeCoverage,
schemeNameExcludePatterns: schemeNameExcludePatterns
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ set
as: String?.self,
in: url
)
let codeCoverage = try consumeArg(
"test-code-coverage",
as: Bool.self,
in: url
)
let xcodeConfiguration = try consumeArg(
"test-xcode-configuration",
as: String?.self,
Expand Down Expand Up @@ -719,7 +724,8 @@ set
enableThreadPerformanceChecker: enableThreadPerformanceChecker,
environmentVariables: environmentVariables,
options: .init(appLanguage: appLanguage,
appRegion: appRegion),
appRegion: appRegion,
codeCoverage: codeCoverage),
testTargets: testTargets,
useRunArgsAndEnv: useRunArgsAndEnv,
xcodeConfiguration: xcodeConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ extension Generator.CreateScheme {
testAction: createTestAction(
appLanguage: schemeInfo.test.options?.appLanguage,
appRegion: schemeInfo.test.options?.appRegion,
codeCoverage: schemeInfo.test.options?.codeCoverage ?? false,
buildConfiguration: schemeInfo.test.xcodeConfiguration ??
defaultXcodeConfiguration,
commandLineArguments: schemeInfo.test.commandLineArguments,
Expand Down
3 changes: 2 additions & 1 deletion tools/generators/xcschemes/src/Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ struct Generator {
targetsByID: targetsByID,
targetsByKey: targetsByKey,
testOptions: .init(appLanguage: autogenerationConfigArguments.appLanguage,
appRegion: autogenerationConfigArguments.appRegion)
appRegion: autogenerationConfigArguments.appRegion,
codeCoverage: autogenerationConfigArguments.codeCoverage)
)

let filteredAutomaticSchemeInfos = try automaticSchemeInfos.filter { scheme in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct SchemeInfo: Equatable {
struct Options: Equatable {
let appLanguage: String?
let appRegion: String?
let codeCoverage: Bool
}

let buildTargets: [Target]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ final class CreateAutomaticSchemeInfoTests: XCTestCase {
enableMainThreadChecker: false,
enableThreadPerformanceChecker: false,
environmentVariables: baseEnvironmentVariables,
options: .init(appLanguage: "en", appRegion: "US"),
options: .init(appLanguage: "en", appRegion: "US", codeCoverage: false),
testTargets: [.init(target: test, isEnabled: true)],
useRunArgsAndEnv: false,
xcodeConfiguration: nil
Expand Down Expand Up @@ -761,7 +761,7 @@ final class CreateAutomaticSchemeInfoTests: XCTestCase {

let schemeInfo = try createAutomaticSchemeInfoWithDefaults(
target: test,
testOptions: .init(appLanguage: "en", appRegion: "US")
testOptions: .init(appLanguage: "en", appRegion: "US", codeCoverage: false)
)

// Assert
Expand Down
2 changes: 1 addition & 1 deletion xcodeproj/internal/xcodeproj_incremental_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _write_autogeneration_config_file(
args = actions.args()
args.set_param_file_format("multiline")

args.add_all(config.get("test_options", ["", ""]))
args.add_all(config.get("test_options", ["", "", False]))
args.add_all(
config.get("scheme_name_exclude_patterns", []),
omit_if_empty = False,
Expand Down
5 changes: 4 additions & 1 deletion xcodeproj/internal/xcschemes/xcscheme_infos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ def _make_diagnostics(
def _make_test_options(
*,
app_region = EMPTY_STRING,
app_language = EMPTY_STRING):
app_language = EMPTY_STRING,
code_coverage = FALSE_ARG):
return struct(
app_region = app_region,
app_language = app_language,
code_coverage = code_coverage,
)

def _make_launch_target(
Expand Down Expand Up @@ -312,6 +314,7 @@ def _options_info_from_dict(options):
return _make_test_options(
app_region = options["app_region"],
app_language = options["app_language"],
code_coverage = options["code_coverage"],
)

def _env_infos_from_dict(env):
Expand Down
Loading