-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate the first batch of Xcode rules' unit tests from Java to Starl…
…ark. (#323)
- Loading branch information
1 parent
6758a28
commit 6961212
Showing
6 changed files
with
913 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests for the `available_xcodes` rule.""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "analysistest") | ||
load( | ||
"@build_bazel_apple_support//xcode:available_xcodes.bzl", | ||
"available_xcodes", | ||
) | ||
load( | ||
"@build_bazel_apple_support//xcode:xcode_version.bzl", | ||
"xcode_version", | ||
) | ||
load(":test_helpers.bzl", "FIXTURE_TAGS", "make_all_tests") | ||
|
||
visibility("private") | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
def _read_version_from_providers(namer): | ||
available_xcodes( | ||
name = namer("my_xcodes"), | ||
default = namer(":xcode_8"), | ||
versions = [ | ||
namer(":xcode_8"), | ||
namer(":xcode_9"), | ||
], | ||
tags = FIXTURE_TAGS, | ||
) | ||
|
||
xcode_version( | ||
name = namer("xcode_8"), | ||
default_ios_sdk_version = "9.0", | ||
default_macos_sdk_version = "9.3", | ||
default_tvos_sdk_version = "9.2", | ||
default_watchos_sdk_version = "9.1", | ||
version = "8", | ||
tags = FIXTURE_TAGS, | ||
) | ||
|
||
xcode_version( | ||
name = namer("xcode_9"), | ||
default_ios_sdk_version = "10.0", | ||
default_macos_sdk_version = "10.3", | ||
default_tvos_sdk_version = "10.2", | ||
default_watchos_sdk_version = "10.1", | ||
version = "9", | ||
tags = FIXTURE_TAGS, | ||
) | ||
|
||
_read_version_from_providers_test( | ||
name = "read_version_from_providers", | ||
target_under_test = namer("my_xcodes"), | ||
) | ||
return ["read_version_from_providers"] | ||
|
||
def _read_version_from_providers_test_impl(ctx): | ||
env = analysistest.begin(ctx) | ||
|
||
# TODO: b/311385128 - Add tests for the provider contents once we've moved | ||
# the providers here. We can't test them yet because they are internal to | ||
# built-in Starlark. | ||
|
||
return analysistest.end(env) | ||
|
||
_read_version_from_providers_test = analysistest.make( | ||
_read_version_from_providers_test_impl, | ||
) | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
def available_xcodes_test(name): | ||
make_all_tests( | ||
name = name, | ||
tests = [ | ||
_read_version_from_providers, | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Common Starlark helpers used by apple_support tests.""" | ||
|
||
visibility(["//test/..."]) | ||
|
||
# Common tags used for all test fixtures to ensure that they don't build unless | ||
# used as a dependency of a test. | ||
FIXTURE_TAGS = [ | ||
"manual", | ||
] | ||
|
||
def make_unique_namer(*, prefix, index): | ||
"""Returns a function used to generate unique names in a package. | ||
When generating multiple test fixtures in a single `.bzl` file that contains | ||
multiple test macros, you generally don't want to worry about ensuring that | ||
all the fixture targets have unique names. This utility makes that easier by | ||
returning a simple function that can be used to generate unique names based | ||
on a prefix and index of the test being created. See `make_all_tests` for | ||
how this is used in practice (most users will not need to call this | ||
directly.) | ||
Notice that the returned function handles same-package label references | ||
(beginning with `:`) correctly as well. | ||
""" | ||
|
||
def namer(suffix): | ||
if suffix.startswith(":"): | ||
return ":{}__{}__{}".format(prefix, index, suffix[1:]) | ||
return "{}__{}__{}".format(prefix, index, suffix) | ||
|
||
return namer | ||
|
||
def make_all_tests(*, name, tests, tags = []): | ||
"""Makes all of the tests defined by a list of test functions. | ||
This function simplifies the process of creating Starlark tests and the | ||
corresponding test suite. It should be called from a test-creation macro | ||
with the desired name of the test suite target, which will be used to create | ||
a unique namer for fixtures created by the macro, and a list of other test | ||
macros that each represents a test and its fixtures. | ||
Each entry in `tests` passed to this function should have the following | ||
behavior: | ||
* It must take a single `namer` argument that will be a function returned | ||
by `make_unique_namer` that the test should use to create unique names | ||
for its fixture targets. | ||
* It must return a list of names of the test targets (not fixtures, just | ||
actual tests) that it created so that they can be added to the test | ||
suite. | ||
```build | ||
def some_test_macro(name): | ||
make_all_tests( | ||
name = name, | ||
tests = [ | ||
test1, | ||
test2, | ||
], | ||
) | ||
def test1(namer): | ||
some_target( | ||
name = namer("foo"), | ||
some_label = namer(":bar") | ||
) | ||
some_test( | ||
name = "test1", | ||
target_under_test = namer(":foo"), | ||
) | ||
return ["test1"] | ||
``` | ||
""" | ||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
returned_test | ||
for index, test_creator in enumerate(tests) | ||
for returned_test in test_creator( | ||
namer = make_unique_namer(prefix = name, index = index + 1), | ||
) | ||
], | ||
tags = tags, | ||
) |
Oops, something went wrong.