Skip to content

Commit

Permalink
rustdoc_test: substitute the root of the current crate (bazelbuild#1777)
Browse files Browse the repository at this point in the history
Otherwise a trivial little crate with no rust_library dependencies, and
a build script dependency, fails. This is unusual because most crates
complex enough to have a build script also have other dependencies, I
ran into while minimizing a test case for another bug.
  • Loading branch information
bsilver8192 authored Jan 17, 2023
1 parent a52041f commit 4e89d52
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def _construct_writer_arguments(ctx, test_runner, opt_test_params, action, crate
# Collect and dedupe all of the file roots in a list before appending
# them to args to prevent generating a large amount of identical args
roots = []
root = crate_info.output.root.path
if not root in roots:
roots.append(root)
for dep in crate_info.deps.to_list():
dep_crate_info = getattr(dep, "crate_info", None)
dep_dep_info = getattr(dep, "dep_info", None)
Expand Down
3 changes: 3 additions & 0 deletions test/unit/rustdoc/rustdoc_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-env=CONST=xyz");
}
3 changes: 3 additions & 0 deletions test/unit/rustdoc/rustdoc_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use rustdoc_proc_macro::make_answer;
#[cfg(feature = "with_proc_macro")]
make_answer!();

#[cfg(feature = "with_build_script")]
pub const CONST: &str = env!("CONST");

/// The answer to the ultimate question
/// ```
/// fn answer() -> u32 { 42 }
Expand Down
28 changes: 28 additions & 0 deletions test/unit/rustdoc/rustdoc_nodep_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[cfg(feature = "with_proc_macro")]
use rustdoc_proc_macro::make_answer;

#[cfg(feature = "with_proc_macro")]
make_answer!();

#[cfg(feature = "with_build_script")]
pub const CONST: &str = env!("CONST");

/// The answer to the ultimate question
/// ```
/// fn answer() -> u32 { 42 }
/// assert_eq!(answer(), 42);
/// ```
#[cfg(not(feature = "with_proc_macro"))]
pub fn answer() -> u32 {
42
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_answer() {
assert_eq!(answer(), 42);
}
}
46 changes: 46 additions & 0 deletions test/unit/rustdoc/rustdoc_unit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@bazel_skylib//lib:unittest.bzl", "analysistest")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//cargo:defs.bzl", "cargo_build_script")
load("//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_proc_macro", "rust_test")
load(
"//test/unit:common.bzl",
Expand Down Expand Up @@ -118,6 +119,12 @@ def _define_targets():
rustdoc_deps = [":adder"],
)

_target_maker(
rust_library,
name = "nodep_lib",
srcs = ["rustdoc_nodep_lib.rs"],
)

_target_maker(
rust_proc_macro,
name = "rustdoc_proc_macro",
Expand All @@ -133,6 +140,14 @@ def _define_targets():
crate_features = ["with_proc_macro"],
)

_target_maker(
rust_library,
name = "lib_nodep_with_proc_macro",
srcs = ["rustdoc_nodep_lib.rs"],
proc_macro_deps = [":rustdoc_proc_macro"],
crate_features = ["with_proc_macro"],
)

_target_maker(
rust_binary,
name = "bin_with_transitive_proc_macro",
Expand All @@ -157,6 +172,37 @@ def _define_targets():
deps = [":cc_lib"],
)

_target_maker(
rust_library,
name = "lib_nodep_with_cc",
srcs = ["rustdoc_nodep_lib.rs"],
crate_features = ["with_cc"],
deps = [":cc_lib"],
)

cargo_build_script(
name = "lib_build_script",
srcs = ["rustdoc_build.rs"],
edition = "2018",
)

_target_maker(
rust_library,
name = "lib_with_build_script",
srcs = ["rustdoc_lib.rs"],
rustdoc_deps = [":adder"],
crate_features = ["with_build_script"],
deps = [":lib_build_script"],
)

_target_maker(
rust_library,
name = "lib_nodep_with_build_script",
srcs = ["rustdoc_nodep_lib.rs"],
crate_features = ["with_build_script"],
deps = [":lib_build_script"],
)

def rustdoc_test_suite(name):
"""Entry-point macro called from the BUILD file.
Expand Down

0 comments on commit 4e89d52

Please sign in to comment.