Skip to content

Commit 6d9706f

Browse files
committed
ctest: Add generation, compilation, and running of tests for constants.
1 parent 8811d19 commit 6d9706f

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

ctest-next/build.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
use std::env;
22

33
fn main() {
4-
println!(
5-
"cargo:rustc-env={}={}",
6-
"HOST_PLATFORM",
7-
env::var("HOST").unwrap()
8-
);
9-
println!(
10-
"cargo:rustc-env={}={}",
11-
"TARGET_PLATFORM",
12-
env::var("TARGET").unwrap()
13-
);
14-
println!(
15-
"cargo:rustc-env={}={}",
16-
"LINKER",
17-
env::var("CC").unwrap_or("".to_string())
18-
);
19-
println!("cargo:rerun-if-changed-env=TARGET")
4+
let host = env::var("HOST").unwrap();
5+
let target = env::var("TARGET").unwrap();
6+
let target_key = target.replace('-', "_");
7+
8+
println!("cargo:rustc-env=HOST_PLATFORM={host}");
9+
println!("cargo:rustc-env=TARGET_PLATFORM={target}");
10+
11+
let linker = env::var(format!("CARGO_TARGET_{}_LINKER", target_key.to_uppercase()))
12+
.or_else(|_| env::var("CC"))
13+
.or_else(|_| env::var(format!("CC_{}", target_key)))
14+
.unwrap_or_default();
15+
16+
let runner =
17+
env::var(format!("CARGO_TARGET_{}_RUNNER", target_key.to_uppercase())).unwrap_or_default();
18+
19+
// As we invoke rustc directly this does not get passed to it, although RUSTFLAGS does.
20+
let flags = env::var(format!(
21+
"CARGO_TARGET_{}_RUSTFLAGS",
22+
target_key.to_uppercase()
23+
))
24+
.unwrap_or_default();
25+
26+
println!("cargo:rustc-env=LINKER={linker}");
27+
println!("cargo:rustc-env=RUNNER={runner}");
28+
println!("cargo:rustc-env=FLAGS={flags}");
29+
30+
println!("cargo:rerun-if-changed-env=TARGET");
2031
}

ctest-next/lib

3.86 MB
Binary file not shown.

ctest-next/src/runner.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ pub fn compile_test<P: AsRef<Path>>(
4848
if !linker.is_empty() {
4949
cmd.arg(format!("-Clinker={}", linker));
5050
}
51+
52+
let flags = env::var("FLAGS").unwrap();
53+
if !flags.is_empty() {
54+
cmd.args(flags.split_whitespace().collect::<Vec<_>>());
55+
}
56+
5157
let output = cmd.output()?;
5258

5359
if !output.status.success() {
@@ -58,8 +64,17 @@ pub fn compile_test<P: AsRef<Path>>(
5864
}
5965

6066
/// Run the compiled test binary.
61-
pub fn run_test<P: AsRef<Path>>(test_binary: P) -> Result<String> {
62-
let output = Command::new(test_binary.as_ref()).output()?;
67+
pub fn run_test(test_binary: &str) -> Result<String> {
68+
let cmd = env::var("RUNNER").unwrap();
69+
let output = if cmd.is_empty() {
70+
Command::new(test_binary).output()?
71+
} else {
72+
let mut cmd = cmd.split_whitespace();
73+
Command::new(cmd.next().unwrap())
74+
.args(cmd.collect::<Vec<_>>())
75+
.arg(test_binary)
76+
.output()?
77+
};
6378

6479
if !output.status.success() {
6580
return Err(std::str::from_utf8(&output.stderr)?.into());

ctest-next/tests/basic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_entrypoint_hierarchy() {
3434
)
3535
.unwrap();
3636

37-
assert!(run_test(test_binary).is_ok());
37+
assert!(run_test(test_binary.to_str().unwrap()).is_ok());
3838
}
3939

4040
#[test]
@@ -50,7 +50,7 @@ fn test_entrypoint_simple() {
5050
let test_binary =
5151
compile_test(env::temp_dir().to_str().unwrap(), crate_path, "simple_out").unwrap();
5252

53-
assert!(run_test(test_binary).is_ok());
53+
assert!(run_test(test_binary.to_str().unwrap()).is_ok());
5454
}
5555

5656
#[test]
@@ -66,7 +66,7 @@ fn test_entrypoint_macro() {
6666
let test_binary =
6767
compile_test(env::temp_dir().to_str().unwrap(), crate_path, "macro_out").unwrap();
6868

69-
assert!(run_test(test_binary).is_ok());
69+
assert!(run_test(test_binary.to_str().unwrap()).is_ok());
7070
}
7171

7272
#[test]

0 commit comments

Comments
 (0)