about summary refs log tree commit diff
path: root/src/tools/clippy/tests/dogfood.rs
blob: 389616801fcaaf6cdfe532d62ee8cf39a1ded59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! This test is a part of quality control and makes clippy eat what it produces. Awesome lints and
//! long error messages
//!
//! See [Eating your own dog food](https://en.wikipedia.org/wiki/Eating_your_own_dog_food) for context

#![warn(rust_2018_idioms, unused_lifetimes)]

use itertools::Itertools;
use std::io::{self, IsTerminal};
use std::path::PathBuf;
use std::process::Command;
use test_utils::IS_RUSTC_TEST_SUITE;
use ui_test::Args;

mod test_utils;

fn main() {
    if IS_RUSTC_TEST_SUITE {
        return;
    }

    let args = Args::test().unwrap();

    if args.list {
        if !args.ignored {
            println!("dogfood: test");
        }
    } else if !args.skip.iter().any(|arg| arg == "dogfood") {
        dogfood();
    }
}

fn dogfood() {
    let mut failed_packages = Vec::new();

    for package in [
        "./",
        "clippy_dev",
        "clippy_lints_internal",
        "clippy_lints",
        "clippy_utils",
        "clippy_config",
        "declare_clippy_lint",
        "lintcheck",
        "rustc_tools_util",
    ] {
        println!("linting {package}");
        if !run_clippy_for_package(package) {
            failed_packages.push(package);
        }
    }

    assert!(
        failed_packages.is_empty(),
        "Dogfood failed for packages `{}`",
        failed_packages.iter().join(", "),
    );
}

#[must_use]
fn run_clippy_for_package(project: &str) -> bool {
    let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

    let mut command = Command::new(&*test_utils::CARGO_CLIPPY_PATH);

    command
        .current_dir(root_dir.join(project))
        .env("CARGO_INCREMENTAL", "0")
        .arg("clippy")
        .arg("--all-targets")
        .arg("--all-features");

    if !io::stdout().is_terminal() {
        command.arg("-q");
    }

    if let Ok(dogfood_args) = std::env::var("__CLIPPY_DOGFOOD_ARGS") {
        for arg in dogfood_args.split_whitespace() {
            command.arg(arg);
        }
    }

    command.arg("--");
    command.arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
    command.args(["-D", "clippy::all", "-D", "clippy::pedantic", "-D", "clippy::dbg_macro"]);
    if !cfg!(feature = "internal") {
        // running a clippy built without internal lints on the clippy source
        // that contains e.g. `allow(clippy::symbol_as_str)`
        command.args(["-A", "unknown_lints"]);
    }

    // Workaround for not being a workspace, add the crate's directory back to the path
    command.args(["--remap-path-prefix", &format!("={project}")]);

    command.status().unwrap().success()
}