diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2022-01-10 13:20:46 -0600 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2022-01-10 15:22:17 -0600 |
| commit | 51dbbf3c4c67446e3ae81e2488f48bfce8dac59f (patch) | |
| tree | 6694f112cc810bf4097f41c04abe64e3385f5bf1 | |
| parent | e66ecf6f0e137196e67daf0ba0982766a2c91175 (diff) | |
Refactor test utils
| -rw-r--r-- | tests/cargo/mod.rs | 4 | ||||
| -rw-r--r-- | tests/compile-test.rs | 6 | ||||
| -rw-r--r-- | tests/dogfood.rs | 27 | ||||
| -rw-r--r-- | tests/test_utils/mod.rs | 13 |
4 files changed, 27 insertions, 23 deletions
diff --git a/tests/cargo/mod.rs b/tests/cargo/mod.rs deleted file mode 100644 index 4dbe71e4b6a..00000000000 --- a/tests/cargo/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[must_use] -pub fn is_rustc_test_suite() -> bool { - option_env!("RUSTC_TEST_SUITE").is_some() -} diff --git a/tests/compile-test.rs b/tests/compile-test.rs index 762b67fc585..531890c863f 100644 --- a/tests/compile-test.rs +++ b/tests/compile-test.rs @@ -1,4 +1,5 @@ #![feature(test)] // compiletest_rs requires this attribute +#![feature(once_cell)] #![cfg_attr(feature = "deny-warnings", deny(warnings))] #![warn(rust_2018_idioms, unused_lifetimes)] @@ -11,8 +12,9 @@ use std::ffi::{OsStr, OsString}; use std::fs; use std::io; use std::path::{Path, PathBuf}; +use test_utils::IS_RUSTC_TEST_SUITE; -mod cargo; +mod test_utils; // whether to run internal tests or not const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal"); @@ -304,7 +306,7 @@ fn run_ui_cargo(config: &mut compiletest::Config) { Ok(result) } - if cargo::is_rustc_test_suite() { + if IS_RUSTC_TEST_SUITE { return; } diff --git a/tests/dogfood.rs b/tests/dogfood.rs index 17033f6e908..42e1628f642 100644 --- a/tests/dogfood.rs +++ b/tests/dogfood.rs @@ -7,28 +7,21 @@ #![cfg_attr(feature = "deny-warnings", deny(warnings))] #![warn(rust_2018_idioms, unused_lifetimes)] -use std::lazy::SyncLazy; use std::path::PathBuf; use std::process::Command; +use test_utils::{CARGO_CLIPPY_PATH, IS_RUSTC_TEST_SUITE}; -mod cargo; - -static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| { - let mut path = std::env::current_exe().unwrap(); - assert!(path.pop()); // deps - path.set_file_name("cargo-clippy"); - path -}); +mod test_utils; #[test] fn dogfood_clippy() { // run clippy on itself and fail the test if lint warnings are reported - if cargo::is_rustc_test_suite() { + if IS_RUSTC_TEST_SUITE { return; } let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let mut command = Command::new(&*CLIPPY_PATH); + let mut command = Command::new(&*CARGO_CLIPPY_PATH); command .current_dir(root_dir) .env("CARGO_INCREMENTAL", "0") @@ -55,7 +48,7 @@ fn dogfood_clippy() { } fn test_no_deps_ignores_path_deps_in_workspaces() { - if cargo::is_rustc_test_suite() { + if IS_RUSTC_TEST_SUITE { return; } let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -74,7 +67,7 @@ fn test_no_deps_ignores_path_deps_in_workspaces() { // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint. // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`. - let output = Command::new(&*CLIPPY_PATH) + let output = Command::new(&*CARGO_CLIPPY_PATH) .current_dir(&cwd) .env("CARGO_INCREMENTAL", "0") .arg("clippy") @@ -93,7 +86,7 @@ fn test_no_deps_ignores_path_deps_in_workspaces() { let lint_path_dep = || { // Test that without the `--no-deps` argument, `path_dep` is linted. - let output = Command::new(&*CLIPPY_PATH) + let output = Command::new(&*CARGO_CLIPPY_PATH) .current_dir(&cwd) .env("CARGO_INCREMENTAL", "0") .arg("clippy") @@ -119,7 +112,7 @@ fn test_no_deps_ignores_path_deps_in_workspaces() { lint_path_dep(); let successful_build = || { - let output = Command::new(&*CLIPPY_PATH) + let output = Command::new(&*CARGO_CLIPPY_PATH) .current_dir(&cwd) .env("CARGO_INCREMENTAL", "0") .arg("clippy") @@ -153,7 +146,7 @@ fn test_no_deps_ignores_path_deps_in_workspaces() { #[test] fn dogfood_subprojects() { // run clippy on remaining subprojects and fail the test if lint warnings are reported - if cargo::is_rustc_test_suite() { + if IS_RUSTC_TEST_SUITE { return; } @@ -218,7 +211,7 @@ fn run_metadata_collection_lint() { fn run_clippy_for_project(project: &str) { let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let mut command = Command::new(&*CLIPPY_PATH); + let mut command = Command::new(&*test_utils::CARGO_CLIPPY_PATH); command .current_dir(root_dir.join(project)) diff --git a/tests/test_utils/mod.rs b/tests/test_utils/mod.rs new file mode 100644 index 00000000000..8a4de3f6def --- /dev/null +++ b/tests/test_utils/mod.rs @@ -0,0 +1,13 @@ +#![allow(dead_code)] // see https://github.com/rust-lang/rust/issues/46379 + +use std::lazy::SyncLazy; +use std::path::PathBuf; + +pub static CARGO_CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| { + let mut path = std::env::current_exe().unwrap(); + assert!(path.pop()); // deps + path.set_file_name("cargo-clippy"); + path +}); + +pub const IS_RUSTC_TEST_SUITE: bool = option_env!("RUSTC_TEST_SUITE").is_some(); |
