Skip to content

Commit

Permalink
Make it possible for unit tests to have dev deps (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBowyer authored and mfarrugi committed Apr 6, 2019
1 parent b3cc7e4 commit 2215277
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/ffi/rust_calling_c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust_library(

rust_test(
name = "matrix_test",
deps = [":matrix"],
crate = ":matrix",
)

rust_doc(
Expand All @@ -41,7 +41,7 @@ rust_library(

rust_test(
name = "matrix_dylib_test",
deps = [":matrix_dynamically_linked"],
crate = ":matrix_dynamically_linked",
)

rust_doc(
Expand Down
2 changes: 1 addition & 1 deletion examples/ffi/rust_calling_c/simple/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ rust_binary(

rust_test(
name = "simple_test",
deps = [":simple_example"],
crate = ":simple_example",
)
2 changes: 1 addition & 1 deletion examples/fibonacci/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust_library(

rust_test(
name = "fibonacci_test",
deps = [":fibonacci"],
crate = ":fibonacci",
)

rust_benchmark(
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ rust_library(

rust_test(
name = "hello_lib_test",
deps = [":hello_lib"],
crate = ":hello_lib",
)

rust_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_out_dir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ rust_binary(
rust_test(
name = "hello_out_dir_test",
out_dir_tar = ":repacked_srcs.tar.gz",
deps = [":hello_out_dir"],
crate = ":hello_out_dir",
)
2 changes: 1 addition & 1 deletion examples/per_platform_printer/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ rust_library(

rust_test(
name = "printer_test",
deps = [":printer"],
crate = ":printer",
)
80 changes: 70 additions & 10 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain")

_OLD_INLINE_TEST_CRATE_MSG = """
--------------------------------------------------------------------------------
Testing crates with inline `#[cfg(test)]` attributes is now handled with the
`crate` attribute in `rust_test`:
This means you should migrate from:
```
rust_test(
name = "{name}",
deps = ["{dep}"],
...
)
```
to the following:
```
rust_test(
name = "{name}",
crate = "{dep}",
...
```
See https://github.com/bazelbuild/rules_rust/pull/203 for more details
--------------------------------------------------------------------------------
"""

# TODO(marco): Separate each rule into its own file.

def _determine_output_hash(lib_rs):
Expand Down Expand Up @@ -124,19 +152,26 @@ def _rust_test_common(ctx, test_binary):
"""
toolchain = find_toolchain(ctx)

if len(ctx.attr.deps) == 1 and len(ctx.files.srcs) == 0:
# Target has a single dependency but no srcs. Build the test binary using
# the dependency's srcs.
parent_crate = ctx.attr.deps[0][CrateInfo]
if ctx.attr.crate:
# Target is building the crate in `test` config
# Build the test binary using the dependency's srcs.
crate = ctx.attr.crate[CrateInfo]
target = CrateInfo(
name = test_binary.basename,
type = parent_crate.type,
root = parent_crate.root,
srcs = parent_crate.srcs,
deps = parent_crate.deps,
type = crate.type,
root = crate.root,
srcs = crate.srcs + ctx.files.srcs,
deps = crate.deps + ctx.attr.deps,
output = test_binary,
edition = parent_crate.edition,
edition = crate.edition,
)
elif len(ctx.attr.deps) == 1 and len(ctx.files.srcs) == 0:
dep = ctx.attr.deps[0].label
msg = _OLD_INLINE_TEST_CRATE_MSG.format(
name=test_binary.basename,
dep=dep if ctx.label.package != dep.package else ":" + dep.name
)
fail(msg)
else:
# Target is a standalone crate. Build the test binary as its own crate.
target = CrateInfo(
Expand Down Expand Up @@ -281,6 +316,18 @@ _rust_library_attrs = {
),
}

_rust_test_attrs = {
"crate": attr.label(
mandatory = False,
doc = _tidy("""
Target inline tests declared in the given crate
These tests are typically those that would be held out under
`#[cfg(test)]` declarations.
"""),
),
}

rust_library = rule(
_rust_library_impl,
attrs = dict(_rust_common_attrs.items() +
Expand Down Expand Up @@ -459,7 +506,8 @@ Hello world

rust_test = rule(
_rust_test_impl,
attrs = _rust_common_attrs,
attrs = dict(_rust_common_attrs.items() +
_rust_test_attrs.items()),
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
Expand Down Expand Up @@ -532,6 +580,18 @@ rust_test(
Run the test with `bazel build //hello_lib:hello_lib_test`.
To run a crate or lib with the `#[cfg(test)]` configuration, handling inline
tests, you should specify the crate directly like so.
```
rust_test(
name = "hello_lib_test",
crate = ":hello_lib",
# You may add other deps that are specific to the test configuration
deps = ["//some/dev/dep"],
)
```
### Example: `test` directory
Integration tests that live in the [`tests` directory][int-tests], they are
Expand Down
6 changes: 3 additions & 3 deletions test/chained_direct_deps/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ rust_library(

rust_test(
name = "mod1_test",
deps = [":mod1"],
crate = ":mod1",
)

rust_test(
name = "mod2_test",
deps = [":mod2"],
crate = ":mod2",
)

rust_test(
name = "mod3_test",
deps = [":mod3"],
crate = ":mod3",
)

rust_doc_test(
Expand Down
4 changes: 1 addition & 3 deletions test/conflicting_deps/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ rust_library(

rust_test(
name = "conflicting_deps_test",
deps = [
":conflicting_deps",
],
crate = ":conflicting_deps",
)
Empty file.
11 changes: 11 additions & 0 deletions test/inline_test_with_deps/dep/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package(default_visibility = ["//visibility:public"])

load(
"//rust:rust.bzl",
"rust_library",
)

rust_library(
name = "dep",
srcs = ["src/lib.rs"],
)
4 changes: 4 additions & 0 deletions test/inline_test_with_deps/dep/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// This exists purely to give us a dep to compile against
pub fn example_test_dep_fn() -> u32 {
1
}
19 changes: 19 additions & 0 deletions test/inline_test_with_deps/test/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])

load(
"//rust:rust.bzl",
"rust_library",
"rust_test",
)

rust_library(
name = "inline",
edition = "2018",
srcs = ["src/lib.rs"],
)

rust_test(
name = "inline_test",
crate = ":inline",
deps = ["//test/inline_test_with_deps/dep"],
)
15 changes: 15 additions & 0 deletions test/inline_test_with_deps/test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[allow(dead_code)]
fn multiply(val: u32) -> u32 {
val * 100
}

#[cfg(test)]
mod tests {
use super::multiply;
use dep::example_test_dep_fn;

#[test]
fn test() {
assert_eq!(100, multiply(example_test_dep_fn()));
}
}
20 changes: 20 additions & 0 deletions test/inline_test_with_deps/test_with_srcs/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package(default_visibility = ["//visibility:public"])

load(
"//rust:rust.bzl",
"rust_library",
"rust_test",
)

rust_library(
name = "inline",
edition = "2018",
srcs = ["src/lib.rs"],
)

rust_test(
name = "inline_test",
crate = ":inline",
srcs = ["src/extra.rs"],
deps = ["//test/inline_test_with_deps/dep"],
)
4 changes: 4 additions & 0 deletions test/inline_test_with_deps/test_with_srcs/src/extra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(test)]
pub(crate) fn extra_test_fn() -> u32 {
100
}
18 changes: 18 additions & 0 deletions test/inline_test_with_deps/test_with_srcs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[allow(dead_code)]
fn multiply(val: u32) -> u32 {
val * 100
}

#[cfg(test)]
mod extra;

#[cfg(test)]
mod tests {
use super::{multiply, extra};
use dep::example_test_dep_fn;

#[test]
fn test() {
assert_eq!(extra::extra_test_fn(), multiply(example_test_dep_fn()));
}
}
2 changes: 1 addition & 1 deletion tools/runfiles/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust_library(
rust_test(
name = "runfiles_test",
data = ["data/sample.txt"],
deps = [":runfiles"],
crate = ":runfiles",
)

rust_doc_test(
Expand Down

0 comments on commit 2215277

Please sign in to comment.