Skip to content

Commit 25c5451

Browse files
committed
Auto merge of #2890 - oli-obk:ui_test, r=RalfJung
Add `./miri run-dep` for running a file with test dependencies available fixes #2443
2 parents cd5f8b4 + 2f18734 commit 25c5451

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

src/tools/miri/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,9 @@ dependencies = [
820820

821821
[[package]]
822822
name = "ui_test"
823-
version = "0.9.0"
823+
version = "0.10.0"
824824
source = "registry+https://github.com/rust-lang/crates.io-index"
825-
checksum = "95033b0e41b8018013d99a6f1486c1ae5bd080378ced60c5f797e93842423b33"
825+
checksum = "191a442639ea102fa62671026047e51d574bfda44b7fdf32151d7314624c1cd2"
826826
dependencies = [
827827
"bstr",
828828
"cargo-platform",

src/tools/miri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ libloading = "0.7"
3939

4040
[dev-dependencies]
4141
colored = "2"
42-
ui_test = "0.9"
42+
ui_test = "0.10"
4343
rustc_version = "0.4"
4444
# Features chosen to match those required by env_logger, to avoid rebuilds
4545
regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] }

src/tools/miri/miri

+8-2
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 $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --miri-run-dep-mode $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

+47-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use colored::*;
22
use regex::bytes::Regex;
3+
use std::ffi::OsString;
34
use std::path::{Path, PathBuf};
45
use std::{env, process::Command};
56
use ui_test::status_emitter::StatusEmitter;
@@ -45,7 +46,7 @@ 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 test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> Config {
4950
// Miri is rustc-like, so we create a default builder for rustc and modify it
5051
let mut program = CommandBuilder::rustc();
5152
program.program = miri_path();
@@ -103,6 +104,26 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
103104
..Config::default()
104105
};
105106

107+
let use_std = env::var_os("MIRI_NO_STD").is_none();
108+
109+
if with_dependencies && use_std {
110+
config.dependencies_crate_manifest_path =
111+
Some(Path::new("test_dependencies").join("Cargo.toml"));
112+
config.dependency_builder.args = vec![
113+
"run".into(),
114+
"--manifest-path".into(),
115+
"cargo-miri/Cargo.toml".into(),
116+
"--".into(),
117+
"miri".into(),
118+
"run".into(), // There is no `cargo miri build` so we just use `cargo miri run`.
119+
];
120+
}
121+
config
122+
}
123+
124+
fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> Result<()> {
125+
let mut config = test_config(target, path, mode, with_dependencies);
126+
106127
// Handle command-line arguments.
107128
let mut after_dashdash = false;
108129
config.path_filter.extend(std::env::args().skip(1).filter(|arg| {
@@ -126,21 +147,6 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
126147
}
127148
}));
128149

129-
let use_std = env::var_os("MIRI_NO_STD").is_none();
130-
131-
if with_dependencies && use_std {
132-
config.dependencies_crate_manifest_path =
133-
Some(Path::new("test_dependencies").join("Cargo.toml"));
134-
config.dependency_builder.args = vec![
135-
"run".into(),
136-
"--manifest-path".into(),
137-
"cargo-miri/Cargo.toml".into(),
138-
"--".into(),
139-
"miri".into(),
140-
"run".into(), // There is no `cargo miri build` so we just use `cargo miri run`.
141-
];
142-
}
143-
144150
eprintln!(" Compiler: {}", config.program.display());
145151
ui_test::run_tests_generic(
146152
config,
@@ -226,8 +232,18 @@ fn get_target() -> String {
226232

227233
fn main() -> Result<()> {
228234
ui_test::color_eyre::install()?;
235+
229236
let target = get_target();
230237

238+
let mut args = std::env::args_os();
239+
240+
// Skip the program name and check whether this is a `./miri run-dep` invocation
241+
if let Some(first) = args.nth(1) {
242+
if first == "--miri-run-dep-mode" {
243+
return run_dep_mode(target, args);
244+
}
245+
}
246+
231247
// Add a test env var to do environment communication tests.
232248
env::set_var("MIRI_ENV_VAR_TEST", "0");
233249
// 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 +266,21 @@ fn main() -> Result<()> {
250266
Ok(())
251267
}
252268

269+
fn run_dep_mode(target: String, mut args: impl Iterator<Item = OsString>) -> Result<()> {
270+
let path = args.next().expect("./miri run-dep must be followed by a file name");
271+
let mut config = test_config(&target, "", Mode::Yolo, /* with dependencies */ true);
272+
config.program.args.remove(0); // remove the `--error-format=json` argument
273+
config.program.args.push("--color".into());
274+
config.program.args.push("always".into());
275+
let mut cmd = ui_test::test_command(config, Path::new(&path))?;
276+
// Separate the arguments to the `cargo miri` invocation from
277+
// the arguments to the interpreted prog
278+
cmd.arg("--");
279+
cmd.args(args);
280+
println!("{cmd:?}");
281+
if cmd.spawn()?.wait()?.success() { Ok(()) } else { std::process::exit(1) }
282+
}
283+
253284
/// This is a custom renderer for `ui_test` output that does not emit github actions
254285
/// `group`s, while still producing regular github actions messages on test failures.
255286
struct TextAndGha;

0 commit comments

Comments
 (0)