From 3f20c721ceed518f3a94c2a29582b1d8789fb29f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 6 Jun 2024 18:59:43 +0200 Subject: [PATCH] Fix `mixing-formats` run-make test --- src/tools/run-make-support/src/lib.rs | 24 +++++++++++++++++++++- src/tools/run-make-support/src/rustc.rs | 5 +++-- tests/run-make/mixing-formats/rmake.rs | 27 +++++++++---------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index d74a0272a62..2b5d983faa3 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -227,7 +227,7 @@ pub fn set_host_rpath(cmd: &mut Command) { let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR"); cmd.env(&ld_lib_path_envvar, { let mut paths = vec![]; - paths.push(PathBuf::from(env_var("TMPDIR"))); + paths.push(env::current_dir().unwrap()); paths.push(PathBuf::from(env_var("HOST_RPATH_DIR"))); for p in env::split_paths(&env_var(&ld_lib_path_envvar)) { paths.push(p.to_path_buf()); @@ -315,6 +315,28 @@ pub fn assert_not_contains(haystack: &str, needle: &str) { } } +/// This function is designed for running commands in a temporary directory +/// that is cleared after the function ends. +/// +/// What this function does: +/// 1) Creates a temporary directory (`tmpdir`) +/// 2) Copies all files from the current directory to `tmpdir` +/// 3) Changes the current working directory to `tmpdir` +/// 4) Calls `callback` +/// 5) Switches working directory back to the original one +/// 6) Removes `tmpdir` +/// Switch current working directory to a temporary directory +pub fn run_in_tmpdir(callback: F) { + let original_dir = env::current_dir().unwrap(); + let tmpdir = original_dir.join("../temporary-directory"); + copy_dir_all(".", &tmpdir); + + env::set_current_dir(&tmpdir).unwrap(); + callback(); + env::set_current_dir(original_dir).unwrap(); + fs::remove_dir_all(tmpdir).unwrap(); +} + /// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct /// containing a `cmd: Command` field and a `output` function. The provided helpers are: /// diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs index e923c3cf4ad..bcdedb2722f 100644 --- a/src/tools/run-make-support/src/rustc.rs +++ b/src/tools/run-make-support/src/rustc.rs @@ -1,9 +1,10 @@ +use std::env; use std::ffi::{OsStr, OsString}; use std::io::Write; use std::path::Path; use std::process::{Command, Output, Stdio}; -use crate::{env_var, handle_failed_output, set_host_rpath, tmp_dir}; +use crate::{env_var, handle_failed_output, set_host_rpath}; /// Construct a new `rustc` invocation. pub fn rustc() -> Rustc { @@ -28,7 +29,7 @@ fn setup_common() -> Command { let rustc = env_var("RUSTC"); let mut cmd = Command::new(rustc); set_host_rpath(&mut cmd); - cmd.arg("--out-dir").arg(tmp_dir()).arg("-L").arg(tmp_dir()); + cmd.arg("-L").arg(env::current_dir().unwrap()); cmd } diff --git a/tests/run-make/mixing-formats/rmake.rs b/tests/run-make/mixing-formats/rmake.rs index 0d40b0325f7..9d072d9a6e4 100644 --- a/tests/run-make/mixing-formats/rmake.rs +++ b/tests/run-make/mixing-formats/rmake.rs @@ -12,31 +12,24 @@ //@ ignore-cross-compile -use run_make_support::{rustc, tmp_dir}; +use run_make_support::{rustc, tmp_dir, run_in_tmpdir}; use std::fs; -fn test_with_teardown(rustc_calls: impl Fn()) { - rustc_calls(); - //FIXME(Oneirical): This should be replaced with the run-make-support fs wrappers. - fs::remove_dir_all(tmp_dir()).unwrap(); - fs::create_dir(tmp_dir()).unwrap(); -} - fn main() { - test_with_teardown(|| { + run_in_tmpdir(|| { // Building just baz rustc().crate_type("rlib").input("foo.rs").run(); rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("dylib,rlib").input("baz.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("bin").input("baz.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("rlib").input("bar1.rs").run(); rustc().crate_type("dylib,rlib").input("baz.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("bin").input("baz.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { // Building baz2 rustc().crate_type("rlib").input("foo.rs").run(); rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run(); @@ -44,42 +37,42 @@ fn main() { rustc().crate_type("dylib").input("baz2.rs").run_fail_assert_exit_code(1); rustc().crate_type("bin").input("baz2.rs").run_fail_assert_exit_code(1); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("rlib").input("foo.rs").run(); rustc().crate_type("rlib").input("bar1.rs").run(); rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("dylib,rlib").input("baz2.rs").run(); rustc().crate_type("bin").input("baz2.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("rlib").input("foo.rs").run(); rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("rlib").input("bar2.rs").run(); rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("bin").input("baz2.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("rlib").input("foo.rs").run(); rustc().crate_type("rlib").input("bar1.rs").run(); rustc().crate_type("rlib").input("bar2.rs").run(); rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("bin").input("baz2.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("rlib").input("bar1.rs").run(); rustc().crate_type("rlib").input("bar2.rs").run(); rustc().crate_type("dylib,rlib").input("baz2.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("bin").input("baz2.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("dylib").input("bar1.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("rlib").input("bar2.rs").run(); rustc().crate_type("dylib,rlib").input("baz2.rs").run(); rustc().crate_type("bin").input("baz2.rs").run(); }); - test_with_teardown(|| { + run_in_tmpdir(|| { rustc().crate_type("dylib").input("foo.rs").arg("-Cprefer-dynamic").run(); rustc().crate_type("rlib").input("bar1.rs").run(); rustc().crate_type("dylib").input("bar2.rs").arg("-Cprefer-dynamic").run();