Skip to content

Commit fecf206

Browse files
Add collect_code_coverage attribute to tests (#2705)
Code coverage is only collected when `True` (the default). This allows individual targets to opt out of code coverage collection, which can be useful in some cases, like UI tests. Signed-off-by: Brentley Jones <[email protected]>
1 parent 9e791e7 commit fecf206

9 files changed

+55
-37
lines changed

apple/internal/rule_factory.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ load(
4040
# Returns the common set of rule attributes to support Apple test rules.
4141
# TODO(b/246990309): Move _COMMON_TEST_ATTRS to rule attrs in a follow up CL.
4242
_COMMON_TEST_ATTRS = {
43+
"collect_code_coverage": attr.bool(
44+
doc = """
45+
Whether to collect code coverage for this test if `--collect_code_coverage=yes`.
46+
""",
47+
default = True,
48+
),
4349
"data": attr.label_list(
4450
allow_files = True,
4551
default = [],

apple/internal/testing/apple_test_rule_support.bzl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,16 @@ def _apple_test_rule_impl(*, ctx, requires_dossiers, test_type):
327327
if test_host_dossier:
328328
direct_runfiles.append(test_host_dossier)
329329

330-
if ctx.file.test_coverage_manifest:
331-
direct_runfiles.append(ctx.file.test_coverage_manifest)
332-
333330
test_host_artifact = test_bundle_target[AppleTestInfo].test_host
334331
if test_host_artifact:
335332
direct_runfiles.append(test_host_artifact)
336333

337-
if ctx.configuration.coverage_enabled:
334+
test_coverage_manifest = None
335+
if ctx.configuration.coverage_enabled and ctx.attr.collect_code_coverage:
336+
test_coverage_manifest = ctx.file.test_coverage_manifest
337+
if test_coverage_manifest:
338+
direct_runfiles.append(test_coverage_manifest)
339+
338340
apple_coverage_support_files = ctx.attr._apple_coverage_support.files
339341
covered_binaries = test_bundle_target[_CoverageFilesInfo].covered_binaries
340342

@@ -358,7 +360,7 @@ def _apple_test_rule_impl(*, ctx, requires_dossiers, test_type):
358360
substitutions = _get_template_substitutions(
359361
test_bundle = test_bundle,
360362
test_bundle_dossier = test_bundle_dossier,
361-
test_coverage_manifest = ctx.file.test_coverage_manifest,
363+
test_coverage_manifest = test_coverage_manifest,
362364
test_env_inherit = ctx.attr.env_inherit,
363365
test_environment = test_environment,
364366
test_filter = ctx.attr.test_filter,

apple/testing/default_runner/ios_test_runner.template.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ if [[ "$test_exit_code" -ne 0 ]]; then
288288
exit "$test_exit_code"
289289
fi
290290

291-
if [[ "${COVERAGE:-}" -ne 1 ]]; then
291+
if [[ "${COVERAGE:-}" -ne 1 || "${APPLE_COVERAGE:-}" -ne 1 ]]; then
292292
# Normal tests run without coverage
293293
exit 0
294294
fi

apple/testing/default_runner/ios_xctestrun_runner.template.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ then
635635
exit 1
636636
fi
637637

638-
if [[ "${COVERAGE:-}" -ne 1 ]]; then
638+
if [[ "${COVERAGE:-}" -ne 1 || "${APPLE_COVERAGE:-}" -ne 1 ]]; then
639639
# Normal tests run without coverage
640640
exit 0
641641
fi

doc/rules-ios.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ Outputs:
566566
## ios_ui_test
567567

568568
<pre>
569-
ios_ui_test(<a href="#ios_ui_test-name">name</a>, <a href="#ios_ui_test-deps">deps</a>, <a href="#ios_ui_test-data">data</a>, <a href="#ios_ui_test-bundle_name">bundle_name</a>, <a href="#ios_ui_test-env">env</a>, <a href="#ios_ui_test-env_inherit">env_inherit</a>, <a href="#ios_ui_test-minimum_deployment_os_version">minimum_deployment_os_version</a>,
570-
<a href="#ios_ui_test-minimum_os_version">minimum_os_version</a>, <a href="#ios_ui_test-platform_type">platform_type</a>, <a href="#ios_ui_test-runner">runner</a>, <a href="#ios_ui_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#ios_ui_test-test_filter">test_filter</a>, <a href="#ios_ui_test-test_host">test_host</a>,
571-
<a href="#ios_ui_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
569+
ios_ui_test(<a href="#ios_ui_test-name">name</a>, <a href="#ios_ui_test-deps">deps</a>, <a href="#ios_ui_test-data">data</a>, <a href="#ios_ui_test-bundle_name">bundle_name</a>, <a href="#ios_ui_test-collect_code_coverage">collect_code_coverage</a>, <a href="#ios_ui_test-env">env</a>, <a href="#ios_ui_test-env_inherit">env_inherit</a>,
570+
<a href="#ios_ui_test-minimum_deployment_os_version">minimum_deployment_os_version</a>, <a href="#ios_ui_test-minimum_os_version">minimum_os_version</a>, <a href="#ios_ui_test-platform_type">platform_type</a>, <a href="#ios_ui_test-runner">runner</a>,
571+
<a href="#ios_ui_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#ios_ui_test-test_filter">test_filter</a>, <a href="#ios_ui_test-test_host">test_host</a>, <a href="#ios_ui_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
572572
</pre>
573573

574574
iOS UI Test rule.
@@ -594,6 +594,7 @@ of the attributes inherited by all test rules, please check the
594594
| <a id="ios_ui_test-deps"></a>deps | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
595595
| <a id="ios_ui_test-data"></a>data | Files to be made available to the test during its execution. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
596596
| <a id="ios_ui_test-bundle_name"></a>bundle_name | The desired name of the bundle (without the extension). If this attribute is not set, then the name of the target will be used instead. | String | optional | `""` |
597+
| <a id="ios_ui_test-collect_code_coverage"></a>collect_code_coverage | Whether to collect code coverage for this test if `--collect_code_coverage=yes`. | Boolean | optional | `True` |
597598
| <a id="ios_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. The values of the dictionary are subject to "Make" variable expansion. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
598599
| <a id="ios_ui_test-env_inherit"></a>env_inherit | List of environment variables to inherit from the external environment. | List of strings | optional | `[]` |
599600
| <a id="ios_ui_test-minimum_deployment_os_version"></a>minimum_deployment_os_version | A required string indicating the minimum deployment OS version supported by the target, represented as a dotted version number (for example, "9.0"). This is different from `minimum_os_version`, which is effective at compile time. Ensure version specific APIs are guarded with `available` clauses. | String | optional | `""` |
@@ -611,9 +612,9 @@ of the attributes inherited by all test rules, please check the
611612
## ios_unit_test
612613

613614
<pre>
614-
ios_unit_test(<a href="#ios_unit_test-name">name</a>, <a href="#ios_unit_test-deps">deps</a>, <a href="#ios_unit_test-data">data</a>, <a href="#ios_unit_test-bundle_name">bundle_name</a>, <a href="#ios_unit_test-env">env</a>, <a href="#ios_unit_test-env_inherit">env_inherit</a>, <a href="#ios_unit_test-minimum_deployment_os_version">minimum_deployment_os_version</a>,
615-
<a href="#ios_unit_test-minimum_os_version">minimum_os_version</a>, <a href="#ios_unit_test-platform_type">platform_type</a>, <a href="#ios_unit_test-runner">runner</a>, <a href="#ios_unit_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#ios_unit_test-test_filter">test_filter</a>,
616-
<a href="#ios_unit_test-test_host">test_host</a>, <a href="#ios_unit_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
615+
ios_unit_test(<a href="#ios_unit_test-name">name</a>, <a href="#ios_unit_test-deps">deps</a>, <a href="#ios_unit_test-data">data</a>, <a href="#ios_unit_test-bundle_name">bundle_name</a>, <a href="#ios_unit_test-collect_code_coverage">collect_code_coverage</a>, <a href="#ios_unit_test-env">env</a>, <a href="#ios_unit_test-env_inherit">env_inherit</a>,
616+
<a href="#ios_unit_test-minimum_deployment_os_version">minimum_deployment_os_version</a>, <a href="#ios_unit_test-minimum_os_version">minimum_os_version</a>, <a href="#ios_unit_test-platform_type">platform_type</a>, <a href="#ios_unit_test-runner">runner</a>,
617+
<a href="#ios_unit_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#ios_unit_test-test_filter">test_filter</a>, <a href="#ios_unit_test-test_host">test_host</a>, <a href="#ios_unit_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
617618
</pre>
618619

619620
Builds and bundles an iOS Unit `.xctest` test bundle. Runs the tests using the
@@ -645,6 +646,7 @@ of the attributes inherited by all test rules, please check the
645646
| <a id="ios_unit_test-deps"></a>deps | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
646647
| <a id="ios_unit_test-data"></a>data | Files to be made available to the test during its execution. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
647648
| <a id="ios_unit_test-bundle_name"></a>bundle_name | The desired name of the bundle (without the extension). If this attribute is not set, then the name of the target will be used instead. | String | optional | `""` |
649+
| <a id="ios_unit_test-collect_code_coverage"></a>collect_code_coverage | Whether to collect code coverage for this test if `--collect_code_coverage=yes`. | Boolean | optional | `True` |
648650
| <a id="ios_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. The values of the dictionary are subject to "Make" variable expansion. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
649651
| <a id="ios_unit_test-env_inherit"></a>env_inherit | List of environment variables to inherit from the external environment. | List of strings | optional | `[]` |
650652
| <a id="ios_unit_test-minimum_deployment_os_version"></a>minimum_deployment_os_version | A required string indicating the minimum deployment OS version supported by the target, represented as a dotted version number (for example, "9.0"). This is different from `minimum_os_version`, which is effective at compile time. Ensure version specific APIs are guarded with `available` clauses. | String | optional | `""` |

doc/rules-macos.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,9 @@ i.e. `--features=-swift.no_generated_header`).
617617
## macos_ui_test
618618

619619
<pre>
620-
macos_ui_test(<a href="#macos_ui_test-name">name</a>, <a href="#macos_ui_test-deps">deps</a>, <a href="#macos_ui_test-data">data</a>, <a href="#macos_ui_test-bundle_name">bundle_name</a>, <a href="#macos_ui_test-env">env</a>, <a href="#macos_ui_test-env_inherit">env_inherit</a>, <a href="#macos_ui_test-minimum_deployment_os_version">minimum_deployment_os_version</a>,
621-
<a href="#macos_ui_test-minimum_os_version">minimum_os_version</a>, <a href="#macos_ui_test-platform_type">platform_type</a>, <a href="#macos_ui_test-runner">runner</a>, <a href="#macos_ui_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#macos_ui_test-test_filter">test_filter</a>,
622-
<a href="#macos_ui_test-test_host">test_host</a>, <a href="#macos_ui_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
620+
macos_ui_test(<a href="#macos_ui_test-name">name</a>, <a href="#macos_ui_test-deps">deps</a>, <a href="#macos_ui_test-data">data</a>, <a href="#macos_ui_test-bundle_name">bundle_name</a>, <a href="#macos_ui_test-collect_code_coverage">collect_code_coverage</a>, <a href="#macos_ui_test-env">env</a>, <a href="#macos_ui_test-env_inherit">env_inherit</a>,
621+
<a href="#macos_ui_test-minimum_deployment_os_version">minimum_deployment_os_version</a>, <a href="#macos_ui_test-minimum_os_version">minimum_os_version</a>, <a href="#macos_ui_test-platform_type">platform_type</a>, <a href="#macos_ui_test-runner">runner</a>,
622+
<a href="#macos_ui_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#macos_ui_test-test_filter">test_filter</a>, <a href="#macos_ui_test-test_host">test_host</a>, <a href="#macos_ui_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
623623
</pre>
624624

625625
Builds and bundles an iOS UI `.xctest` test bundle. Runs the tests using the
@@ -636,6 +636,7 @@ Note: macOS UI tests are not currently supported in the default test runner.
636636
| <a id="macos_ui_test-deps"></a>deps | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
637637
| <a id="macos_ui_test-data"></a>data | Files to be made available to the test during its execution. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
638638
| <a id="macos_ui_test-bundle_name"></a>bundle_name | The desired name of the bundle (without the extension). If this attribute is not set, then the name of the target will be used instead. | String | optional | `""` |
639+
| <a id="macos_ui_test-collect_code_coverage"></a>collect_code_coverage | Whether to collect code coverage for this test if `--collect_code_coverage=yes`. | Boolean | optional | `True` |
639640
| <a id="macos_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. The values of the dictionary are subject to "Make" variable expansion. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
640641
| <a id="macos_ui_test-env_inherit"></a>env_inherit | List of environment variables to inherit from the external environment. | List of strings | optional | `[]` |
641642
| <a id="macos_ui_test-minimum_deployment_os_version"></a>minimum_deployment_os_version | A required string indicating the minimum deployment OS version supported by the target, represented as a dotted version number (for example, "9.0"). This is different from `minimum_os_version`, which is effective at compile time. Ensure version specific APIs are guarded with `available` clauses. | String | optional | `""` |
@@ -653,9 +654,9 @@ Note: macOS UI tests are not currently supported in the default test runner.
653654
## macos_unit_test
654655

655656
<pre>
656-
macos_unit_test(<a href="#macos_unit_test-name">name</a>, <a href="#macos_unit_test-deps">deps</a>, <a href="#macos_unit_test-data">data</a>, <a href="#macos_unit_test-bundle_name">bundle_name</a>, <a href="#macos_unit_test-env">env</a>, <a href="#macos_unit_test-env_inherit">env_inherit</a>, <a href="#macos_unit_test-minimum_deployment_os_version">minimum_deployment_os_version</a>,
657-
<a href="#macos_unit_test-minimum_os_version">minimum_os_version</a>, <a href="#macos_unit_test-platform_type">platform_type</a>, <a href="#macos_unit_test-runner">runner</a>, <a href="#macos_unit_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#macos_unit_test-test_filter">test_filter</a>,
658-
<a href="#macos_unit_test-test_host">test_host</a>, <a href="#macos_unit_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
657+
macos_unit_test(<a href="#macos_unit_test-name">name</a>, <a href="#macos_unit_test-deps">deps</a>, <a href="#macos_unit_test-data">data</a>, <a href="#macos_unit_test-bundle_name">bundle_name</a>, <a href="#macos_unit_test-collect_code_coverage">collect_code_coverage</a>, <a href="#macos_unit_test-env">env</a>, <a href="#macos_unit_test-env_inherit">env_inherit</a>,
658+
<a href="#macos_unit_test-minimum_deployment_os_version">minimum_deployment_os_version</a>, <a href="#macos_unit_test-minimum_os_version">minimum_os_version</a>, <a href="#macos_unit_test-platform_type">platform_type</a>, <a href="#macos_unit_test-runner">runner</a>,
659+
<a href="#macos_unit_test-test_coverage_manifest">test_coverage_manifest</a>, <a href="#macos_unit_test-test_filter">test_filter</a>, <a href="#macos_unit_test-test_host">test_host</a>, <a href="#macos_unit_test-test_host_is_bundle_loader">test_host_is_bundle_loader</a>)
659660
</pre>
660661

661662
Builds and bundles a macOS unit `.xctest` test bundle. Runs the tests using the
@@ -678,6 +679,7 @@ find more information about testing for Apple platforms
678679
| <a id="macos_unit_test-deps"></a>deps | - | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
679680
| <a id="macos_unit_test-data"></a>data | Files to be made available to the test during its execution. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
680681
| <a id="macos_unit_test-bundle_name"></a>bundle_name | The desired name of the bundle (without the extension). If this attribute is not set, then the name of the target will be used instead. | String | optional | `""` |
682+
| <a id="macos_unit_test-collect_code_coverage"></a>collect_code_coverage | Whether to collect code coverage for this test if `--collect_code_coverage=yes`. | Boolean | optional | `True` |
681683
| <a id="macos_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. The values of the dictionary are subject to "Make" variable expansion. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
682684
| <a id="macos_unit_test-env_inherit"></a>env_inherit | List of environment variables to inherit from the external environment. | List of strings | optional | `[]` |
683685
| <a id="macos_unit_test-minimum_deployment_os_version"></a>minimum_deployment_os_version | A required string indicating the minimum deployment OS version supported by the target, represented as a dotted version number (for example, "9.0"). This is different from `minimum_os_version`, which is effective at compile time. Ensure version specific APIs are guarded with `available` clauses. | String | optional | `""` |

0 commit comments

Comments
 (0)