Skip to content

Commit e0108ee

Browse files
qticascramm
authored and
scramm
committed
Introduce @rules_testing for Starlark tests (bazelbuild#2480)
This is a follow-up PR of bazelbuild#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.
1 parent 05d2d58 commit e0108ee

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

WORKSPACE.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ http_archive(
7676
#
7777
# load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")
7878
# rbe_preconfig(name = "buildkite_config", toolchain = "ubuntu2004-bazel-java11")
79+
80+
http_archive(
81+
name = "rules_testing",
82+
sha256 = "b84ed8546f1969d700ead4546de9f7637e0f058d835e47e865dcbb13c4210aed",
83+
strip_prefix = "rules_testing-0.5.0",
84+
url = "https://github.com/bazelbuild/rules_testing/releases/download/v0.5.0/rules_testing-v0.5.0.tar.gz",
85+
)

test/bindgen/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":bindgen_test.bzl", "bindgen_test_suite")
2+
3+
bindgen_test_suite(name = "cc_bindgen_test_suite")

test/bindgen/bindgen_test.bzl

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Analysis test for for rust_bindgen_library rule."""
2+
3+
load("@rules_cc//cc:defs.bzl", "cc_library")
4+
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")
5+
load("@rules_rust//rust:defs.bzl", "rust_binary")
6+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
7+
8+
def _test_cc_linkopt_impl(env, target):
9+
# Assert
10+
env.expect.that_action(target.actions[0]) \
11+
.contains_at_least_args(["--codegen=link-arg=-shared"])
12+
13+
def _test_cc_linkopt(name):
14+
# Arrange
15+
cc_library(
16+
name = name + "_cc",
17+
srcs = ["simple.cc"],
18+
hdrs = ["simple.h"],
19+
linkopts = ["-shared"],
20+
tags = ["manual"],
21+
)
22+
rust_bindgen_library(
23+
name = name + "_rust_bindgen",
24+
cc_lib = name + "_cc",
25+
header = "simple.h",
26+
tags = ["manual"],
27+
edition = "2021",
28+
)
29+
rust_binary(
30+
name = name + "_rust_binary",
31+
srcs = ["main.rs"],
32+
deps = [name + "_rust_bindgen"],
33+
tags = ["manual"],
34+
edition = "2021",
35+
)
36+
37+
# Act
38+
# TODO: Use targets attr to also verify `rust_bindgen_library` not having
39+
# the linkopt after https://github.com/bazelbuild/rules_testing/issues/67
40+
# is released
41+
analysis_test(
42+
name = name,
43+
target = name + "_rust_binary",
44+
impl = _test_cc_linkopt_impl,
45+
)
46+
47+
def bindgen_test_suite(name):
48+
test_suite(
49+
name = name,
50+
tests = [
51+
_test_cc_linkopt,
52+
],
53+
)

test/bindgen/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Analysis test shouldn't need this file.
2+
// This is a workaround until
3+
// https://github.com/bazelbuild/rules_rust/issues/2499
4+
// is fixed
5+
fn main() {
6+
println!("Hello world");
7+
}

test/bindgen/simple.cc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Analysis test shouldn't need this file.
2+
// This is a workaround until
3+
// https://github.com/bazelbuild/rules_rust/issues/2499
4+
// is fixed

test/bindgen/simple.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Analysis test shouldn't need this file.
2+
// This is a workaround until
3+
// https://github.com/bazelbuild/rules_rust/issues/2499
4+
// is fixed

0 commit comments

Comments
 (0)