Skip to content

Commit 641927f

Browse files
committed
Add ./miri run-dep for running a file with test dependencies available
1 parent 3f7d620 commit 641927f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/tools/miri/miri

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ test|bless)
306306
# Only in root project as `cargo-miri` has no tests.
307307
$CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
308308
;;
309-
run)
309+
run|run-dep)
310310
# Scan for "--target" to overwrite the "MIRI_TEST_TARGET" env var so
311311
# that we set the MIRI_SYSROOT up the right way.
312312
FOUND_TARGET_OPT=0
@@ -323,11 +323,17 @@ run)
323323
# Make sure Miri actually uses this target.
324324
MIRIFLAGS="$MIRIFLAGS --target $MIRI_TEST_TARGET"
325325
fi
326+
326327
# First build and get a sysroot.
327328
$CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
328329
find_sysroot
329330
# Then run the actual command.
330-
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
331+
332+
if [ "$COMMAND" = "run-dep" ]; then
333+
exec $CARGO test --test compiletest -- miri-run-dep-mode $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
334+
else
335+
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
336+
fi
331337
;;
332338
fmt)
333339
find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \

src/tools/miri/tests/compiletest.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use colored::*;
22
use regex::bytes::Regex;
3+
use std::io::Write;
34
use std::path::{Path, PathBuf};
45
use std::{env, process::Command};
56
use ui_test::status_emitter::StatusEmitter;
@@ -45,7 +46,13 @@ fn build_so_for_c_ffi_tests() -> PathBuf {
4546
so_file_path
4647
}
4748

48-
fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> Result<()> {
49+
fn run_test_config(
50+
args: impl Iterator<Item = String>,
51+
target: &str,
52+
path: &str,
53+
mode: Mode,
54+
with_dependencies: bool,
55+
) -> Config {
4956
// Miri is rustc-like, so we create a default builder for rustc and modify it
5057
let mut program = CommandBuilder::rustc();
5158
program.program = miri_path();
@@ -105,7 +112,7 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
105112

106113
// Handle command-line arguments.
107114
let mut after_dashdash = false;
108-
config.path_filter.extend(std::env::args().skip(1).filter(|arg| {
115+
config.path_filter.extend(args.filter(|arg| {
109116
if after_dashdash {
110117
// Just propagate everything.
111118
return true;
@@ -140,6 +147,11 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
140147
"run".into(), // There is no `cargo miri build` so we just use `cargo miri run`.
141148
];
142149
}
150+
config
151+
}
152+
153+
fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> Result<()> {
154+
let config = run_test_config(std::env::args().skip(1), target, path, mode, with_dependencies);
143155

144156
eprintln!(" Compiler: {}", config.program.display());
145157
ui_test::run_tests_generic(
@@ -226,8 +238,15 @@ fn get_target() -> String {
226238

227239
fn main() -> Result<()> {
228240
ui_test::color_eyre::install()?;
241+
229242
let target = get_target();
230243

244+
if let Some(first) = std::env::args().nth(1) {
245+
if first == "miri-run-dep-mode" {
246+
return run_dep_mode(target);
247+
}
248+
}
249+
231250
// Add a test env var to do environment communication tests.
232251
env::set_var("MIRI_ENV_VAR_TEST", "0");
233252
// Let the tests know where to store temp files (they might run for a different target, which can make this hard to find).
@@ -250,6 +269,21 @@ fn main() -> Result<()> {
250269
Ok(())
251270
}
252271

272+
fn run_dep_mode(target: String) -> Result<()> {
273+
let files = std::env::args().skip_while(|arg| arg != "--").skip(1);
274+
for path in files {
275+
let mut config = run_test_config(std::iter::empty(), &target, &path, Mode::Yolo, true);
276+
config.program.args.remove(0); // remove the `--error-format=json` argument
277+
config.program.args.push("--color".into());
278+
config.program.args.push("always".into());
279+
let output = ui_test::run_file(config, Path::new(&path))?;
280+
std::io::stderr().write_all(&output.stderr)?;
281+
std::io::stdout().write_all(&output.stdout)?;
282+
std::process::exit(output.status.code().unwrap());
283+
}
284+
Ok(())
285+
}
286+
253287
/// This is a custom renderer for `ui_test` output that does not emit github actions
254288
/// `group`s, while still producing regular github actions messages on test failures.
255289
struct TextAndGha;

0 commit comments

Comments
 (0)