Skip to content

Commit

Permalink
Use crate_info.deps and others instead of ctx.rule.attr.deps in rust …
Browse files Browse the repository at this point in the history
…analyzer aspect
  • Loading branch information
hlopko committed May 27, 2021
1 parent 5bb12cc commit a82d193
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 2 deletions.
9 changes: 8 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def _rust_test_common(ctx, toolchain, output):
toolchain = toolchain,
crate_type = crate_type,
crate_info = crate_info,
rust_flags = ["--test"],
rust_flags = ["--test"] if ctx.attr.harness else ["--cfg", "test"],
)

return _create_test_launcher(ctx, toolchain, output, providers)
Expand Down Expand Up @@ -703,6 +703,13 @@ _rust_test_attrs = {
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
"""),
),
"harness": attr.bool(
mandatory = False,
default = True,
doc = _tidy("""\
Whether to use libtest (defaults to True).
"""),
),
"_launcher": attr.label(
executable = True,
default = Label("//util/launcher:launcher"),
Expand Down
5 changes: 4 additions & 1 deletion rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _rust_analyzer_aspect_impl(target, ctx):
dep_infos = [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.deps if RustAnalyzerInfo in dep]
if hasattr(ctx.rule.attr, "proc_macro_deps"):
dep_infos += [dep[RustAnalyzerInfo] for dep in ctx.rule.attr.proc_macro_deps if RustAnalyzerInfo in dep]
if hasattr(ctx.rule.attr, "crate"):
dep_infos.append(ctx.rule.attr.crate[RustAnalyzerInfo])

transitive_deps = depset(direct = dep_infos, order = "postorder", transitive = [dep.transitive_deps for dep in dep_infos])

crate_info = target[rust_common.crate_info]
Expand Down Expand Up @@ -102,7 +105,7 @@ def find_proc_macro_dylib_path(toolchain, target):
return None

rust_analyzer_aspect = aspect(
attr_aspects = ["deps", "proc_macro_deps"],
attr_aspects = ["deps", "proc_macro_deps", "crate"],
implementation = _rust_analyzer_aspect_impl,
toolchains = [str(Label("//rust:toolchain"))],
incompatible_use_toolchain_transition = True,
Expand Down
49 changes: 49 additions & 0 deletions test/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_proc_macro", "rust_test")

rust_library(
name = "mylib",
srcs = ["mylib.rs"],
proc_macro_deps = [":proc_macro_dep"],
deps = [":lib_dep"],
)

rust_library(
name = "lib_dep",
srcs = ["lib_dep.rs"],
)

rust_proc_macro(
name = "proc_macro_dep",
srcs = ["proc_macro_dep.rs"],
)

rust_test(
name = "mylib_test",
crate = ":mylib",
proc_macro_deps = [":extra_proc_macro_dep"],
deps = [":extra_test_dep"],
)

rust_library(
name = "extra_test_dep",
srcs = ["extra_test_dep.rs"],
)

rust_proc_macro(
name = "extra_proc_macro_dep",
srcs = ["extra_proc_macro_dep.rs"],
)

rust_analyzer(
name = "rust_analyzer",
testonly = True,
targets = [":mylib_test"],
)

rust_test(
name = "rust_project_json_test",
srcs = ["rust_project_json_test.rs"],
args = ["$(location :rust-project.json)"],
data = [":rust-project.json"],
harness = False,
)
Empty file.
Empty file.
Empty file added test/rust_analyzer/lib_dep.rs
Empty file.
Empty file added test/rust_analyzer/mylib.rs
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions test/rust_analyzer/rust_project_json_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::env;

fn main() {
let args: Vec<String> = env::args().collect();
let rust_project_path = args.get(1).expect("expected rust-project.json path as first argument.");
let content = std::fs::read_to_string(rust_project_path).expect(&format!("couldn't open {}", rust_project_path));

for dep in &["lib_dep", "extra_test_dep", "proc_macro_dep", "extra_proc_macro_dep"] {
if !content.contains(dep) {
panic!("expected rust-project.json to contain {}.", dep);
}
}
}
4 changes: 4 additions & 0 deletions test/unit/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load(":rust_analyzer_test.bzl", "rust_analyzer_test_suite")

############################ UNIT TESTS #############################
rust_analyzer_test_suite(name = "rust_analyzer_test_suite")
Empty file.
52 changes: 52 additions & 0 deletions test/unit/rust_analyzer/rust_analyzer_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Unittests for rust rules."""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

# This is an unittest for rust_analyzer.bzl, violating private visibility is necessary.
# buildifier: disable=bzl-visibility
load("//rust:defs.bzl", "rust_analyzer", "rust_library")

def _rust_analyzer_hello_world_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
asserts.true(env, len(tut.actions) == 1, "expected one action, got %s" % len(tut.actions))
action = tut.actions[0]
outputs = action.outputs.to_list()
asserts.true(env, len(outputs) == 1, "expected one output, got %s" % len(outputs))
output = outputs[0]
asserts.true(env, output.path.endswith("rust-project.json"))
return analysistest.end(env)

rust_analyzer_hello_world_test = analysistest.make(_rust_analyzer_hello_world_test_impl)

def _rust_analyzer_test():
rust_library(
name = "mylib",
srcs = ["mylib.rs"],
)

rust_analyzer(
name = "rust_analyzer",
testonly = True,
targets = [":mylib"],
)

rust_analyzer_hello_world_test(
name = "rust_analyzer_hello_world_test",
target_under_test = ":rust_analyzer",
)

def rust_analyzer_test_suite(name):
"""Entry-point macro called from the BUILD file.
Args:
name: Name of the macro.
"""
_rust_analyzer_test()

native.test_suite(
name = name,
tests = [
":rust_analyzer_hello_world_test",
],
)

0 comments on commit a82d193

Please sign in to comment.