diff options
author | Vinh Tran <vinhdaitran@google.com> | 2024-02-19 12:08:36 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-19 17:08:36 +0000 |
commit | 184da7d4ecac0f6c60f291d502e45f4829faa9d3 (patch) | |
tree | 5d65dab59deb03c7361018dba4fb8ab41a154a0b | |
parent | 05d2d5851d9c1843000b2582d9944bc5951e8eee (diff) | |
download | bazelbuild-rules_rust-184da7d4ecac0f6c60f291d502e45f4829faa9d3.tar.gz |
Introduce @rules_testing for Starlark tests (#2480)
This is a follow-up PR of
https://github.com/bazelbuild/rules_rust/pull/2422.
I'm scoping it to a separate PR to introduce
[@rules_testing](https://github.com/bazelbuild/rules_testing) (a new
Starlark testing framework). The framework removes a lot of boilerplate
code (unncessarily) required when writing analysis tests.
-rw-r--r-- | WORKSPACE.bazel | 7 | ||||
-rw-r--r-- | test/bindgen/BUILD.bazel | 3 | ||||
-rw-r--r-- | test/bindgen/bindgen_test.bzl | 53 | ||||
-rw-r--r-- | test/bindgen/main.rs | 7 | ||||
-rw-r--r-- | test/bindgen/simple.cc | 4 | ||||
-rw-r--r-- | test/bindgen/simple.h | 4 |
6 files changed, 78 insertions, 0 deletions
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index b520a3c3..d32161aa 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -76,3 +76,10 @@ http_archive( # # load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig") # rbe_preconfig(name = "buildkite_config", toolchain = "ubuntu2004-bazel-java11") + +http_archive( + name = "rules_testing", + sha256 = "b84ed8546f1969d700ead4546de9f7637e0f058d835e47e865dcbb13c4210aed", + strip_prefix = "rules_testing-0.5.0", + url = "https://github.com/bazelbuild/rules_testing/releases/download/v0.5.0/rules_testing-v0.5.0.tar.gz", +) diff --git a/test/bindgen/BUILD.bazel b/test/bindgen/BUILD.bazel new file mode 100644 index 00000000..c0263d2a --- /dev/null +++ b/test/bindgen/BUILD.bazel @@ -0,0 +1,3 @@ +load(":bindgen_test.bzl", "bindgen_test_suite") + +bindgen_test_suite(name = "cc_bindgen_test_suite") diff --git a/test/bindgen/bindgen_test.bzl b/test/bindgen/bindgen_test.bzl new file mode 100644 index 00000000..a5adf5df --- /dev/null +++ b/test/bindgen/bindgen_test.bzl @@ -0,0 +1,53 @@ +"""Analysis test for for rust_bindgen_library rule.""" + +load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library") +load("@rules_rust//rust:defs.bzl", "rust_binary") +load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") + +def _test_cc_linkopt_impl(env, target): + # Assert + env.expect.that_action(target.actions[0]) \ + .contains_at_least_args(["--codegen=link-arg=-shared"]) + +def _test_cc_linkopt(name): + # Arrange + cc_library( + name = name + "_cc", + srcs = ["simple.cc"], + hdrs = ["simple.h"], + linkopts = ["-shared"], + tags = ["manual"], + ) + rust_bindgen_library( + name = name + "_rust_bindgen", + cc_lib = name + "_cc", + header = "simple.h", + tags = ["manual"], + edition = "2021", + ) + rust_binary( + name = name + "_rust_binary", + srcs = ["main.rs"], + deps = [name + "_rust_bindgen"], + tags = ["manual"], + edition = "2021", + ) + + # Act + # TODO: Use targets attr to also verify `rust_bindgen_library` not having + # the linkopt after https://github.com/bazelbuild/rules_testing/issues/67 + # is released + analysis_test( + name = name, + target = name + "_rust_binary", + impl = _test_cc_linkopt_impl, + ) + +def bindgen_test_suite(name): + test_suite( + name = name, + tests = [ + _test_cc_linkopt, + ], + ) diff --git a/test/bindgen/main.rs b/test/bindgen/main.rs new file mode 100644 index 00000000..c5a415d2 --- /dev/null +++ b/test/bindgen/main.rs @@ -0,0 +1,7 @@ +// Analysis test shouldn't need this file. +// This is a workaround until +// https://github.com/bazelbuild/rules_rust/issues/2499 +// is fixed +fn main() { + println!("Hello world"); +} diff --git a/test/bindgen/simple.cc b/test/bindgen/simple.cc new file mode 100644 index 00000000..1cd12d51 --- /dev/null +++ b/test/bindgen/simple.cc @@ -0,0 +1,4 @@ +// Analysis test shouldn't need this file. +// This is a workaround until +// https://github.com/bazelbuild/rules_rust/issues/2499 +// is fixed diff --git a/test/bindgen/simple.h b/test/bindgen/simple.h new file mode 100644 index 00000000..1cd12d51 --- /dev/null +++ b/test/bindgen/simple.h @@ -0,0 +1,4 @@ +// Analysis test shouldn't need this file. +// This is a workaround until +// https://github.com/bazelbuild/rules_rust/issues/2499 +// is fixed |