mirror of https://github.com/bazelbuild/rules_rust
Inform users that `crate` and `srcs` are mutually exclusive (#1650)
This should avoid confusion in cases where users have defined tests with both `rust_test.crate` and `rust_test.srcs` set and later find `rust_test.srcs` are actually unused and not being tested. relates to https://github.com/bazelbuild/rules_rust/issues/2324
This commit is contained in:
parent
3089f92f19
commit
bf684354f5
|
@ -19,7 +19,6 @@ rust_binary(
|
|||
|
||||
rust_test(
|
||||
name = "unit_test",
|
||||
srcs = glob(["**/*.rs"]),
|
||||
crate = ":no_cargo_manifests",
|
||||
edition = "2018",
|
||||
)
|
||||
|
|
|
@ -69,7 +69,6 @@ rust_binary(
|
|||
|
||||
rust_test(
|
||||
name = "unit_test",
|
||||
srcs = glob(["**/*.rs"]),
|
||||
crate = ":vendor_local",
|
||||
edition = "2018",
|
||||
)
|
||||
|
|
|
@ -75,7 +75,6 @@ rust_binary(
|
|||
|
||||
rust_test(
|
||||
name = "unit_test",
|
||||
srcs = glob(["**/*.rs"]),
|
||||
crate = ":vendor_remote",
|
||||
edition = "2018",
|
||||
)
|
||||
|
|
|
@ -70,7 +70,6 @@ rust_binary(
|
|||
|
||||
rust_test(
|
||||
name = "unit_test",
|
||||
srcs = glob(["**/*.rs"]),
|
||||
crate = ":vendor_remote",
|
||||
edition = "2018",
|
||||
)
|
||||
|
|
|
@ -283,6 +283,12 @@ def _rust_test_impl(ctx):
|
|||
deps = transform_deps(ctx.attr.deps)
|
||||
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
|
||||
|
||||
if toolchain._incompatible_test_attr_crate_and_srcs_mutually_exclusive:
|
||||
if ctx.attr.crate and ctx.attr.srcs:
|
||||
fail("rust_test.crate and rust_test.srcs are mutually exclusive. Update {} to use only one of these attributes".format(
|
||||
ctx.label,
|
||||
))
|
||||
|
||||
if ctx.attr.crate:
|
||||
# Target is building the crate in `test` config
|
||||
crate = ctx.attr.crate[rust_common.crate_info] if rust_common.crate_info in ctx.attr.crate else ctx.attr.crate[rust_common.test_crate_info].crate
|
||||
|
|
|
@ -4,6 +4,11 @@ load(":incompatible.bzl", "incompatible_flag")
|
|||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
bzl_library(
|
||||
name = "bzl_lib",
|
||||
srcs = glob(["**/*.bzl"]),
|
||||
)
|
||||
|
||||
# A flag controlling whether to rename first-party crates such that their names
|
||||
# encode the Bazel package and target name, instead of just the target name.
|
||||
#
|
||||
|
@ -60,14 +65,16 @@ bool_flag(
|
|||
build_setting_default = False,
|
||||
)
|
||||
|
||||
bzl_library(
|
||||
name = "bzl_lib",
|
||||
srcs = glob(["**/*.bzl"]),
|
||||
)
|
||||
|
||||
# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain
|
||||
incompatible_flag(
|
||||
name = "experimental_toolchain_generated_sysroot",
|
||||
build_setting_default = True,
|
||||
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
|
||||
)
|
||||
|
||||
# A flag to make `rust_test.crate` and `rust_test.srcs` mutually exclusive.
|
||||
incompatible_flag(
|
||||
name = "incompatible_test_attr_crate_and_srcs_mutually_exclusive",
|
||||
build_setting_default = True,
|
||||
issue = "https://github.com/bazelbuild/rules_rust/issues/2324",
|
||||
)
|
||||
|
|
|
@ -643,6 +643,7 @@ def _rust_toolchain_impl(ctx):
|
|||
_experimental_use_global_allocator = experimental_use_global_allocator,
|
||||
_experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
|
||||
_experimental_toolchain_generated_sysroot = ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled,
|
||||
_incompatible_test_attr_crate_and_srcs_mutually_exclusive = ctx.attr._incompatible_test_attr_crate_and_srcs_mutually_exclusive[IncompatibleFlagInfo].enabled,
|
||||
_no_std = no_std,
|
||||
)
|
||||
return [
|
||||
|
@ -806,6 +807,9 @@ rust_toolchain = rule(
|
|||
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator."
|
||||
),
|
||||
),
|
||||
"_incompatible_test_attr_crate_and_srcs_mutually_exclusive": attr.label(
|
||||
default = Label("//rust/settings:incompatible_test_attr_crate_and_srcs_mutually_exclusive"),
|
||||
),
|
||||
"_no_std": attr.label(
|
||||
default = Label("//:no_std"),
|
||||
),
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
load("//rust:defs.bzl", "rust_library", "rust_test")
|
||||
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
rust_library(
|
||||
name = "inline",
|
||||
srcs = ["src/lib.rs"],
|
||||
edition = "2018",
|
||||
# Not all source files are included in this this target (`extra.rs`)
|
||||
# and as a result, rustfmt complains about a missing module. Do not
|
||||
# run rustfmt to avoid this issue.
|
||||
tags = ["norustfmt"],
|
||||
)
|
||||
|
||||
rust_test(
|
||||
name = "inline_test",
|
||||
srcs = ["src/extra.rs"],
|
||||
crate = ":inline",
|
||||
deps = ["//test/inline_test_with_deps/dep"],
|
||||
)
|
|
@ -1,4 +0,0 @@
|
|||
#[cfg(test)]
|
||||
pub(crate) fn extra_test_fn() -> u32 {
|
||||
100
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
#[allow(dead_code)]
|
||||
fn multiply(val: u32) -> u32 {
|
||||
val * 100
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod extra;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{extra, multiply};
|
||||
use dep::example_test_dep_fn;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(extra::extra_test_fn(), multiply(example_test_dep_fn()));
|
||||
}
|
||||
}
|
|
@ -30,9 +30,6 @@ rust_doc_test(
|
|||
rust_test_suite(
|
||||
name = "suite",
|
||||
srcs = glob(["tests/**"]),
|
||||
# Add the 'crate' argument, which will be passed as a kwarg
|
||||
# to the underlying rust_test rules. This will make OUT_DIR
|
||||
# available when compiling integration tests.
|
||||
crate = ":demo_lib",
|
||||
edition = "2018",
|
||||
deps = [":build_script"],
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue