Skip to content

Commit

Permalink
tests: move various supporting code under tests/support (#2183)
Browse files Browse the repository at this point in the history
tests: move various supporting code under tests/support
* Move subject classes under support
* Switch to using rules_testing DefaultInfo subject
* Move fake cc toolchains under tests/support/cc_toolchains (these
  are used in several places, not just tests/cc)
* Make literal references of old //tests/cc locations use the
  constants from support.bzl

This is both for code clarity (support code being in a common location),
but also to make it easier to import the support code for the subset
of tests Google imports and runs.
  • Loading branch information
rickeylev authored Sep 5, 2024
1 parent fe1d9a7 commit 612baef
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 182 deletions.
2 changes: 1 addition & 1 deletion tests/base_rules/base_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ 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("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
load("//tests/base_rules:py_info_subject.bzl", "py_info_subject")
load("//tests/base_rules:util.bzl", pt_util = "util")
load("//tests/support:py_info_subject.bzl", "py_info_subject")

_tests = []

Expand Down
2 changes: 1 addition & 1 deletion tests/base_rules/precompile/precompile_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load("//python:py_binary.bzl", "py_binary")
load("//python:py_info.bzl", "PyInfo")
load("//python:py_library.bzl", "py_library")
load("//python:py_test.bzl", "py_test")
load("//tests/base_rules:py_info_subject.bzl", "py_info_subject")
load("//tests/support:py_info_subject.bzl", "py_info_subject")
load(
"//tests/support:support.bzl",
"CC_TOOLCHAIN",
Expand Down
10 changes: 5 additions & 5 deletions tests/base_rules/py_executable_base_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
load("//tests/support:support.bzl", "LINUX_X86_64", "WINDOWS_X86_64")
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")

_BuiltinPyRuntimeInfo = PyRuntimeInfo

Expand Down Expand Up @@ -51,8 +51,8 @@ def _test_basic_windows(name, config):
# platforms.
"//command_line_option:build_python_zip": "true",
"//command_line_option:cpu": "windows_x86_64",
"//command_line_option:crosstool_top": Label("//tests/cc:cc_toolchain_suite"),
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
"//command_line_option:platforms": [WINDOWS_X86_64],
},
attr_values = {"target_compatible_with": target_compatible_with},
Expand Down Expand Up @@ -96,8 +96,8 @@ def _test_basic_zip(name, config):
# platforms.
"//command_line_option:build_python_zip": "true",
"//command_line_option:cpu": "linux_x86_64",
"//command_line_option:crosstool_top": Label("//tests/cc:cc_toolchain_suite"),
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
"//command_line_option:platforms": [LINUX_X86_64],
},
attr_values = {"target_compatible_with": target_compatible_with},
Expand Down
15 changes: 5 additions & 10 deletions tests/base_rules/py_test/py_test_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ load(
"create_executable_tests",
)
load("//tests/base_rules:util.bzl", pt_util = "util")
load("//tests/support:support.bzl", "LINUX_X86_64", "MAC_X86_64")

# 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"))]
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "MAC_X86_64")

# The Windows CI currently runs as root, which breaks when
# the analysis tests try to install (but not use, because
Expand Down Expand Up @@ -63,8 +58,8 @@ def _test_mac_requires_darwin_for_execution(name, config):
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:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
"//command_line_option:platforms": [MAC_X86_64],
},
attr_values = _SKIP_WINDOWS,
Expand Down Expand Up @@ -96,8 +91,8 @@ def _test_non_mac_doesnt_require_darwin_for_execution(name, config):
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:crosstool_top": CROSSTOOL_TOP,
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
"//command_line_option:platforms": [LINUX_X86_64],
},
attr_values = _SKIP_WINDOWS,
Expand Down
137 changes: 0 additions & 137 deletions tests/cc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,140 +11,3 @@
# 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("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS")
load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load(":fake_cc_toolchain_config.bzl", "fake_cc_toolchain_config")

package(default_visibility = ["//:__subpackages__"])

exports_files(["fake_header.h"])

filegroup(
name = "libpython",
srcs = ["libpython-fake.so"],
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
)

toolchain(
name = "fake_py_cc_toolchain",
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
toolchain = ":fake_py_cc_toolchain_impl",
toolchain_type = "@rules_python//python/cc:toolchain_type",
)

