aboutsummaryrefslogtreecommitdiff
path: root/tools/build_defs/python/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tools/build_defs/python/tests')
-rw-r--r--tools/build_defs/python/tests/BUILD.bazel27
-rw-r--r--tools/build_defs/python/tests/base_tests.bzl124
-rw-r--r--tools/build_defs/python/tests/py_binary/BUILD.bazel17
-rw-r--r--tools/build_defs/python/tests/py_binary/py_binary_tests.bzl28
-rw-r--r--tools/build_defs/python/tests/py_executable_base_tests.bzl272
-rw-r--r--tools/build_defs/python/tests/py_info_subject.bzl95
-rw-r--r--tools/build_defs/python/tests/py_library/BUILD.bazel18
-rw-r--r--tools/build_defs/python/tests/py_library/py_library_tests.bzl148
-rw-r--r--tools/build_defs/python/tests/py_test/BUILD.bazel18
-rw-r--r--tools/build_defs/python/tests/py_test/py_test_tests.bzl107
-rw-r--r--tools/build_defs/python/tests/py_wheel/BUILD.bazel18
-rw-r--r--tools/build_defs/python/tests/py_wheel/py_wheel_tests.bzl39
-rw-r--r--tools/build_defs/python/tests/util.bzl78
13 files changed, 989 insertions, 0 deletions
diff --git a/tools/build_defs/python/tests/BUILD.bazel b/tools/build_defs/python/tests/BUILD.bazel
new file mode 100644
index 0000000..e271850
--- /dev/null
+++ b/tools/build_defs/python/tests/BUILD.bazel
@@ -0,0 +1,27 @@
+# Copyright 2023 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.
+
+platform(
+ name = "mac",
+ constraint_values = [
+ "@platforms//os:macos",
+ ],
+)
+
+platform(
+ name = "linux",
+ constraint_values = [
+ "@platforms//os:linux",
+ ],
+)
diff --git a/tools/build_defs/python/tests/base_tests.bzl b/tools/build_defs/python/tests/base_tests.bzl
new file mode 100644
index 0000000..467611f
--- /dev/null
+++ b/tools/build_defs/python/tests/base_tests.bzl
@@ -0,0 +1,124 @@
+# Copyright 2023 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 common to py_test, py_binary, and py_library rules."""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
+load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS", rt_util = "util")
+load("//python:defs.bzl", "PyInfo")
+load("//tools/build_defs/python/tests:py_info_subject.bzl", "py_info_subject")
+load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")
+
+_tests = []
+
+def _produces_py_info_impl(ctx):
+ return [PyInfo(transitive_sources = depset(ctx.files.srcs))]
+
+_produces_py_info = rule(
+ implementation = _produces_py_info_impl,
+ attrs = {"srcs": attr.label_list(allow_files = True)},
+)
+
+def _test_consumes_provider(name, config):
+ rt_util.helper_target(
+ config.base_test_rule,
+ name = name + "_subject",
+ deps = [name + "_produces_py_info"],
+ )
+ rt_util.helper_target(
+ _produces_py_info,
+ name = name + "_produces_py_info",
+ srcs = [rt_util.empty_file(name + "_produce.py")],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_consumes_provider_impl,
+ )
+
+def _test_consumes_provider_impl(env, target):
+ env.expect.that_target(target).provider(
+ PyInfo,
+ factory = py_info_subject,
+ ).transitive_sources().contains("{package}/{test_name}_produce.py")
+
+_tests.append(_test_consumes_provider)
+
+def _test_requires_provider(name, config):
+ rt_util.helper_target(
+ config.base_test_rule,
+ name = name + "_subject",
+ deps = [name + "_nopyinfo"],
+ )
+ rt_util.helper_target(
+ native.filegroup,
+ name = name + "_nopyinfo",
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_requires_provider_impl,
+ expect_failure = True,
+ )
+
+def _test_requires_provider_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("mandatory*PyInfo"),
+ )
+
+_tests.append(_test_requires_provider)
+
+def _test_data_sets_uses_shared_library(name, config):
+ rt_util.helper_target(
+ config.base_test_rule,
+ name = name + "_subject",
+ data = [rt_util.empty_file(name + "_dso.so")],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_data_sets_uses_shared_library_impl,
+ )
+
+def _test_data_sets_uses_shared_library_impl(env, target):
+ env.expect.that_target(target).provider(
+ PyInfo,
+ factory = py_info_subject,
+ ).uses_shared_libraries().equals(True)
+
+_tests.append(_test_data_sets_uses_shared_library)
+
+def _test_tags_can_be_tuple(name, config):
+ # We don't use a helper because we want to ensure that value passed is
+ # a tuple.
+ config.base_test_rule(
+ name = name + "_subject",
+ tags = ("one", "two") + tuple(PREVENT_IMPLICIT_BUILDING_TAGS),
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_tags_can_be_tuple_impl,
+ )
+
+def _test_tags_can_be_tuple_impl(env, target):
+ env.expect.that_target(target).tags().contains_at_least([
+ "one",
+ "two",
+ ])
+
+_tests.append(_test_tags_can_be_tuple)
+
+def create_base_tests(config):
+ return pt_util.create_tests(_tests, config = config)
diff --git a/tools/build_defs/python/tests/py_binary/BUILD.bazel b/tools/build_defs/python/tests/py_binary/BUILD.bazel
new file mode 100644
index 0000000..17a6690
--- /dev/null
+++ b/tools/build_defs/python/tests/py_binary/BUILD.bazel
@@ -0,0 +1,17 @@
+# Copyright 2023 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.
+
+load(":py_binary_tests.bzl", "py_binary_test_suite")
+
+py_binary_test_suite(name = "py_binary_tests")
diff --git a/tools/build_defs/python/tests/py_binary/py_binary_tests.bzl b/tools/build_defs/python/tests/py_binary/py_binary_tests.bzl
new file mode 100644
index 0000000..8d32632
--- /dev/null
+++ b/tools/build_defs/python/tests/py_binary/py_binary_tests.bzl
@@ -0,0 +1,28 @@
+# Copyright 2023 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 py_binary."""
+
+load("//python:defs.bzl", "py_binary")
+load(
+ "//tools/build_defs/python/tests:py_executable_base_tests.bzl",
+ "create_executable_tests",
+)
+
+def py_binary_test_suite(name):
+ config = struct(rule = py_binary)
+
+ native.test_suite(
+ name = name,
+ tests = create_executable_tests(config),
+ )
diff --git a/tools/build_defs/python/tests/py_executable_base_tests.bzl b/tools/build_defs/python/tests/py_executable_base_tests.bzl
new file mode 100644
index 0000000..c66ea11
--- /dev/null
+++ b/tools/build_defs/python/tests/py_executable_base_tests.bzl
@@ -0,0 +1,272 @@
+# Copyright 2023 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 common to py_binary and py_test (executable rules)."""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
+load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", rt_util = "util")
+load("//tools/build_defs/python/tests:base_tests.bzl", "create_base_tests")
+load("//tools/build_defs/python/tests:util.bzl", "WINDOWS_ATTR", pt_util = "util")
+
+_tests = []
+
+def _test_executable_in_runfiles(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_subject.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_executable_in_runfiles_impl,
+ target = name + "_subject",
+ attrs = WINDOWS_ATTR,
+ )
+
+_tests.append(_test_executable_in_runfiles)
+
+def _test_executable_in_runfiles_impl(env, target):
+ if pt_util.is_windows(env):
+ exe = ".exe"
+ else:
+ exe = ""
+
+ env.expect.that_target(target).runfiles().contains_at_least([
+ "{workspace}/{package}/{test_name}_subject" + exe,
+ ])
+
+def _test_default_main_can_be_generated(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [rt_util.empty_file(name + "_subject.py")],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_default_main_can_be_generated_impl,
+ target = name + "_subject",
+ )
+
+_tests.append(_test_default_main_can_be_generated)
+
+def _test_default_main_can_be_generated_impl(env, target):
+ env.expect.that_target(target).default_outputs().contains(
+ "{package}/{test_name}_subject.py",
+ )
+
+def _test_default_main_can_have_multiple_path_segments(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "/subject",
+ srcs = [name + "/subject.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_default_main_can_have_multiple_path_segments_impl,
+ target = name + "/subject",
+ )
+
+_tests.append(_test_default_main_can_have_multiple_path_segments)
+
+def _test_default_main_can_have_multiple_path_segments_impl(env, target):
+ env.expect.that_target(target).default_outputs().contains(
+ "{package}/{test_name}/subject.py",
+ )
+
+def _test_default_main_must_be_in_srcs(name, config):
+ # Bazel 5 will crash with a Java stacktrace when the native Python
+ # rules have an error.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["other.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_default_main_must_be_in_srcs_impl,
+ target = name + "_subject",
+ expect_failure = True,
+ )
+
+_tests.append(_test_default_main_must_be_in_srcs)
+
+def _test_default_main_must_be_in_srcs_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("default*does not appear in srcs"),
+ )
+
+def _test_default_main_cannot_be_ambiguous(name, config):
+ # Bazel 5 will crash with a Java stacktrace when the native Python
+ # rules have an error.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_subject.py", "other/{}_subject.py".format(name)],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_default_main_cannot_be_ambiguous_impl,
+ target = name + "_subject",
+ expect_failure = True,
+ )
+
+_tests.append(_test_default_main_cannot_be_ambiguous)
+
+def _test_default_main_cannot_be_ambiguous_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("default main*matches multiple files"),
+ )
+
+def _test_explicit_main(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["custom.py"],
+ main = "custom.py",
+ )
+ analysis_test(
+ name = name,
+ impl = _test_explicit_main_impl,
+ target = name + "_subject",
+ )
+
+_tests.append(_test_explicit_main)
+
+def _test_explicit_main_impl(env, target):
+ # There isn't a direct way to ask what main file was selected, so we
+ # rely on it being in the default outputs.
+ env.expect.that_target(target).default_outputs().contains(
+ "{package}/custom.py",
+ )
+
+def _test_explicit_main_cannot_be_ambiguous(name, config):
+ # Bazel 5 will crash with a Java stacktrace when the native Python
+ # rules have an error.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["x/foo.py", "y/foo.py"],
+ main = "foo.py",
+ )
+ analysis_test(
+ name = name,
+ impl = _test_explicit_main_cannot_be_ambiguous_impl,
+ target = name + "_subject",
+ expect_failure = True,
+ )
+
+_tests.append(_test_explicit_main_cannot_be_ambiguous)
+
+def _test_explicit_main_cannot_be_ambiguous_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("foo.py*matches multiple"),
+ )
+
+def _test_files_to_build(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_subject.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_files_to_build_impl,
+ target = name + "_subject",
+ attrs = WINDOWS_ATTR,
+ )
+
+_tests.append(_test_files_to_build)
+
+def _test_files_to_build_impl(env, target):
+ default_outputs = env.expect.that_target(target).default_outputs()
+ if pt_util.is_windows(env):
+ default_outputs.contains("{package}/{test_name}_subject.exe")
+ else:
+ default_outputs.contains_exactly([
+ "{package}/{test_name}_subject",
+ "{package}/{test_name}_subject.py",
+ ])
+
+def _test_name_cannot_end_in_py(name, config):
+ # Bazel 5 will crash with a Java stacktrace when the native Python
+ # rules have an error.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject.py",
+ srcs = ["main.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_name_cannot_end_in_py_impl,
+ target = name + "_subject.py",
+ expect_failure = True,
+ )
+
+_tests.append(_test_name_cannot_end_in_py)
+
+def _test_name_cannot_end_in_py_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("name must not end in*.py"),
+ )
+
+# Can't test this -- mandatory validation happens before analysis test
+# can intercept it
+# TODO(#1069): Once re-implemented in Starlark, modify rule logic to make this
+# testable.
+# def _test_srcs_is_mandatory(name, config):
+# rt_util.helper_target(
+# config.rule,
+# name = name + "_subject",
+# )
+# analysis_test(
+# name = name,
+# impl = _test_srcs_is_mandatory,
+# target = name + "_subject",
+# expect_failure = True,
+# )
+#
+# _tests.append(_test_srcs_is_mandatory)
+#
+# def _test_srcs_is_mandatory_impl(env, target):
+# env.expect.that_target(target).failures().contains_predicate(
+# matching.str_matches("mandatory*srcs"),
+# )
+
+# =====
+# You were gonna add a test at the end, weren't you?
+# Nope. Please keep them sorted; put it in its alphabetical location.
+# Here's the alphabet so you don't have to sing that song in your head:
+# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
+# =====
+
+def create_executable_tests(config):
+ def _executable_with_srcs_wrapper(name, **kwargs):
+ if not kwargs.get("srcs"):
+ kwargs["srcs"] = [name + ".py"]
+ config.rule(name = name, **kwargs)
+
+ config = pt_util.struct_with(config, base_test_rule = _executable_with_srcs_wrapper)
+ return pt_util.create_tests(_tests, config = config) + create_base_tests(config = config)
diff --git a/tools/build_defs/python/tests/py_info_subject.bzl b/tools/build_defs/python/tests/py_info_subject.bzl
new file mode 100644
index 0000000..20185e5
--- /dev/null
+++ b/tools/build_defs/python/tests/py_info_subject.bzl
@@ -0,0 +1,95 @@
+# Copyright 2023 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.
+"""PyInfo testing subject."""
+
+load("@rules_testing//lib:truth.bzl", "subjects")
+
+def py_info_subject(info, *, meta):
+ """Creates a new `PyInfoSubject` for a PyInfo provider instance.
+
+ Method: PyInfoSubject.new
+
+ Args:
+ info: The PyInfo object
+ meta: ExpectMeta object.
+
+ Returns:
+ A `PyInfoSubject` struct
+ """
+
+ # buildifier: disable=uninitialized
+ public = struct(
+ # go/keep-sorted start
+ has_py2_only_sources = lambda *a, **k: _py_info_subject_has_py2_only_sources(self, *a, **k),
+ has_py3_only_sources = lambda *a, **k: _py_info_subject_has_py3_only_sources(self, *a, **k),
+ imports = lambda *a, **k: _py_info_subject_imports(self, *a, **k),
+ transitive_sources = lambda *a, **k: _py_info_subject_transitive_sources(self, *a, **k),
+ uses_shared_libraries = lambda *a, **k: _py_info_subject_uses_shared_libraries(self, *a, **k),
+ # go/keep-sorted end
+ )
+ self = struct(
+ actual = info,
+ meta = meta,
+ )
+ return public
+
+def _py_info_subject_has_py2_only_sources(self):
+ """Returns a `BoolSubject` for the `has_py2_only_sources` attribute.
+
+ Method: PyInfoSubject.has_py2_only_sources
+ """
+ return subjects.bool(
+ self.actual.has_py2_only_sources,
+ meta = self.meta.derive("has_py2_only_sources()"),
+ )
+
+def _py_info_subject_has_py3_only_sources(self):
+ """Returns a `BoolSubject` for the `has_py3_only_sources` attribute.
+
+ Method: PyInfoSubject.has_py3_only_sources
+ """
+ return subjects.bool(
+ self.actual.has_py3_only_sources,
+ meta = self.meta.derive("has_py3_only_sources()"),
+ )
+
+def _py_info_subject_imports(self):
+ """Returns a `CollectionSubject` for the `imports` attribute.
+
+ Method: PyInfoSubject.imports
+ """
+ return subjects.collection(
+ self.actual.imports,
+ meta = self.meta.derive("imports()"),
+ )
+
+def _py_info_subject_transitive_sources(self):
+ """Returns a `DepsetFileSubject` for the `transitive_sources` attribute.
+
+ Method: PyInfoSubject.transitive_sources
+ """
+ return subjects.depset_file(
+ self.actual.transitive_sources,
+ meta = self.meta.derive("transitive_sources()"),
+ )
+
+def _py_info_subject_uses_shared_libraries(self):
+ """Returns a `BoolSubject` for the `uses_shared_libraries` attribute.
+
+ Method: PyInfoSubject.uses_shared_libraries
+ """
+ return subjects.bool(
+ self.actual.uses_shared_libraries,
+ meta = self.meta.derive("uses_shared_libraries()"),
+ )
diff --git a/tools/build_defs/python/tests/py_library/BUILD.bazel b/tools/build_defs/python/tests/py_library/BUILD.bazel
new file mode 100644
index 0000000..9de414b
--- /dev/null
+++ b/tools/build_defs/python/tests/py_library/BUILD.bazel
@@ -0,0 +1,18 @@
+# Copyright 2023 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 py_library."""
+
+load(":py_library_tests.bzl", "py_library_test_suite")
+
+py_library_test_suite(name = "py_library_tests")
diff --git a/tools/build_defs/python/tests/py_library/py_library_tests.bzl b/tools/build_defs/python/tests/py_library/py_library_tests.bzl
new file mode 100644
index 0000000..1fcb0c1
--- /dev/null
+++ b/tools/build_defs/python/tests/py_library/py_library_tests.bzl
@@ -0,0 +1,148 @@
+"""Test for py_library."""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
+load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", rt_util = "util")
+load("//python:defs.bzl", "PyRuntimeInfo", "py_library")
+load("//tools/build_defs/python/tests:base_tests.bzl", "create_base_tests")
+load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")
+
+_tests = []
+
+def _test_py_runtime_info_not_present(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["lib.py"],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_py_runtime_info_not_present_impl,
+ )
+
+def _test_py_runtime_info_not_present_impl(env, target):
+ env.expect.that_bool(PyRuntimeInfo in target).equals(False)
+
+_tests.append(_test_py_runtime_info_not_present)
+
+def _test_files_to_build(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["lib.py"],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_files_to_build_impl,
+ )
+
+def _test_files_to_build_impl(env, target):
+ env.expect.that_target(target).default_outputs().contains_exactly([
+ "{package}/lib.py",
+ ])
+
+_tests.append(_test_files_to_build)
+
+def _test_srcs_can_contain_rule_generating_py_and_nonpy_files(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["lib.py", name + "_gensrcs"],
+ )
+ rt_util.helper_target(
+ native.genrule,
+ name = name + "_gensrcs",
+ cmd = "touch $(OUTS)",
+ outs = [name + "_gen.py", name + "_gen.cc"],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl,
+ )
+
+def _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl(env, target):
+ env.expect.that_target(target).default_outputs().contains_exactly([
+ "{package}/{test_name}_gen.py",
+ "{package}/lib.py",
+ ])
+
+_tests.append(_test_srcs_can_contain_rule_generating_py_and_nonpy_files)
+
+def _test_srcs_generating_no_py_files_is_error(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_gen"],
+ )
+ rt_util.helper_target(
+ native.genrule,
+ name = name + "_gen",
+ cmd = "touch $(OUTS)",
+ outs = [name + "_gen.cc"],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_srcs_generating_no_py_files_is_error_impl,
+ expect_failure = True,
+ )
+
+def _test_srcs_generating_no_py_files_is_error_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("does not produce*srcs files"),
+ )
+
+_tests.append(_test_srcs_generating_no_py_files_is_error)
+
+def _test_files_to_compile(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = ["lib1.py"],
+ deps = [name + "_lib2"],
+ )
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_lib2",
+ srcs = ["lib2.py"],
+ deps = [name + "_lib3"],
+ )
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_lib3",
+ srcs = ["lib3.py"],
+ )
+ analysis_test(
+ name = name,
+ target = name + "_subject",
+ impl = _test_files_to_compile_impl,
+ )
+
+def _test_files_to_compile_impl(env, target):
+ target = env.expect.that_target(target)
+ target.output_group(
+ "compilation_prerequisites_INTERNAL_",
+ ).contains_exactly([
+ "{package}/lib1.py",
+ "{package}/lib2.py",
+ "{package}/lib3.py",
+ ])
+ target.output_group(
+ "compilation_outputs",
+ ).contains_exactly([
+ "{package}/lib1.py",
+ "{package}/lib2.py",
+ "{package}/lib3.py",
+ ])
+
+_tests.append(_test_files_to_compile)
+
+def py_library_test_suite(name):
+ config = struct(rule = py_library, base_test_rule = py_library)
+ native.test_suite(
+ name = name,
+ tests = pt_util.create_tests(_tests, config = config) + create_base_tests(config),
+ )
diff --git a/tools/build_defs/python/tests/py_test/BUILD.bazel b/tools/build_defs/python/tests/py_test/BUILD.bazel
new file mode 100644
index 0000000..2dc0e5b
--- /dev/null
+++ b/tools/build_defs/python/tests/py_test/BUILD.bazel
@@ -0,0 +1,18 @@
+# Copyright 2023 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 py_test."""
+
+load(":py_test_tests.bzl", "py_test_test_suite")
+
+py_test_test_suite(name = "py_test_tests")
diff --git a/tools/build_defs/python/tests/py_test/py_test_tests.bzl b/tools/build_defs/python/tests/py_test/py_test_tests.bzl
new file mode 100644
index 0000000..1ecb252
--- /dev/null
+++ b/tools/build_defs/python/tests/py_test/py_test_tests.bzl
@@ -0,0 +1,107 @@
+# Copyright 2023 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 py_test."""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
+load("@rules_testing//lib:util.bzl", rt_util = "util")
+load("//python:defs.bzl", "py_test")
+load(
+ "//tools/build_defs/python/tests:py_executable_base_tests.bzl",
+ "create_executable_tests",
+)
+load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")
+
+# Explicit Label() calls are required so that it resolves in @rules_python context instead of
+# @rules_testing context.
+_FAKE_CC_TOOLCHAIN = Label("//tests/cc:cc_toolchain_suite")
+_FAKE_CC_TOOLCHAINS = [str(Label("//tests/cc:all"))]
+_PLATFORM_MAC = Label("//tools/build_defs/python/tests:mac")
+_PLATFORM_LINUX = Label("//tools/build_defs/python/tests:linux")
+
+_tests = []
+
+def _test_mac_requires_darwin_for_execution(name, config):
+ # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is
+ # a different object that isn't equal to any other, which prevents
+ # rules_testing from detecting it properly and fails with an error.
+ # This is fixed in Bazel 6+.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_subject.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_mac_requires_darwin_for_execution_impl,
+ target = name + "_subject",
+ config_settings = {
+ "//command_line_option:cpu": "darwin_x86_64",
+ "//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN,
+ "//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS,
+ "//command_line_option:platforms": [_PLATFORM_MAC],
+ },
+ )
+
+def _test_mac_requires_darwin_for_execution_impl(env, target):
+ env.expect.that_target(target).provider(
+ testing.ExecutionInfo,
+ ).requirements().keys().contains("requires-darwin")
+
+_tests.append(_test_mac_requires_darwin_for_execution)
+
+def _test_non_mac_doesnt_require_darwin_for_execution(name, config):
+ # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is
+ # a different object that isn't equal to any other, which prevents
+ # rules_testing from detecting it properly and fails with an error.
+ # This is fixed in Bazel 6+.
+ if not pt_util.is_bazel_6_or_higher():
+ rt_util.skip_test(name = name)
+ return
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_subject",
+ srcs = [name + "_subject.py"],
+ )
+ analysis_test(
+ name = name,
+ impl = _test_non_mac_doesnt_require_darwin_for_execution_impl,
+ target = name + "_subject",
+ config_settings = {
+ "//command_line_option:cpu": "k8",
+ "//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN,
+ "//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS,
+ "//command_line_option:platforms": [_PLATFORM_LINUX],
+ },
+ )
+
+def _test_non_mac_doesnt_require_darwin_for_execution_impl(env, target):
+ # Non-mac builds don't have the provider at all.
+ if testing.ExecutionInfo not in target:
+ return
+ env.expect.that_target(target).provider(
+ testing.ExecutionInfo,
+ ).requirements().keys().not_contains("requires-darwin")
+
+_tests.append(_test_non_mac_doesnt_require_darwin_for_execution)
+
+def py_test_test_suite(name):
+ config = struct(rule = py_test)
+ native.test_suite(
+ name = name,
+ tests = pt_util.create_tests(_tests, config = config) + create_executable_tests(config),
+ )
diff --git a/tools/build_defs/python/tests/py_wheel/BUILD.bazel b/tools/build_defs/python/tests/py_wheel/BUILD.bazel
new file mode 100644
index 0000000..d925bb9
--- /dev/null
+++ b/tools/build_defs/python/tests/py_wheel/BUILD.bazel
@@ -0,0 +1,18 @@
+# Copyright 2023 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 py_wheel."""
+
+load(":py_wheel_tests.bzl", "py_wheel_test_suite")
+
+py_wheel_test_suite(name = "py_wheel_tests")
diff --git a/tools/build_defs/python/tests/py_wheel/py_wheel_tests.bzl b/tools/build_defs/python/tests/py_wheel/py_wheel_tests.bzl
new file mode 100644
index 0000000..4408592
--- /dev/null
+++ b/tools/build_defs/python/tests/py_wheel/py_wheel_tests.bzl
@@ -0,0 +1,39 @@
+"""Test for py_wheel."""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
+load("@rules_testing//lib:truth.bzl", "matching")
+load("@rules_testing//lib:util.bzl", rt_util = "util")
+load("//python:packaging.bzl", "py_wheel")
+load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")
+
+_tests = []
+
+def _test_too_long_project_url_label(name, config):
+ rt_util.helper_target(
+ config.rule,
+ name = name + "_wheel",
+ distribution = name + "_wheel",
+ python_tag = "py3",
+ version = "0.0.1",
+ project_urls = {"This is a label whose length is above the limit!": "www.example.com"},
+ )
+ analysis_test(
+ name = name,
+ target = name + "_wheel",
+ impl = _test_too_long_project_url_label_impl,
+ expect_failure = True,
+ )
+
+def _test_too_long_project_url_label_impl(env, target):
+ env.expect.that_target(target).failures().contains_predicate(
+ matching.str_matches("in `project_urls` is too long"),
+ )
+
+_tests.append(_test_too_long_project_url_label)
+
+def py_wheel_test_suite(name):
+ config = struct(rule = py_wheel, base_test_rule = py_wheel)
+ native.test_suite(
+ name = name,
+ tests = pt_util.create_tests(_tests, config = config),
+ )
diff --git a/tools/build_defs/python/tests/util.bzl b/tools/build_defs/python/tests/util.bzl
new file mode 100644
index 0000000..9b386ca
--- /dev/null
+++ b/tools/build_defs/python/tests/util.bzl
@@ -0,0 +1,78 @@
+# Copyright 2023 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.
+"""Helpers and utilities multiple tests re-use."""
+
+load("@bazel_skylib//lib:structs.bzl", "structs")
+
+# Use this with is_windows()
+WINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")}
+
+def _create_tests(tests, **kwargs):
+ test_names = []
+ for func in tests:
+ test_name = _test_name_from_function(func)
+ func(name = test_name, **kwargs)
+ test_names.append(test_name)
+ return test_names
+
+def _test_name_from_function(func):
+ """Derives the name of the given rule implementation function.
+
+ Args:
+ func: the function whose name to extract
+
+ Returns:
+ The name of the given function. Note it will have leading and trailing
+ "_" stripped -- this allows passing a private function and having the
+ name of the test not start with "_".
+ """
+
+ # Starlark currently stringifies a function as "<function NAME>", so we use
+ # that knowledge to parse the "NAME" portion out.
+ # NOTE: This is relying on an implementation detail of Bazel
+ func_name = str(func)
+ func_name = func_name.partition("<function ")[-1]
+ func_name = func_name.rpartition(">")[0]
+ func_name = func_name.partition(" ")[0]
+ return func_name.strip("_")
+
+def _struct_with(s, **kwargs):
+ struct_dict = structs.to_dict(s)
+ struct_dict.update(kwargs)
+ return struct(**struct_dict)
+
+def _is_bazel_6_or_higher():
+ # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a
+ # different object that isn't equal to any other. This is fixed in bazel 6+.
+ return testing.ExecutionInfo == testing.ExecutionInfo
+
+def _is_windows(env):
+ """Tell if the target platform is windows.
+
+ This assumes the `WINDOWS_ATTR` attribute was added.
+
+ Args:
+ env: The test env struct
+ Returns:
+ True if the target is Windows, False if not.
+ """
+ constraint = env.ctx.attr.windows[platform_common.ConstraintValueInfo]
+ return env.ctx.target_platform_has_constraint(constraint)
+
+util = struct(
+ create_tests = _create_tests,
+ struct_with = _struct_with,
+ is_bazel_6_or_higher = _is_bazel_6_or_higher,
+ is_windows = _is_windows,
+)