Refactor test utils

This commit is contained in:
Cameron Steffen 2022-01-10 13:20:46 -06:00
parent e66ecf6f0e
commit 51dbbf3c4c
4 changed files with 27 additions and 23 deletions

View File

@ -1,4 +0,0 @@
#[must_use]
pub fn is_rustc_test_suite() -> bool {
option_env!("RUSTC_TEST_SUITE").is_some()
}

View File

@ -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;
}

View File

@ -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))

13
tests/test_utils/mod.rs Normal file
View File

@ -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();