Skip to content

Add ./miri run-dep for running a file with test dependencies available #2890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ libloading = "0.7"

[dev-dependencies]
colored = "2"
ui_test = "0.9"
ui_test = "0.10"
rustc_version = "0.4"
# Features chosen to match those required by env_logger, to avoid rebuilds
regex = { version = "1.5.5", default-features = false, features = ["perf", "std"] }
Expand Down
10 changes: 8 additions & 2 deletions miri
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ test|bless)
# Only in root project as `cargo-miri` has no tests.
$CARGO test $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml "$@"
;;
run)
run|run-dep)
# Scan for "--target" to overwrite the "MIRI_TEST_TARGET" env var so
# that we set the MIRI_SYSROOT up the right way.
FOUND_TARGET_OPT=0
Expand All @@ -323,11 +323,17 @@ run)
# Make sure Miri actually uses this target.
MIRIFLAGS="$MIRIFLAGS --target $MIRI_TEST_TARGET"
fi

# First build and get a sysroot.
$CARGO build $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml
find_sysroot
# Then run the actual command.
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"

if [ "$COMMAND" = "run-dep" ]; then
exec $CARGO test --test compiletest $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- --miri-run-dep-mode $MIRIFLAGS "$@"
else
exec $CARGO run $CARGO_EXTRA_FLAGS --manifest-path "$MIRIDIR"/Cargo.toml -- $MIRIFLAGS "$@"
fi
;;
fmt)
find "$MIRIDIR" -not \( -name target -prune \) -name '*.rs' \
Expand Down
63 changes: 47 additions & 16 deletions tests/compiletest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use colored::*;
use regex::bytes::Regex;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::{env, process::Command};
use ui_test::status_emitter::StatusEmitter;
Expand Down Expand Up @@ -45,7 +46,7 @@ fn build_so_for_c_ffi_tests() -> PathBuf {
so_file_path
}

fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> Result<()> {
fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> Config {
// Miri is rustc-like, so we create a default builder for rustc and modify it
let mut program = CommandBuilder::rustc();
program.program = miri_path();
Expand Down Expand Up @@ -103,6 +104,26 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
..Config::default()
};

let use_std = env::var_os("MIRI_NO_STD").is_none();

if with_dependencies && use_std {
config.dependencies_crate_manifest_path =
Some(Path::new("test_dependencies").join("Cargo.toml"));
config.dependency_builder.args = vec![
"run".into(),
"--manifest-path".into(),
"cargo-miri/Cargo.toml".into(),
"--".into(),
"miri".into(),
"run".into(), // There is no `cargo miri build` so we just use `cargo miri run`.
];
}
config
}

fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> Result<()> {
let mut config = test_config(target, path, mode, with_dependencies);

// Handle command-line arguments.
let mut after_dashdash = false;
config.path_filter.extend(std::env::args().skip(1).filter(|arg| {
Expand All @@ -126,21 +147,6 @@ fn run_tests(mode: Mode, path: &str, target: &str, with_dependencies: bool) -> R
}
}));

let use_std = env::var_os("MIRI_NO_STD").is_none();

if with_dependencies && use_std {
config.dependencies_crate_manifest_path =
Some(Path::new("test_dependencies").join("Cargo.toml"));
config.dependency_builder.args = vec![
"run".into(),
"--manifest-path".into(),
"cargo-miri/Cargo.toml".into(),
"--".into(),
"miri".into(),
"run".into(), // There is no `cargo miri build` so we just use `cargo miri run`.
];
}

eprintln!(" Compiler: {}", config.program.display());
ui_test::run_tests_generic(
config,
Expand Down Expand Up @@ -226,8 +232,18 @@ fn get_target() -> String {

fn main() -> Result<()> {
ui_test::color_eyre::install()?;

let target = get_target();

let mut args = std::env::args_os();

// Skip the program name and check whether this is a `./miri run-dep` invocation
if let Some(first) = args.nth(1) {
if first == "--miri-run-dep-mode" {
return run_dep_mode(target, args);
}
}

// Add a test env var to do environment communication tests.
env::set_var("MIRI_ENV_VAR_TEST", "0");
// Let the tests know where to store temp files (they might run for a different target, which can make this hard to find).
Expand All @@ -250,6 +266,21 @@ fn main() -> Result<()> {
Ok(())
}

fn run_dep_mode(target: String, mut args: impl Iterator<Item = OsString>) -> Result<()> {
let path = args.next().expect("./miri run-dep must be followed by a file name");
let mut config = test_config(&target, "", Mode::Yolo, /* with dependencies */ true);
config.program.args.remove(0); // remove the `--error-format=json` argument
config.program.args.push("--color".into());
config.program.args.push("always".into());
let mut cmd = ui_test::test_command(config, Path::new(&path))?;
// Separate the arguments to the `cargo miri` invocation from
// the arguments to the interpreted prog
cmd.arg("--");
Copy link
Member

Choose a reason for hiding this comment

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

Who is this -- for? Separating cargo run arguments from arguments passed to the program?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, i'll add a comment

cmd.args(args);
println!("{cmd:?}");
if cmd.spawn()?.wait()?.success() { Ok(()) } else { std::process::exit(1) }
}

/// This is a custom renderer for `ui_test` output that does not emit github actions
/// `group`s, while still producing regular github actions messages on test failures.
struct TextAndGha;
Expand Down