Auto merge of #131095 - GuillaumeGomez:switch-to-env-variables, r= · rust-lang/rust@ff81870 (original) (raw)

`@@ -112,8 +112,7 @@ mod __doctest_mod {{

`

112

112

` use std::path::PathBuf;

`

113

113

``

114

114

` pub static BINARY_PATH: OnceLock = 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";

`

117

116

``

118

117

` #[allow(unused)]

`

119

118

` pub fn doctest_path() -> Option<&'static PathBuf> {{

`

`@@ -123,8 +122,8 @@ mod __doctest_mod {{

`

123

122

` #[allow(unused)]

`

124

123

` pub fn doctest_runner(bin: &std::path::Path, test_nb: usize) -> Result<(), String> {{

`

125

124

` 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<_>>())

`

128

127

` .output()

`

129

128

` .expect("failed to run command");

`

130

129

` if !out.status.success() {{

`

`@@ -138,36 +137,27 @@ mod __doctest_mod {{

`

138

137

`#[rustc_main]

`

139

138

`fn main() -> std::process::ExitCode {{

`

140

139

`const TESTS: [test::TestDescAndFn; {nb_tests}] = [{ids}];

`

141

``

`-

let bin_marker = std::ffi::OsStr::new(__doctest_mod::BIN_OPTION);

`

142

140

`let test_marker = std::ffi::OsStr::new(__doctest_mod::RUN_OPTION);

`

143

141

`let test_args = &[{test_args}];

`

``

142

`+

const ENV_BIN: &'static str = "RUSTDOC_DOCTEST_BIN_PATH";

`

144

143

``

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::().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::() {{

`

``

150

`+

if let Some(test) = TESTS.get(nb_test) {{

`

``

151

`+

if let test::StaticTestFn(f) = test.testfn {{

`

``

152

`+

return std::process::Termination::report(f());

`

164

153

` }}

`

165

154

` }}

`

166

``

`` -

panic!("Unexpected value after {{}}", __doctest_mod::RUN_OPTION);

``

167

155

` }}

`

``

156

`` +

panic!("Unexpected value for {{}}", __doctest_mod::RUN_OPTION);

``

168

157

`}}

`

169

158

``

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");

`

171

161

`std::process::Termination::report(test::test_main(test_args, Vec::from(TESTS), None))

`

172

162

`}}",

`

173

163

` nb_tests = self.nb_tests,

`