Skip to content

Commit e904761

Browse files
Use environment variables instead of command line arguments for merged doctests
1 parent 612796c commit e904761

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

Diff for: src/librustdoc/doctest.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,7 @@ fn run_test(
644644
} else {
645645
cmd = Command::new(&output_file);
646646
if doctest.is_multiple_tests {
647-
cmd.arg("*doctest-bin-path");
648-
cmd.arg(&output_file);
647+
cmd.env("RUSTDOC_DOCTEST_BIN_PATH", &output_file);
649648
}
650649
}
651650
if let Some(run_directory) = &rustdoc_options.test_run_directory {

Diff for: src/librustdoc/doctest/runner.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ mod __doctest_mod {{
112112
use std::path::PathBuf;
113113
114114
pub static BINARY_PATH: OnceLock<PathBuf> = OnceLock::new();
115-
pub const RUN_OPTION: &str = \"*doctest-inner-test\";
116-
pub const BIN_OPTION: &str = \"*doctest-bin-path\";
115+
pub const RUN_OPTION: &str = \"RUSTDOC_DOCTEST_RUN_NB_TEST\";
117116
118117
#[allow(unused)]
119118
pub fn doctest_path() -> Option<&'static PathBuf> {{
@@ -123,8 +122,8 @@ mod __doctest_mod {{
123122
#[allow(unused)]
124123
pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> Result<(), String> {{
125124
let out = std::process::Command::new(bin)
126-
.arg(self::RUN_OPTION)
127-
.arg(test_nb.to_string())
125+
.env(self::RUN_OPTION, test_nb.to_string())
126+
.args(std::env::args().skip(1).collect::<Vec<_>>())
128127
.output()
129128
.expect(\"failed to run command\");
130129
if !out.status.success() {{
@@ -138,36 +137,27 @@ mod __doctest_mod {{
138137
#[rustc_main]
139138
fn main() -> std::process::ExitCode {{
140139
const TESTS: [test::TestDescAndFn; {nb_tests}] = [{ids}];
141-
let bin_marker = std::ffi::OsStr::new(__doctest_mod::BIN_OPTION);
142140
let test_marker = std::ffi::OsStr::new(__doctest_mod::RUN_OPTION);
143141
let test_args = &[{test_args}];
142+
const ENV_BIN: &'static str = \"RUSTDOC_DOCTEST_BIN_PATH\";
144143
145-
let mut args = std::env::args_os().skip(1);
146-
while let Some(arg) = args.next() {{
147-
if arg == bin_marker {{
148-
let Some(binary) = args.next() else {{
149-
panic!(\"missing argument after `{{}}`\", __doctest_mod::BIN_OPTION);
150-
}};
151-
if crate::__doctest_mod::BINARY_PATH.set(binary.into()).is_err() {{
152-
panic!(\"`{{}}` option was used more than once\", bin_marker.to_string_lossy());
153-
}}
154-
return std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None));
155-
}} else if arg == test_marker {{
156-
let Some(nb_test) = args.next() else {{
157-
panic!(\"missing argument after `{{}}`\", __doctest_mod::RUN_OPTION);
158-
}};
159-
if let Some(nb_test) = nb_test.to_str().and_then(|nb| nb.parse::<usize>().ok()) {{
160-
if let Some(test) = TESTS.get(nb_test) {{
161-
if let test::StaticTestFn(f) = test.testfn {{
162-
return std::process::Termination::report(f());
163-
}}
144+
if let Ok(binary) = std::env::var(ENV_BIN) {{
145+
let _ = crate::__doctest_mod::BINARY_PATH.set(binary.into());
146+
unsafe {{ std::env::remove_var(ENV_BIN); }}
147+
return std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None));
148+
}} else if let Ok(nb_test) = std::env::var(__doctest_mod::RUN_OPTION) {{
149+
if let Ok(nb_test) = nb_test.parse::<usize>() {{
150+
if let Some(test) = TESTS.get(nb_test) {{
151+
if let test::StaticTestFn(f) = test.testfn {{
152+
return std::process::Termination::report(f());
164153
}}
165154
}}
166-
panic!(\"Unexpected value after `{{}}`\", __doctest_mod::RUN_OPTION);
167155
}}
156+
panic!(\"Unexpected value for `{{}}`\", __doctest_mod::RUN_OPTION);
168157
}}
169158
170-
eprintln!(\"WARNING: No argument provided so doctests will be run in the same process\");
159+
eprintln!(\"WARNING: No rustdoc doctest environment variable provided so doctests will be run in \
160+
the same process\");
171161
std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None))
172162
}}",
173163
nb_tests = self.nb_tests,

0 commit comments

Comments
 (0)