From a2d2f87bf9e00d4038ceb8bc337a2168aab12a89 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sun, 7 Apr 2024 10:41:33 +0200 Subject: [PATCH] Build aux-files just-in-time --- src/aux_builds.rs | 3 +- src/custom_flags.rs | 3 +- src/custom_flags/run.rs | 4 +- src/custom_flags/rustfix.rs | 6 ++- src/lib.rs | 5 +- src/per_test_config.rs | 25 ++++++---- src/status_emitter.rs | 54 ++++++++++++++++++++++ src/tests.rs | 1 + tests/integrations/basic-fail/Cargo.stdout | 2 +- 9 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/aux_builds.rs b/src/aux_builds.rs index 84e41d99..daf277c2 100644 --- a/src/aux_builds.rs +++ b/src/aux_builds.rs @@ -46,11 +46,12 @@ impl Build for AuxBuilder { revision: "", comments: &comments, path: &self.aux_file, + aux_dir: self.aux_file.parent().unwrap(), }; config.patch_out_dir(); - let mut aux_cmd = config.build_command()?; + let mut aux_cmd = config.build_command(build_manager)?; let mut extra_args = config.build_aux_files(self.aux_file.parent().unwrap(), build_manager)?; diff --git a/src/custom_flags.rs b/src/custom_flags.rs index 18eb3ac5..615d70c4 100644 --- a/src/custom_flags.rs +++ b/src/custom_flags.rs @@ -5,7 +5,7 @@ use std::{ process::{Command, Output}, }; -use crate::{per_test_config::TestConfig, Config, Errored}; +use crate::{build_manager::BuildManager, per_test_config::TestConfig, Config, Errored}; pub mod run; pub mod rustfix; @@ -30,6 +30,7 @@ pub trait Flag: Send + Sync + UnwindSafe + RefUnwindSafe + std::fmt::Debug { _config: &TestConfig<'_>, cmd: Command, _output: &Output, + _build_manager: &BuildManager<'_>, ) -> Result, Errored> { Ok(Some(cmd)) } diff --git a/src/custom_flags/run.rs b/src/custom_flags/run.rs index e7e273c8..6f6f2018 100644 --- a/src/custom_flags/run.rs +++ b/src/custom_flags/run.rs @@ -3,7 +3,7 @@ use bstr::ByteSlice; use std::process::{Command, Output}; -use crate::{per_test_config::TestConfig, Error, Errored}; +use crate::{build_manager::BuildManager, per_test_config::TestConfig, Error, Errored}; use super::Flag; @@ -22,6 +22,7 @@ impl Flag for Run { config: &TestConfig<'_>, cmd: Command, _output: &Output, + _build_manager: &BuildManager<'_>, ) -> Result, Errored> { let mut cmd = cmd; let exit_code = self.exit_code; @@ -31,6 +32,7 @@ impl Flag for Run { revision: &revision, comments: config.comments, path: config.path, + aux_dir: config.aux_dir, }; cmd.arg("--print").arg("file-names"); let output = cmd.output().unwrap(); diff --git a/src/custom_flags/rustfix.rs b/src/custom_flags/rustfix.rs index fb471fe1..840d5020 100644 --- a/src/custom_flags/rustfix.rs +++ b/src/custom_flags/rustfix.rs @@ -8,6 +8,7 @@ use std::{ use spanned::Span; use crate::{ + build_manager::BuildManager, parser::OptWithLine, per_test_config::{Comments, Revisioned, TestConfig}, rustc_stderr, Error, Errored, Mode, @@ -41,6 +42,7 @@ impl Flag for RustfixMode { config: &TestConfig<'_>, cmd: Command, output: &Output, + build_manager: &BuildManager<'_>, ) -> Result, Errored> { let global_rustfix = match *config.mode()? { Mode::Pass | Mode::Panic => RustfixMode::Disabled, @@ -132,6 +134,7 @@ impl Flag for RustfixMode { revision: config.revision, comments: &rustfix_comments, path: config.path, + aux_dir: config.aux_dir, }; let run = fixed_code.is_some(); @@ -157,6 +160,7 @@ impl Flag for RustfixMode { revision: config.revision, comments: &rustfix_comments, path: &rustfix_path, + aux_dir: config.aux_dir, }; if !errors.is_empty() { return Err(Errored { @@ -171,7 +175,7 @@ impl Flag for RustfixMode { return Ok(Some(cmd)); } - let mut cmd = config.build_command()?; + let mut cmd = config.build_command(build_manager)?; cmd.arg("--crate-name").arg(crate_name); let output = cmd.output().unwrap(); if output.status.success() { diff --git a/src/lib.rs b/src/lib.rs index 39d58313..fafe86f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,9 +139,11 @@ pub fn test_command(mut config: Config, path: &Path) -> Result { config, revision: "", comments: &comments, + aux_dir: &path.parent().unwrap().join("auxiliary"), path, }; - let mut result = config.build_command().unwrap(); + let build_manager = BuildManager::new(&(), config.config.clone()); + let mut result = config.build_command(&build_manager).unwrap(); result.args(extra_args); Ok(result) @@ -349,6 +351,7 @@ fn parse_and_test_file( revision, comments: &comments, path: status.path(), + aux_dir: &status.path().parent().unwrap().join("auxiliary"), }; let result = test_config.run_test(build_manager); diff --git a/src/per_test_config.rs b/src/per_test_config.rs index 3bcecfba..03fe4306 100644 --- a/src/per_test_config.rs +++ b/src/per_test_config.rs @@ -30,6 +30,8 @@ pub struct TestConfig<'a> { pub(crate) comments: &'a Comments, /// The path to the current file pub path: &'a Path, + /// The path to the folder where to look for aux files + pub aux_dir: &'a Path, } impl TestConfig<'_> { @@ -83,14 +85,20 @@ impl TestConfig<'_> { } } - pub(crate) fn build_command(&self) -> Result { + pub(crate) fn build_command( + &self, + build_manager: &BuildManager<'_>, + ) -> Result { let TestConfig { config, revision, comments, path, + aux_dir, } = self; let mut cmd = config.program.build(&config.out_dir); + let extra_args = self.build_aux_files(aux_dir, build_manager)?; + cmd.args(extra_args); cmd.arg(path); if !revision.is_empty() { cmd.arg(format!("--cfg={revision}")); @@ -393,16 +401,9 @@ impl TestConfig<'_> { } pub(crate) fn run_test(mut self, build_manager: &BuildManager<'_>) -> TestResult { - let extra_args = self.build_aux_files( - &self.path.parent().unwrap().join("auxiliary"), - build_manager, - )?; - self.patch_out_dir(); - self.config.program.args.extend(extra_args); - - let mut cmd = self.build_command()?; + let mut cmd = self.build_command(build_manager)?; let stdin = self.path.with_extension(self.extension("stdin")); if stdin.exists() { cmd.stdin(std::fs::File::open(stdin).unwrap()); @@ -414,7 +415,11 @@ impl TestConfig<'_> { for rev in self.comments() { for custom in rev.custom.values() { - if let Some(c) = custom.content.post_test_action(&self, cmd, &output)? { + if let Some(c) = + custom + .content + .post_test_action(&self, cmd, &output, build_manager)? + { cmd = c; } else { return Ok(TestOk::Ok); diff --git a/src/status_emitter.rs b/src/status_emitter.rs index 10bbf0ed..3bbafbcc 100644 --- a/src/status_emitter.rs +++ b/src/status_emitter.rs @@ -73,8 +73,62 @@ pub trait Summary { fn test_failure(&mut self, _status: &dyn TestStatus, _errors: &Errors) {} } +/// Report no summary impl Summary for () {} +/// Emit nothing +impl StatusEmitter for () { + fn register_test(&self, path: PathBuf) -> Box { + Box::new(SilentStatus { + path, + revision: String::new(), + }) + } + + fn finalize( + &self, + _failed: usize, + _succeeded: usize, + _ignored: usize, + _filtered: usize, + ) -> Box { + Box::new(()) + } +} + +struct SilentStatus { + revision: String, + path: PathBuf, +} + +impl TestStatus for SilentStatus { + fn for_revision(&self, revision: &str) -> Box { + Box::new(SilentStatus { + revision: revision.into(), + path: self.path.clone(), + }) + } + + fn failed_test<'a>( + &'a self, + _cmd: &'a Command, + _stderr: &'a [u8], + _stdout: &'a [u8], + ) -> Box { + Box::new(()) + } + + fn update_status(&self, _msg: String) {} + + fn path(&self) -> &Path { + &self.path + } + + fn revision(&self) -> &str { + &self.revision + } +} + /// A human readable output emitter. #[derive(Clone)] pub struct Text { diff --git a/src/tests.rs b/src/tests.rs index 52eb804a..954f4562 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -25,6 +25,7 @@ macro_rules! config { path, comments: &comments, revision: "", + aux_dir: Path::new("unused_doesnt_exist"), }; }; } diff --git a/tests/integrations/basic-fail/Cargo.stdout b/tests/integrations/basic-fail/Cargo.stdout index c6c3b6bb..c32b76f4 100644 --- a/tests/integrations/basic-fail/Cargo.stdout +++ b/tests/integrations/basic-fail/Cargo.stdout @@ -519,7 +519,7 @@ full stdout: FAILED TEST: tests/actual_tests_bless/aux_proc_macro_no_main.rs -command: "rustc" "--error-format=json" "--crate-type=lib" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail.rlib" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "--extern" "the_proc_macro=$DIR/tests/integrations/basic-fail/../../../target/$TMP/tests/actual_tests_bless/auxiliary/libthe_proc_macro.so" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/tests/actual_tests_bless/auxiliary" "--out-dir" "$TMP "tests/actual_tests_bless/aux_proc_macro_no_main.rs" "--edition" "2021" +command: "rustc" "--error-format=json" "--crate-type=lib" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail.rlib" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "--out-dir" "$TMP "--extern" "the_proc_macro=$DIR/tests/integrations/basic-fail/../../../target/$TMP/tests/actual_tests_bless/auxiliary/libthe_proc_macro.so" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/tests/actual_tests_bless/auxiliary" "tests/actual_tests_bless/aux_proc_macro_no_main.rs" "--edition" "2021" error: there were 1 unmatched diagnostics --> tests/actual_tests_bless/aux_proc_macro_no_main.rs:7:8