py_cc_toolchain(
name = "fake_py_cc_toolchain_impl",
headers = ":fake_headers",
libs = ":fake_libs",
python_version = "3.999",
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
)

# buildifier: disable=native-cc
cc_library(
name = "fake_headers",
hdrs = ["fake_header.h"],
data = ["data.txt"],
includes = ["fake_include"],
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
)

# buildifier: disable=native-cc
cc_library(
name = "fake_libs",
srcs = ["libpython3.so"],
data = ["libdata.txt"],
tags = PREVENT_IMPLICIT_BUILDING_TAGS,
)

cc_toolchain_suite(
name = "cc_toolchain_suite",
tags = ["manual"],
toolchains = {
"darwin_x86_64": ":mac_toolchain",
"k8": ":linux_toolchain",
"windows_x86_64": ":windows_toolchain",
},
)

filegroup(name = "empty")

cc_toolchain(
name = "mac_toolchain",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
toolchain_config = ":mac_toolchain_config",
toolchain_identifier = "mac-toolchain",
)

toolchain(
name = "mac_toolchain_definition",
target_compatible_with = ["@platforms//os:macos"],
toolchain = ":mac_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

fake_cc_toolchain_config(
name = "mac_toolchain_config",
target_cpu = "darwin_x86_64",
toolchain_identifier = "mac-toolchain",
)

cc_toolchain(
name = "linux_toolchain",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
toolchain_config = ":linux_toolchain_config",
toolchain_identifier = "linux-toolchain",
)

toolchain(
name = "linux_toolchain_definition",
target_compatible_with = ["@platforms//os:linux"],
toolchain = ":linux_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

fake_cc_toolchain_config(
name = "linux_toolchain_config",
target_cpu = "k8",
toolchain_identifier = "linux-toolchain",
)

cc_toolchain(
name = "windows_toolchain",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
toolchain_config = ":windows_toolchain_config",
toolchain_identifier = "windows-toolchain",
)

toolchain(
name = "windows_toolchain_definition",
target_compatible_with = ["@platforms//os:windows"],
toolchain = ":windows_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

fake_cc_toolchain_config(
name = "windows_toolchain_config",
target_cpu = "windows_x86_64",
toolchain_identifier = "windows-toolchain",
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
load("@rules_cc//cc:defs.bzl", "CcInfo")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching")
load("//tests:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:support.bzl", "CC_TOOLCHAIN")

_tests = []

Expand All @@ -27,11 +28,11 @@ def _test_current_toolchain_headers(name):
impl = _test_current_toolchain_headers_impl,
target = "//python/cc:current_py_cc_headers",
config_settings = {
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
},
attrs = {
"header": attr.label(
default = "//tests/cc:fake_header.h",
default = "//tests/support/cc_toolchains:fake_header.h",
allow_single_file = True,
),
},
Expand All @@ -58,7 +59,7 @@ def _test_current_toolchain_headers_impl(env, target):

# Check that the forward DefaultInfo looks correct
env.expect.that_target(target).runfiles().contains_predicate(
matching.str_matches("*/cc/data.txt"),
matching.str_matches("*/cc_toolchains/data.txt"),
)

_tests.append(_test_current_toolchain_headers)
Expand Down
6 changes: 3 additions & 3 deletions tests/cc/current_py_cc_libs/current_py_cc_libs_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
load("@rules_cc//cc:defs.bzl", "CcInfo")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching")
load("//tests:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")

_tests = []

Expand All @@ -27,11 +27,11 @@ def _test_current_toolchain_libs(name):
impl = _test_current_toolchain_libs_impl,
target = "//python/cc:current_py_cc_libs",
config_settings = {
"//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
"//command_line_option:extra_toolchains": [str(Label("//tests/support/cc_toolchains:all"))],
},
attrs = {
"lib": attr.label(
default = "//tests/cc:libpython",
default = "//tests/support/cc_toolchains:libpython",
allow_single_file = True,
),
},
Expand Down
21 changes: 7 additions & 14 deletions tests/cc/py_cc_toolchain/py_cc_toolchain_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
load("//tests:cc_info_subject.bzl", "cc_info_subject")
load("//tests:default_info_subject.bzl", "default_info_subject")
load("//tests:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")
load("//tests/support:cc_info_subject.bzl", "cc_info_subject")
load("//tests/support:py_cc_toolchain_info_subject.bzl", "PyCcToolchainInfoSubject")

_tests = []

def _py_cc_toolchain_test(name):
analysis_test(
name = name,
impl = _py_cc_toolchain_test_impl,
target = "//tests/cc:fake_py_cc_toolchain_impl",
target = "//tests/support/cc_toolchains:fake_py_cc_toolchain_impl",
attrs = {
"header": attr.label(
default = "//tests/cc:fake_header.h",
default = "//tests/support/cc_toolchains:fake_header.h",
allow_single_file = True,
),
},
Expand Down Expand Up @@ -63,15 +62,9 @@ def _py_cc_toolchain_test_impl(env, target):
matching.str_matches("*/fake_include"),
])

# TODO: Once subjects.default_info is available, do
# default_info = headers_providers.get("DefaultInfo", factory=subjects.default_info)
# https://github.com/bazelbuild/rules_python/issues/1297
default_info = default_info_subject(
headers_providers.get("DefaultInfo", factory = lambda v, meta: v),
meta = env.expect.meta.derive(expr = "default_info"),
)
default_info = headers_providers.get("DefaultInfo", factory = subjects.default_info)
default_info.runfiles().contains_predicate(
matching.str_matches("*/cc/data.txt"),
matching.str_matches("*/cc_toolchains/data.txt"),
)

libs_providers = toolchain.libs().providers_map()
Expand All @@ -82,7 +75,7 @@ def _py_cc_toolchain_test_impl(env, target):
cc_info.linking_context().linker_inputs().has_size(2)

default_info = libs_providers.get("DefaultInfo", factory = subjects.default_info)
default_info.runfiles().contains("{workspace}/tests/cc/libdata.txt")
default_info.runfiles().contains("{workspace}/tests/support/cc_toolchains/libdata.txt")
default_info.runfiles().contains_predicate(
matching.str_matches("/libpython3."),
)
Expand Down
3 changes: 2 additions & 1 deletion tests/config_settings/transition/multi_version_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load("//python:py_info.bzl", "PyInfo")
load("//python/config_settings:transition.bzl", py_binary_transitioned = "py_binary", py_test_transitioned = "py_test")
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
load("//tests/support:support.bzl", "CC_TOOLCHAIN")

# NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel
# and if you find tests failing, it could be because of the toolchain resolution issues here.
Expand Down Expand Up @@ -87,7 +88,7 @@ def _setup_py_binary_windows(name, *, impl, build_python_zip):
impl = impl,
config_settings = {
"//command_line_option:build_python_zip": build_python_zip,
"//command_line_option:extra_toolchains": "//tests/cc:all",
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
"//command_line_option:platforms": str(Label("//tests/support:windows_x86_64")),
},
)
Expand Down
2 changes: 1 addition & 1 deletion tests/py_runtime/py_runtime_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject")
load("//tests/base_rules:util.bzl", br_util = "util")
load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject")

_tests = []

Expand Down
5 changes: 3 additions & 2 deletions tests/py_runtime_pair/py_runtime_pair_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ load("//python:py_binary.bzl", "py_binary")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject")
load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject")
load("//tests/support:support.bzl", "CC_TOOLCHAIN")

def _toolchain_factory(value, meta):
return subjects.struct(
Expand Down Expand Up @@ -129,7 +130,7 @@ def _test_py_runtime_pair_and_binary(name):
config_settings = {
"//command_line_option:extra_toolchains": [
"//tests/py_runtime_pair:{}_toolchain".format(name),
"//tests/cc:all",
CC_TOOLCHAIN,
],
},
)
Expand Down
3 changes: 2 additions & 1 deletion tests/runtime_env_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
load(":runtime_env_toolchain_tests.bzl", "runtime_env_toolchain_test_suite")

runtime_env_toolchain_test_suite(name = "runtime_env_toolchain_tests")
Expand All @@ -26,7 +27,7 @@ py_reconfig_test(
extra_toolchains = [
"//python/runtime_env_toolchains:all",
# Necessary for RBE CI
"//tests/cc:all",
CC_TOOLCHAIN,
],
main = "toolchain_runs_test.py",
deps = ["//python/runfiles"],
Expand Down
File renamed without changes.
Loading

0 comments on commit 612baef

Please sign in to comment.