Skip to content

Commit c17af58

Browse files
authored
Add test filter parameter to ios_unit_test (#1739)
This is a port from this [PR](#1616) to get into the 5.x branch so we could use the ability to use the test filter parameter (cherry picked from commit 3d5944d)
1 parent 201fcbe commit c17af58

File tree

8 files changed

+168
-11
lines changed

8 files changed

+168
-11
lines changed

apple/internal/rule_factory.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ AppleTestRunnerInfo provider.
202202
"test_host": attr.label(
203203
providers = [AppleBundleInfo],
204204
),
205+
"test_filter": attr.string(
206+
doc = """
207+
Test filter string that will be passed into the test runner to select which tests will run.
208+
""",
209+
default = "",
210+
),
205211
"_apple_coverage_support": attr.label(
206212
cfg = "exec",
207213
default = Label("@build_bazel_apple_support//tools:coverage_support"),

apple/internal/testing/apple_test_rule_support.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ This aspect propagates a `CoverageFilesInfo` provider.
102102
implementation = _coverage_files_aspect_impl,
103103
)
104104

105-
def _get_template_substitutions(test_type, test_bundle, test_environment, test_host = None):
105+
def _get_template_substitutions(test_type, test_bundle, test_environment, test_host = None, test_filter = None):
106106
"""Dictionary with the substitutions to be applied to the template script."""
107107
subs = {}
108108

@@ -113,6 +113,7 @@ def _get_template_substitutions(test_type, test_bundle, test_environment, test_h
113113
subs["test_bundle_path"] = test_bundle.short_path
114114
subs["test_type"] = test_type.upper()
115115
subs["test_env"] = ",".join([k + "=" + v for (k, v) in test_environment.items()])
116+
subs["test_filter"] = test_filter or ""
116117

117118
return {"%(" + k + ")s": subs[k] for k in subs}
118119

@@ -174,6 +175,7 @@ def _apple_test_rule_impl(ctx, test_type):
174175
test_bundle,
175176
test_environment,
176177
test_host = test_host_archive,
178+
test_filter = ctx.attr.test_filter,
177179
),
178180
is_executable = True,
179181
)

apple/testing/default_runner/ios_test_runner.template.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,26 @@ if [[ -n "${command_line_args}" ]]; then
127127
LAUNCH_OPTIONS_JSON_STR+="\"args\":[\"$command_line_args\"]"
128128
fi
129129

130+
TEST_FILTER="%(test_filter)s"
131+
130132
# Use the TESTBRIDGE_TEST_ONLY environment variable set by Bazel's --test_filter
131133
# flag to set tests_to_run value in ios_test_runner's launch_options.
132134
# Any test prefixed with '-' will be passed to "skip_tests". Otherwise the tests
133135
# is passed to "tests_to_run"
134-
if [[ -n "$TESTBRIDGE_TEST_ONLY" ]]; then
136+
if [[ -n "$TESTBRIDGE_TEST_ONLY" || -n "$TEST_FILTER" ]]; then
135137
if [[ -n "${LAUNCH_OPTIONS_JSON_STR}" ]]; then
136138
LAUNCH_OPTIONS_JSON_STR+=","
137139
fi
138140

139141
IFS=","
140-
ALL_TESTS=("$TESTBRIDGE_TEST_ONLY")
142+
if [[ -n "$TESTBRIDGE_TEST_ONLY" && -n "$TEST_FILTER" ]]; then
143+
ALL_TESTS=("$TESTBRIDGE_TEST_ONLY,$TEST_FILTER")
144+
elif [[ -n "$TESTBRIDGE_TEST_ONLY" ]]; then
145+
ALL_TESTS=("$TESTBRIDGE_TEST_ONLY")
146+
else
147+
ALL_TESTS=("$TEST_FILTER")
148+
fi
149+
141150
for TEST in $ALL_TESTS; do
142151
if [[ $TEST == -* ]]; then
143152
if [[ -n "$SKIP_TESTS" ]]; then

doc/rules-ios.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ Builds and bundles an iOS Sticker Pack Extension.
500500
## ios_ui_test
501501

502502
<pre>
503-
ios_ui_test(<a href="#ios_ui_test-name">name</a>, <a href="#ios_ui_test-data">data</a>, <a href="#ios_ui_test-deps">deps</a>, <a href="#ios_ui_test-env">env</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_host">test_host</a>)
503+
ios_ui_test(<a href="#ios_ui_test-name">name</a>, <a href="#ios_ui_test-data">data</a>, <a href="#ios_ui_test-deps">deps</a>, <a href="#ios_ui_test-env">env</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_filter">test_filter</a>, <a href="#ios_ui_test-test_host">test_host</a>)
504504
</pre>
505505

506506
iOS UI Test rule.
@@ -529,6 +529,7 @@ of the attributes inherited by all test rules, please check the
529529
| <a id="ios_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
530530
| <a id="ios_ui_test-platform_type"></a>platform_type | - | String | optional | "ios" |
531531
| <a id="ios_ui_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
532+
| <a id="ios_ui_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
532533
| <a id="ios_ui_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
533534

534535

@@ -537,7 +538,7 @@ of the attributes inherited by all test rules, please check the
537538
## ios_unit_test
538539

539540
<pre>
540-
ios_unit_test(<a href="#ios_unit_test-name">name</a>, <a href="#ios_unit_test-data">data</a>, <a href="#ios_unit_test-deps">deps</a>, <a href="#ios_unit_test-env">env</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_host">test_host</a>)
541+
ios_unit_test(<a href="#ios_unit_test-name">name</a>, <a href="#ios_unit_test-data">data</a>, <a href="#ios_unit_test-deps">deps</a>, <a href="#ios_unit_test-env">env</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_filter">test_filter</a>, <a href="#ios_unit_test-test_host">test_host</a>)
541542
</pre>
542543

543544
Builds and bundles an iOS Unit `.xctest` test bundle. Runs the tests using the
@@ -571,6 +572,7 @@ of the attributes inherited by all test rules, please check the
571572
| <a id="ios_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
572573
| <a id="ios_unit_test-platform_type"></a>platform_type | - | String | optional | "ios" |
573574
| <a id="ios_unit_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
575+
| <a id="ios_unit_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
574576
| <a id="ios_unit_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
575577

576578

doc/rules-macos.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ Builds and bundles a macOS Spotlight Importer.
406406
## macos_ui_test
407407

408408
<pre>
409-
macos_ui_test(<a href="#macos_ui_test-name">name</a>, <a href="#macos_ui_test-data">data</a>, <a href="#macos_ui_test-deps">deps</a>, <a href="#macos_ui_test-env">env</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_host">test_host</a>)
409+
macos_ui_test(<a href="#macos_ui_test-name">name</a>, <a href="#macos_ui_test-data">data</a>, <a href="#macos_ui_test-deps">deps</a>, <a href="#macos_ui_test-env">env</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_filter">test_filter</a>, <a href="#macos_ui_test-test_host">test_host</a>)
410410
</pre>
411411

412412
Builds and bundles an iOS UI `.xctest` test bundle. Runs the tests using the
@@ -427,6 +427,7 @@ Note: macOS UI tests are not currently supported in the default test runner.
427427
| <a id="macos_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
428428
| <a id="macos_ui_test-platform_type"></a>platform_type | - | String | optional | "macos" |
429429
| <a id="macos_ui_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
430+
| <a id="macos_ui_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
430431
| <a id="macos_ui_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
431432

432433

@@ -435,7 +436,7 @@ Note: macOS UI tests are not currently supported in the default test runner.
435436
## macos_unit_test
436437

437438
<pre>
438-
macos_unit_test(<a href="#macos_unit_test-name">name</a>, <a href="#macos_unit_test-data">data</a>, <a href="#macos_unit_test-deps">deps</a>, <a href="#macos_unit_test-env">env</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_host">test_host</a>)
439+
macos_unit_test(<a href="#macos_unit_test-name">name</a>, <a href="#macos_unit_test-data">data</a>, <a href="#macos_unit_test-deps">deps</a>, <a href="#macos_unit_test-env">env</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_filter">test_filter</a>, <a href="#macos_unit_test-test_host">test_host</a>)
439440
</pre>
440441

441442
Builds and bundles a macOS unit `.xctest` test bundle. Runs the tests using the
@@ -462,6 +463,7 @@ find more information about testing for Apple platforms
462463
| <a id="macos_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
463464
| <a id="macos_unit_test-platform_type"></a>platform_type | - | String | optional | "macos" |
464465
| <a id="macos_unit_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
466+
| <a id="macos_unit_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
465467
| <a id="macos_unit_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
466468

467469

doc/rules-tvos.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ i.e. `--features=-swift.no_generated_header`).
310310
## tvos_ui_test
311311

312312
<pre>
313-
tvos_ui_test(<a href="#tvos_ui_test-name">name</a>, <a href="#tvos_ui_test-data">data</a>, <a href="#tvos_ui_test-deps">deps</a>, <a href="#tvos_ui_test-env">env</a>, <a href="#tvos_ui_test-platform_type">platform_type</a>, <a href="#tvos_ui_test-runner">runner</a>, <a href="#tvos_ui_test-test_host">test_host</a>)
313+
tvos_ui_test(<a href="#tvos_ui_test-name">name</a>, <a href="#tvos_ui_test-data">data</a>, <a href="#tvos_ui_test-deps">deps</a>, <a href="#tvos_ui_test-env">env</a>, <a href="#tvos_ui_test-platform_type">platform_type</a>, <a href="#tvos_ui_test-runner">runner</a>, <a href="#tvos_ui_test-test_filter">test_filter</a>, <a href="#tvos_ui_test-test_host">test_host</a>)
314314
</pre>
315315

316316

@@ -337,6 +337,7 @@ the attributes inherited by all test rules, please check the
337337
| <a id="tvos_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
338338
| <a id="tvos_ui_test-platform_type"></a>platform_type | - | String | optional | "tvos" |
339339
| <a id="tvos_ui_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
340+
| <a id="tvos_ui_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
340341
| <a id="tvos_ui_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
341342

342343

@@ -345,7 +346,7 @@ the attributes inherited by all test rules, please check the
345346
## tvos_unit_test
346347

347348
<pre>
348-
tvos_unit_test(<a href="#tvos_unit_test-name">name</a>, <a href="#tvos_unit_test-data">data</a>, <a href="#tvos_unit_test-deps">deps</a>, <a href="#tvos_unit_test-env">env</a>, <a href="#tvos_unit_test-platform_type">platform_type</a>, <a href="#tvos_unit_test-runner">runner</a>, <a href="#tvos_unit_test-test_host">test_host</a>)
349+
tvos_unit_test(<a href="#tvos_unit_test-name">name</a>, <a href="#tvos_unit_test-data">data</a>, <a href="#tvos_unit_test-deps">deps</a>, <a href="#tvos_unit_test-env">env</a>, <a href="#tvos_unit_test-platform_type">platform_type</a>, <a href="#tvos_unit_test-runner">runner</a>, <a href="#tvos_unit_test-test_filter">test_filter</a>, <a href="#tvos_unit_test-test_host">test_host</a>)
349350
</pre>
350351

351352

@@ -380,6 +381,7 @@ of the attributes inherited by all test rules, please check the
380381
| <a id="tvos_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
381382
| <a id="tvos_unit_test-platform_type"></a>platform_type | - | String | optional | "tvos" |
382383
| <a id="tvos_unit_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
384+
| <a id="tvos_unit_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
383385
| <a id="tvos_unit_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
384386

385387

doc/rules-watchos.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Builds and bundles a watchOS Static Framework.
268268
## watchos_ui_test
269269

270270
<pre>
271-
watchos_ui_test(<a href="#watchos_ui_test-name">name</a>, <a href="#watchos_ui_test-data">data</a>, <a href="#watchos_ui_test-deps">deps</a>, <a href="#watchos_ui_test-env">env</a>, <a href="#watchos_ui_test-platform_type">platform_type</a>, <a href="#watchos_ui_test-runner">runner</a>, <a href="#watchos_ui_test-test_host">test_host</a>)
271+
watchos_ui_test(<a href="#watchos_ui_test-name">name</a>, <a href="#watchos_ui_test-data">data</a>, <a href="#watchos_ui_test-deps">deps</a>, <a href="#watchos_ui_test-env">env</a>, <a href="#watchos_ui_test-platform_type">platform_type</a>, <a href="#watchos_ui_test-runner">runner</a>, <a href="#watchos_ui_test-test_filter">test_filter</a>, <a href="#watchos_ui_test-test_host">test_host</a>)
272272
</pre>
273273

274274
watchOS UI Test rule.
@@ -284,6 +284,7 @@ watchOS UI Test rule.
284284
| <a id="watchos_ui_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
285285
| <a id="watchos_ui_test-platform_type"></a>platform_type | - | String | optional | "watchos" |
286286
| <a id="watchos_ui_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
287+
| <a id="watchos_ui_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
287288
| <a id="watchos_ui_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
288289

289290

@@ -292,7 +293,7 @@ watchOS UI Test rule.
292293
## watchos_unit_test
293294

294295
<pre>
295-
watchos_unit_test(<a href="#watchos_unit_test-name">name</a>, <a href="#watchos_unit_test-data">data</a>, <a href="#watchos_unit_test-deps">deps</a>, <a href="#watchos_unit_test-env">env</a>, <a href="#watchos_unit_test-platform_type">platform_type</a>, <a href="#watchos_unit_test-runner">runner</a>, <a href="#watchos_unit_test-test_host">test_host</a>)
296+
watchos_unit_test(<a href="#watchos_unit_test-name">name</a>, <a href="#watchos_unit_test-data">data</a>, <a href="#watchos_unit_test-deps">deps</a>, <a href="#watchos_unit_test-env">env</a>, <a href="#watchos_unit_test-platform_type">platform_type</a>, <a href="#watchos_unit_test-runner">runner</a>, <a href="#watchos_unit_test-test_filter">test_filter</a>, <a href="#watchos_unit_test-test_host">test_host</a>)
296297
</pre>
297298

298299
watchOS Unit Test rule.
@@ -308,6 +309,7 @@ watchOS Unit Test rule.
308309
| <a id="watchos_unit_test-env"></a>env | Dictionary of environment variables that should be set during the test execution. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
309310
| <a id="watchos_unit_test-platform_type"></a>platform_type | - | String | optional | "watchos" |
310311
| <a id="watchos_unit_test-runner"></a>runner | The runner target that will provide the logic on how to run the tests. Needs to provide the AppleTestRunnerInfo provider. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
312+
| <a id="watchos_unit_test-test_filter"></a>test_filter | Test filter string that will be passed into the test runner to select which tests will run. | String | optional | "" |
311313
| <a id="watchos_unit_test-test_host"></a>test_host | - | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
312314

313315

0 commit comments

Comments
 (0)