Skip to content

Commit

Permalink
Fix crate_wide_allow premise tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Aug 9, 2022
1 parent 375736d commit cff7aea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions examples/general/crate_wide_allow/Cargo.lock

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

1 change: 1 addition & 0 deletions examples/general/crate_wide_allow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
45 changes: 26 additions & 19 deletions examples/general/crate_wide_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
Expand Down Expand Up @@ -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(),
);
}
Expand Down

0 comments on commit cff7aea

Please sign in to comment.