From 2eac6f5c0c0942f907d6e71813920a3c32af46ca Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 18 Nov 2023 17:23:29 -0800 Subject: [PATCH] Fix clippy-wrapper test race condition. --- crates/cargo-test-support/src/lib.rs | 23 ------------------ crates/cargo-test-support/src/tools.rs | 32 ++++++++++++++++++++++++++ tests/testsuite/check.rs | 4 ++-- tests/testsuite/fix.rs | 4 ++-- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index d5d62fc25e4..4e3ef6118a9 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -523,29 +523,6 @@ pub fn cargo_exe() -> PathBuf { snapbox::cmd::cargo_bin("cargo") } -/// A wrapper around `rustc` instead of calling `clippy`. -pub fn wrapped_clippy_driver() -> PathBuf { - let clippy_driver = project() - .at(paths::global_root().join("clippy-driver")) - .file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1")) - .file( - "src/main.rs", - r#" - fn main() { - let mut args = std::env::args_os(); - let _me = args.next().unwrap(); - let rustc = args.next().unwrap(); - let status = std::process::Command::new(rustc).args(args).status().unwrap(); - std::process::exit(status.code().unwrap_or(1)); - } - "#, - ) - .build(); - clippy_driver.cargo("build").run(); - - clippy_driver.bin("clippy-driver") -} - /// This is the raw output from the process. /// /// This is similar to `std::process::Output`, however the `status` is diff --git a/crates/cargo-test-support/src/tools.rs b/crates/cargo-test-support/src/tools.rs index 2ce2849ae32..b6fa4092f88 100644 --- a/crates/cargo-test-support/src/tools.rs +++ b/crates/cargo-test-support/src/tools.rs @@ -7,6 +7,7 @@ use std::sync::OnceLock; static ECHO_WRAPPER: OnceLock>> = OnceLock::new(); static ECHO: OnceLock>> = OnceLock::new(); +static CLIPPY_DRIVER: OnceLock>> = OnceLock::new(); /// Returns the path to an executable that works as a wrapper around rustc. /// @@ -107,3 +108,34 @@ pub fn echo_subcommand() -> Project { p.cargo("build").run(); p } + +/// A wrapper around `rustc` instead of calling `clippy`. +pub fn wrapped_clippy_driver() -> PathBuf { + let mut lock = CLIPPY_DRIVER + .get_or_init(|| Default::default()) + .lock() + .unwrap(); + if let Some(path) = &*lock { + return path.clone(); + } + let clippy_driver = project() + .at(paths::global_root().join("clippy-driver")) + .file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1")) + .file( + "src/main.rs", + r#" + fn main() { + let mut args = std::env::args_os(); + let _me = args.next().unwrap(); + let rustc = args.next().unwrap(); + let status = std::process::Command::new(rustc).args(args).status().unwrap(); + std::process::exit(status.code().unwrap_or(1)); + } + "#, + ) + .build(); + clippy_driver.cargo("build").run(); + let path = clippy_driver.bin("clippy-driver"); + *lock = Some(path.clone()); + path +} diff --git a/tests/testsuite/check.rs b/tests/testsuite/check.rs index 03611ae67e7..db153fc3cc5 100644 --- a/tests/testsuite/check.rs +++ b/tests/testsuite/check.rs @@ -6,8 +6,8 @@ use crate::messages::raw_rustc_output; use cargo_test_support::install::exe; use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; +use cargo_test_support::tools; use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project}; -use cargo_test_support::{tools, wrapped_clippy_driver}; #[cargo_test] fn check_success() { @@ -1432,7 +1432,7 @@ fn check_fixable_warning_for_clippy() { foo.cargo("check") // We can't use `clippy` so we use a `rustc` workspace wrapper instead - .env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver()) + .env("RUSTC_WORKSPACE_WRAPPER", tools::wrapped_clippy_driver()) .with_stderr_contains("[..] (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)") .run(); } diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 33de721cd5b..4e1f5428974 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -5,8 +5,8 @@ use cargo_test_support::compare::assert_match_exact; use cargo_test_support::git::{self, init}; use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::{Dependency, Package}; +use cargo_test_support::tools; use cargo_test_support::{basic_manifest, is_nightly, project, Project}; -use cargo_test_support::{tools, wrapped_clippy_driver}; #[cargo_test] fn do_not_fix_broken_builds() { @@ -193,7 +193,7 @@ fn broken_clippy_fixes_backed_out() { .env("__CARGO_FIX_YOLO", "1") .env("RUSTC", p.root().join("foo/target/debug/foo")) // We can't use `clippy` so we use a `rustc` workspace wrapper instead - .env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver()) + .env("RUSTC_WORKSPACE_WRAPPER", tools::wrapped_clippy_driver()) .with_stderr_contains( "warning: failed to automatically apply fixes suggested by rustc \ to crate `bar`\n\