Skip to content

Commit cdca6ed

Browse files
authored
Add test case for OUT_DIR in tests. (#954)
* Add test case for OUT_DIR in tests. * Add some env printing, and make the test fail on purpose. * Passing when OUT_DIR is used in a macro. * Cleanup * Remove Cargo.lock * Buildifier warnings=all * Remove Cargo.toml
1 parent 6f79458 commit cdca6ed

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

test/out_dir_in_tests/BUILD.bazel

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("//cargo:cargo_build_script.bzl", "cargo_build_script")
2+
load("//rust:defs.bzl", "rust_test_suite")
3+
load(
4+
"//rust:rust.bzl",
5+
"rust_library",
6+
"rust_test",
7+
)
8+
9+
cargo_build_script(
10+
name = "build_script",
11+
srcs = ["build.rs"],
12+
)
13+
14+
rust_library(
15+
name = "demo_lib",
16+
srcs = [
17+
"src/lib.rs",
18+
],
19+
deps = [":build_script"],
20+
)
21+
22+
rust_test(
23+
name = "demo_lib_test",
24+
crate = ":demo_lib",
25+
)
26+
27+
rust_test_suite(
28+
name = "suite",
29+
srcs = glob(["tests/**"]),
30+
# Add the 'crate' argument, which will be passed as a kwarg
31+
# to the underlying rust_test rules. This will make OUT_DIR
32+
# available when compiling integration tests.
33+
crate = ":demo_lib",
34+
)

test/out_dir_in_tests/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::env;
2+
use std::fs::File;
3+
use std::io::prelude::*;
4+
use std::path::PathBuf;
5+
6+
fn main() -> std::io::Result<()> {
7+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
8+
let mut file = File::create(out_path.join("test_content.txt"))?;
9+
file.write_all(b"Test content")?;
10+
Ok(())
11+
}

test/out_dir_in_tests/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use std::env;
4+
5+
#[test]
6+
fn can_find_the_out_dir_file() {
7+
// The file contents must be included via a macro.
8+
let contents = include_str!(concat!(env!("OUT_DIR"), "/test_content.txt"));
9+
assert_eq!("Test content", contents);
10+
}
11+
12+
#[test]
13+
fn no_out_dir_at_runtime() {
14+
// Cargo seems to set this at runtime as well, although the documentation
15+
// says it's only available at compile time.
16+
assert!(env::var("OUT_DIR").is_err());
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::env;
2+
3+
#[test]
4+
fn can_find_the_out_dir_file() {
5+
// The file contents must be included via a macro.
6+
let contents = include_str!(concat!(env!("OUT_DIR"), "/test_content.txt"));
7+
assert_eq!("Test content", contents);
8+
}
9+
10+
#[test]
11+
fn no_out_dir_at_runtime() {
12+
// Cargo seems to set this at runtime as well, although the documentation
13+
// says it's only available at compile time.
14+
assert!(env::var("OUT_DIR").is_err());
15+
}

0 commit comments

Comments
 (0)