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

Migrate the first batch of Xcode rules' unit tests from Java to Starlark. #323

Merged
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
4 changes: 4 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ tasks:
name: "Last Green Bazel"
bazel: last_green
<<: *common
# TODO: Remove once we test with Bazel 8+
test_targets:
- "//..."
- "//test:xcode_config_test"

macos_latest_head_deps:
name: "Current LTS with Head Deps"
Expand Down
15 changes: 14 additions & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//rules:apple_genrule.bzl", "apple_genrule")
load(":apple_support_test.bzl", "apple_support_test")
load(":available_xcodes_test.bzl", "available_xcodes_test")
load(":binary_tests.bzl", "binary_test_suite")
load(":linking_tests.bzl", "linking_test_suite")
load(":starlark_apple_binary.bzl", "starlark_apple_binary")
load(":universal_binary_test.bzl", "universal_binary_test")
load(":xcode_config_test.bzl", "xcode_config_test")
load(":xcode_support_test.bzl", "xcode_support_test")
load(":xcode_version_test.bzl", "xcode_version_test")

licenses(["notice"])

# Custom rules that test rule-context APIs. Check their implementations for more details.
apple_support_test(name = "apple_support_test")

xcode_support_test(name = "xcode_support_test")
available_xcodes_test(name = "available_xcodes_test")

binary_test_suite(name = "binary")

linking_test_suite(name = "linking")

xcode_config_test(name = "xcode_config_test")

xcode_support_test(name = "xcode_support_test")

xcode_version_test(name = "xcode_version_test")

# Test to ensure the environment variable contract of apple_genrule.
sh_test(
name = "apple_genrule_test",
Expand Down Expand Up @@ -51,6 +60,10 @@ bzl_library(
deps = [
"//lib:apple_support",
"//lib:xcode_support",
"//xcode:available_xcodes",
"//xcode:xcode_config",
"//xcode:xcode_version",
"@bazel_skylib//lib:unittest",
],
)

Expand Down
90 changes: 90 additions & 0 deletions test/available_xcodes_test.bzl
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,
],
)
98 changes: 98 additions & 0 deletions test/test_helpers.bzl
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,
)
Loading