From cff7aea4c12bdd76cd6232bef2c87dddea3cad6f Mon Sep 17 00:00:00 2001 From: "Samuel E. Moelius III" Date: Sat, 6 Aug 2022 20:20:43 -0400 Subject: [PATCH] Fix `crate_wide_allow` premise tests --- examples/general/crate_wide_allow/Cargo.lock | 1 + examples/general/crate_wide_allow/Cargo.toml | 1 + examples/general/crate_wide_allow/src/lib.rs | 45 +++++++++++--------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/examples/general/crate_wide_allow/Cargo.lock b/examples/general/crate_wide_allow/Cargo.lock index 094d23423..47cba8db4 100644 --- a/examples/general/crate_wide_allow/Cargo.lock +++ b/examples/general/crate_wide_allow/Cargo.lock @@ -181,6 +181,7 @@ name = "crate_wide_allow" version = "2.0.9" dependencies = [ "assert_cmd", + "cargo_metadata", "clippy_utils", "dylint_internal", "dylint_linting", diff --git a/examples/general/crate_wide_allow/Cargo.toml b/examples/general/crate_wide_allow/Cargo.toml index 2b02a830f..5cdbe98de 100644 --- a/examples/general/crate_wide_allow/Cargo.toml +++ b/examples/general/crate_wide_allow/Cargo.toml @@ -21,6 +21,7 @@ dylint_linting = { path = "../../../utils/linting" } [dev-dependencies] assert_cmd = "2.0.4" +cargo_metadata = "0.15.0" lazy_static = "1.4.0" dylint_internal = { path = "../../../internal" } diff --git a/examples/general/crate_wide_allow/src/lib.rs b/examples/general/crate_wide_allow/src/lib.rs index bc4792a03..210ca37e7 100644 --- a/examples/general/crate_wide_allow/src/lib.rs +++ b/examples/general/crate_wide_allow/src/lib.rs @@ -71,9 +71,10 @@ impl EarlyLintPass for CrateWideAllow { #[cfg(test)] mod test { use assert_cmd::{assert::Assert, Command}; + use cargo_metadata::MetadataCommand; use dylint_internal::env; use lazy_static::lazy_static; - use std::{path::Path, sync::Mutex}; + use std::{env::consts, path::Path, sync::Mutex}; lazy_static! { static ref MUTEX: Mutex<()> = Mutex::new(()); @@ -108,32 +109,38 @@ mod test { // * Setting `RUSTFLAGS` forces `cargo check` to be re-run. Unfortunately, this also forces // `cargo-dylint` to be rebuilt, which causes problems on Windows, hence the need for the // mutex. + // smoelius: Invoking `cargo-dylint` directly by path, rather than through `cargo run`, avoids + // the rebuilding problem. But oddly enough, the tests are faster with the mutex than without. - fn test(rustflags: &str, check: impl Fn(Assert) -> Assert) { + fn test(rustflags: &str, assert: impl Fn(Assert) -> Assert) { let _lock = MUTEX.lock().unwrap(); - let manifest = Path::new(env!("CARGO_MANIFEST_DIR")) + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")) .join("..") .join("..") - .join("..") - .join("Cargo.toml"); + .join(".."); + + Command::new("cargo") + .current_dir(&manifest_dir) + .args(["build", "--bin", "cargo-dylint"]) + .assert() + .success(); + + let metadata = MetadataCommand::new() + .current_dir(manifest_dir) + .no_deps() + .exec() + .unwrap(); + let cargo_dylint = metadata + .target_directory + .join("debug") + .join(format!("cargo-dylint{}", consts::EXE_SUFFIX)); - check( - Command::new("cargo") + assert( + Command::new(cargo_dylint) .env_remove(env::DYLINT_LIBRARY_PATH) .env(env::RUSTFLAGS, rustflags) - .args(&[ - "run", - "--manifest-path", - &manifest.to_string_lossy(), - "--bin", - "cargo-dylint", - "--", - "dylint", - "clippy", - "--", - "--examples", - ]) + .args(&["dylint", "clippy", "--", "--examples"]) .assert(), ); }