Skip to content
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
46 changes: 37 additions & 9 deletions apple/internal/testing/apple_test_assembler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ _SHARED_SUITE_TEST_ATTRS = {
]
}

def _assemble(name, bundle_rule, test_rule, runner = None, runners = None, **kwargs):
def _assemble(
name,
bundle_rule,
test_rule,
generate_tests_func = None,
runner = None,
runners = None,
**kwargs):
"""Assembles the test bundle and test targets.

This method expects that either `runner` or `runners` is populated, but never both. If `runner`
Expand All @@ -82,6 +89,8 @@ def _assemble(name, bundle_rule, test_rule, runner = None, runners = None, **kwa
name: The name of the test target or test suite to create.
bundle_rule: The bundling rule to instantiate.
test_rule: The test rule to instantiate.
generate_tests_func: A function that is passed the attributes for a test rule and returns a
list of attributes to use to create N test targets. Mutually exclusive with `runners`.
runner: A single runner target to use for the test target. Mutually exclusive with
`runners`.
runners: A list of runner targets to use for the test targets. Mutually exclusive with
Expand All @@ -90,8 +99,22 @@ def _assemble(name, bundle_rule, test_rule, runner = None, runners = None, **kwa
"""
if runner != None and runners != None:
fail("Can't specify both runner and runners.")
elif not runner and not runners:
fail("Must specify one of runner or runners.")
elif not runner and not runners and not generate_tests_func:
fail("Must specify one of runner, runners, or generate_tests_func.")

if runner:
if generate_tests_func:
fail("Can't specify both runner and generate_tests_func.")
elif runners:
if generate_tests_func:
fail("Can't specify both runners and generate_tests_func.")

generate_tests_func = lambda **attrs: [
attrs | {
"name": "{}_{}".format(name, runner.rsplit(":", 1)[-1]),
"runner": runner
} for runner in runners
]

test_bundle_name = name + ".__internal__.__test_bundle"

Expand Down Expand Up @@ -129,17 +152,22 @@ def _assemble(name, bundle_rule, test_rule, runner = None, runners = None, **kwa
deps = [":{}".format(test_bundle_name)],
**test_attrs
)
elif runners:
elif generate_tests_func:
all_test_attrs = generate_tests_func(
name = name,
deps = [":{}".format(test_bundle_name)],
**test_attrs
)

tests = []
for runner in runners:
test_name = "{}_{}".format(name, runner.rsplit(":", 1)[-1])
for single_test_attrs in all_test_attrs:
test_name = single_test_attrs.pop("name")
tests.append(":{}".format(test_name))
test_rule(
name = test_name,
runner = runner,
deps = [":{}".format(test_bundle_name)],
**test_attrs
**single_test_attrs
)

shared_test_suite_attrs = {k: v for (k, v) in test_attrs.items() if k in _SHARED_SUITE_TEST_ATTRS}
native.test_suite(
name = name,
Expand Down
56 changes: 35 additions & 21 deletions apple/ios.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,41 +75,55 @@ def ios_ui_test(name, **kwargs):
**kwargs
)

def ios_unit_test_suite(name, runners = None, **kwargs):
"""Generates a [test_suite] containing an [ios_unit_test] for each of the given `runners`.

`ios_unit_test_suite` takes the same parameters as [ios_unit_test], except `runner` is replaced by `runners`.

[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_unit_test]: #ios_unit_test

Args:
runners: a list of runner targets
**kwargs: passed to the [ios_unit_test]
"""
def ios_unit_test_suite(name, generate_tests_func = None, runners = None, **kwargs):
"""Generates a [test_suite] containing an [ios_unit_test] for each of the given `runners` or \
results of `generate_tests_func`.

`ios_unit_test_suite` takes the same parameters as [ios_unit_test], except `runner` is
replaced by `generate_tests_func` and `runners`.

[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_unit_test]: #ios_unit_test

Args:
name: The name of the test suite to create.
generate_tests_func: A function that is passed the attributes for a test rule and returns a
list of attribute dictionaries to use to create N test targets. Mutually exclusive with
`runners`.
runners: A `list` of runner targets.
**kwargs: passed to the [ios_unit_test]
"""
apple_test_assembler.assemble(
name = name,
bundle_rule = _ios_internal_unit_test_bundle,
generate_tests_func = generate_tests_func,
test_rule = _ios_unit_test,
runners = runners,
**kwargs
)

def ios_ui_test_suite(name, runners = None, **kwargs):
"""Generates a [test_suite] containing an [ios_ui_test] for each of the given `runners`.
def ios_ui_test_suite(name, generate_tests_func = None, runners = None, **kwargs):
"""Generates a [test_suite] containing an [ios_ui_test] for each of the given `runners` or \
results of `generate_tests_func`.

`ios_ui_test_suite` takes the same parameters as [ios_ui_test], except `runner` is replaced by `runners`.
`ios_ui_test_suite` takes the same parameters as [ios_ui_test], except `runner` is replaced by
`generate_tests_func` and `runners`.

[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_ui_test]: #ios_ui_test
[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_ui_test]: #ios_ui_test

Args:
runners: a list of runner targets
**kwargs: passed to the [ios_ui_test]
"""
Args:
name: The name of the test suite to create.
generate_tests_func: A function that is passed the attributes for a test rule and returns a
list of attribute dictionaries to use to create N test targets. Mutually exclusive with
`runners`.
runners: A `list` of runner targets. Mutually exclusive with `generate_tests_func`.
**kwargs: passed to the [ios_ui_test]
"""
apple_test_assembler.assemble(
name = name,
bundle_rule = _ios_internal_ui_test_bundle,
generate_tests_func = generate_tests_func,
test_rule = _ios_ui_test,
runners = runners,
**kwargs
Expand Down
2 changes: 1 addition & 1 deletion doc/rules-apple.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ ios_application(
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="provisioning_profile_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="provisioning_profile_repository-fallback_profiles"></a>fallback_profiles | - | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
| <a id="provisioning_profile_repository-repo_mapping"></a>repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`), it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |
| <a id="provisioning_profile_repository-repo_mapping"></a>repo_mapping | In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | |

**ENVIRONMENT VARIABLES**

Expand Down
24 changes: 14 additions & 10 deletions doc/rules-ios.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,12 +733,13 @@ in Xcode.
## ios_ui_test_suite

<pre>
ios_ui_test_suite(<a href="#ios_ui_test_suite-name">name</a>, <a href="#ios_ui_test_suite-runners">runners</a>, <a href="#ios_ui_test_suite-kwargs">kwargs</a>)
ios_ui_test_suite(<a href="#ios_ui_test_suite-name">name</a>, <a href="#ios_ui_test_suite-generate_tests_func">generate_tests_func</a>, <a href="#ios_ui_test_suite-runners">runners</a>, <a href="#ios_ui_test_suite-kwargs">kwargs</a>)
</pre>

Generates a [test_suite] containing an [ios_ui_test] for each of the given `runners`.
Generates a [test_suite] containing an [ios_ui_test] for each of the given `runners` or results of `generate_tests_func`.

`ios_ui_test_suite` takes the same parameters as [ios_ui_test], except `runner` is replaced by `runners`.
`ios_ui_test_suite` takes the same parameters as [ios_ui_test], except `runner` is replaced by
`generate_tests_func` and `runners`.

[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_ui_test]: #ios_ui_test
Expand All @@ -749,8 +750,9 @@ Generates a [test_suite] containing an [ios_ui_test] for each of the given `runn

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="ios_ui_test_suite-name"></a>name | <p align="center"> - </p> | none |
| <a id="ios_ui_test_suite-runners"></a>runners | a list of runner targets | `None` |
| <a id="ios_ui_test_suite-name"></a>name | The name of the test suite to create. | none |
| <a id="ios_ui_test_suite-generate_tests_func"></a>generate_tests_func | A function that is passed the attributes for a test rule and returns a list of attribute dictionaries to use to create N test targets. Mutually exclusive with `runners`. | `None` |
| <a id="ios_ui_test_suite-runners"></a>runners | A `list` of runner targets. Mutually exclusive with `generate_tests_func`. | `None` |
| <a id="ios_ui_test_suite-kwargs"></a>kwargs | passed to the [ios_ui_test] | none |


Expand All @@ -759,12 +761,13 @@ Generates a [test_suite] containing an [ios_ui_test] for each of the given `runn
## ios_unit_test_suite

<pre>
ios_unit_test_suite(<a href="#ios_unit_test_suite-name">name</a>, <a href="#ios_unit_test_suite-runners">runners</a>, <a href="#ios_unit_test_suite-kwargs">kwargs</a>)
ios_unit_test_suite(<a href="#ios_unit_test_suite-name">name</a>, <a href="#ios_unit_test_suite-generate_tests_func">generate_tests_func</a>, <a href="#ios_unit_test_suite-runners">runners</a>, <a href="#ios_unit_test_suite-kwargs">kwargs</a>)
</pre>

Generates a [test_suite] containing an [ios_unit_test] for each of the given `runners`.
Generates a [test_suite] containing an [ios_unit_test] for each of the given `runners` or results of `generate_tests_func`.

`ios_unit_test_suite` takes the same parameters as [ios_unit_test], except `runner` is replaced by `runners`.
`ios_unit_test_suite` takes the same parameters as [ios_unit_test], except `runner` is
replaced by `generate_tests_func` and `runners`.

[test_suite]: https://docs.bazel.build/versions/master/be/general.html#test_suite
[ios_unit_test]: #ios_unit_test
Expand All @@ -775,8 +778,9 @@ Generates a [test_suite] containing an [ios_unit_test] for each of the given `ru

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="ios_unit_test_suite-name"></a>name | <p align="center"> - </p> | none |
| <a id="ios_unit_test_suite-runners"></a>runners | a list of runner targets | `None` |
| <a id="ios_unit_test_suite-name"></a>name | The name of the test suite to create. | none |
| <a id="ios_unit_test_suite-generate_tests_func"></a>generate_tests_func | A function that is passed the attributes for a test rule and returns a list of attribute dictionaries to use to create N test targets. Mutually exclusive with `runners`. | `None` |
| <a id="ios_unit_test_suite-runners"></a>runners | A `list` of runner targets. | `None` |
| <a id="ios_unit_test_suite-kwargs"></a>kwargs | passed to the [ios_unit_test] | none |