Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/run-make/rustdoc-test-builder/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{self, Command};
use std::{env, fs};

fn main() {
let args: Vec<OsString> = env::args_os().collect();
let log_path = env::var_os("BUILDER_LOG").map(PathBuf::from).expect("BUILDER_LOG must be set");
let real_rustc = env::var_os("REAL_RUSTC").expect("REAL_RUSTC must be set");

let log_contents =
args.iter().skip(1).map(|arg| arg.to_string_lossy()).collect::<Vec<_>>().join("\n");
fs::write(&log_path, log_contents).expect("failed to write builder log");

let status = Command::new(real_rustc)
.args(args.iter().skip(1))
.status()
.expect("failed to invoke real rustc");

if !status.success() {
process::exit(status.code().unwrap_or(1));
}
}
3 changes: 3 additions & 0 deletions tests/run-make/rustdoc-test-builder/doctest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! ```rust
//! assert_eq!(2 + 2, 4);
//! ```
43 changes: 40 additions & 3 deletions tests/run-make/rustdoc-test-builder/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// This test ensures that if the rustdoc test binary is not executable, it will
// gracefully fail and not panic.
// This test validates the `--test-builder` rustdoc option.
// It ensures that:
// 1. When the test-builder path points to a non-executable file, rustdoc gracefully fails
// 2. When the test-builder path points to a valid executable, it receives rustc arguments

//@ needs-target-std

use run_make_support::{path, rfs, rustdoc};
use run_make_support::{bare_rustc, path, rfs, rustc_path, rustdoc};

fn main() {
// Test 1: Verify that a non-executable test-builder fails gracefully
let absolute_path = path("foo.rs").canonicalize().expect("failed to get absolute path");
let output = rustdoc()
.input("foo.rs")
Expand All @@ -19,4 +22,38 @@ fn main() {
output.assert_stdout_contains("Failed to spawn ");
// ... and that we didn't panic.
output.assert_not_ice();

// Some targets (for example wasm) cannot execute doctests directly even with a runner,
// so only exercise the success path when the target can run on the host.
let target = std::env::var("TARGET").expect("TARGET must be set");
if target.contains("wasm") {
return;
}
Comment on lines 26 to 30
Copy link
Member

@jieyouxu jieyouxu Nov 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: sorry I didn't look at this, but this should instead use //@ ignore-wasm or //@ ignore-cross-compile (if this is host-only), otherwise this will show as test pass on WASM but in reality nothing is checked. Since this is in a rollup, it can be done as a follow-up.

(Also, there is a run_make_support::target() helper for what you're doing here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jieyouxu. I’d actually prefer to keep the test running on wasm: the failing-path check (non-executable builder should error without panicking) still runs there, so the test isn’t a no-op even after we skip the success branch. I’ll switch the guard to use run_make_support::target() for clarity in a followup PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added that to this PR since there was a link error in the rollup tests (fixed too).


// Test 2: Verify that a valid test-builder is invoked with correct arguments
// Build a custom test-builder that logs its arguments and forwards to rustc.
// Use `bare_rustc` so we compile for the host architecture even in cross builds.
let builder_bin = path("builder-bin");
bare_rustc().input("builder.rs").output(&builder_bin).run();

let log_path = path("builder.log");
let _ = std::fs::remove_file(&log_path);

// Run rustdoc with our custom test-builder
rustdoc()
.input("doctest.rs")
.arg("--test")
.arg("-Zunstable-options")
.arg("--test-builder")
.arg(&builder_bin)
.env("REAL_RUSTC", rustc_path())
.env("BUILDER_LOG", &log_path)
.run();

// Verify the custom builder was invoked with rustc-style arguments
let log_contents = rfs::read_to_string(&log_path);
assert!(
log_contents.contains("--crate-type"),
"expected builder to receive rustc arguments, got:\n{log_contents}"
);
}
Loading