Skip to content

Commit 7eefd77

Browse files
Auto merge of #148608 - osamakader:doc-test-builder, r=<try>
Add test for --test-builder success path try-job: test-various
2 parents fb23dd3 + 1c1923f commit 7eefd77

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::ffi::OsString;
2+
use std::path::PathBuf;
3+
use std::process::{self, Command};
4+
use std::{env, fs};
5+
6+
fn main() {
7+
let args: Vec<OsString> = env::args_os().collect();
8+
let log_path = env::var_os("BUILDER_LOG").map(PathBuf::from).expect("BUILDER_LOG must be set");
9+
let real_rustc = env::var_os("REAL_RUSTC").expect("REAL_RUSTC must be set");
10+
11+
let log_contents =
12+
args.iter().skip(1).map(|arg| arg.to_string_lossy()).collect::<Vec<_>>().join("\n");
13+
fs::write(&log_path, log_contents).expect("failed to write builder log");
14+
15+
let status = Command::new(real_rustc)
16+
.args(args.iter().skip(1))
17+
.status()
18+
.expect("failed to invoke real rustc");
19+
20+
if !status.success() {
21+
process::exit(status.code().unwrap_or(1));
22+
}
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! ```rust
2+
//! assert_eq!(2 + 2, 4);
3+
//! ```

tests/run-make/rustdoc-test-builder/rmake.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
// This test ensures that if the rustdoc test binary is not executable, it will
2-
// gracefully fail and not panic.
1+
// This test validates the `--test-builder` rustdoc option.
2+
// It ensures that:
3+
// 1. When the test-builder path points to a non-executable file, rustdoc gracefully fails
4+
// 2. When the test-builder path points to a valid executable, it receives rustc arguments
35

46
//@ needs-target-std
57

6-
use run_make_support::{path, rfs, rustdoc};
8+
use run_make_support::{bare_rustc, path, rfs, rustc_path, rustdoc};
79

810
fn main() {
11+
// Test 1: Verify that a non-executable test-builder fails gracefully
912
let absolute_path = path("foo.rs").canonicalize().expect("failed to get absolute path");
1013
let output = rustdoc()
1114
.input("foo.rs")
@@ -19,4 +22,38 @@ fn main() {
1922
output.assert_stdout_contains("Failed to spawn ");
2023
// ... and that we didn't panic.
2124
output.assert_not_ice();
25+
26+
// Some targets (for example wasm) cannot execute doctests directly even with a runner,
27+
// so only exercise the success path when the target can run on the host.
28+
let target = std::env::var("TARGET").expect("TARGET must be set");
29+
if target.contains("wasm") {
30+
return;
31+
}
32+
33+
// Test 2: Verify that a valid test-builder is invoked with correct arguments
34+
// Build a custom test-builder that logs its arguments and forwards to rustc.
35+
// Use `bare_rustc` so we compile for the host architecture even in cross builds.
36+
let builder_bin = path("builder-bin");
37+
bare_rustc().input("builder.rs").output(&builder_bin).run();
38+
39+
let log_path = path("builder.log");
40+
let _ = std::fs::remove_file(&log_path);
41+
42+
// Run rustdoc with our custom test-builder
43+
rustdoc()
44+
.input("doctest.rs")
45+
.arg("--test")
46+
.arg("-Zunstable-options")
47+
.arg("--test-builder")
48+
.arg(&builder_bin)
49+
.env("REAL_RUSTC", rustc_path())
50+
.env("BUILDER_LOG", &log_path)
51+
.run();
52+
53+
// Verify the custom builder was invoked with rustc-style arguments
54+
let log_contents = rfs::read_to_string(&log_path);
55+
assert!(
56+
log_contents.contains("--crate-type"),
57+
"expected builder to receive rustc arguments, got:\n{log_contents}"
58+
);
2259
}

0 commit comments

Comments
 (0)