Skip to content

Commit

Permalink
Refactor test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Jan 10, 2022
1 parent e66ecf6 commit 51dbbf3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
4 changes: 0 additions & 4 deletions tests/cargo/mod.rs

This file was deleted.

6 changes: 4 additions & 2 deletions 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)]

Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 10 additions & 17 deletions tests/dogfood.rs
Expand Up @@ -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")
Expand All @@ -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"));
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions 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();

0 comments on commit 51dbbf3

Please sign in to comment.