]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Rollup merge of #101142 - nnethercote:improve-hir-stats, r=davidtwco
[rust.git] / tests / dogfood.rs
1 //! This test is a part of quality control and makes clippy eat what it produces. Awesome lints and
2 //! long error messages
3 //!
4 //! See [Eating your own dog food](https://en.wikipedia.org/wiki/Eating_your_own_dog_food) for context
5
6 #![feature(once_cell)]
7 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
8 #![warn(rust_2018_idioms, unused_lifetimes)]
9
10 use std::path::PathBuf;
11 use std::process::Command;
12 use test_utils::IS_RUSTC_TEST_SUITE;
13
14 mod test_utils;
15
16 #[test]
17 fn dogfood_clippy() {
18     if IS_RUSTC_TEST_SUITE {
19         return;
20     }
21
22     // "" is the root package
23     for package in &["", "clippy_dev", "clippy_lints", "clippy_utils", "rustc_tools_util"] {
24         run_clippy_for_package(package, &["-D", "clippy::all", "-D", "clippy::pedantic"]);
25     }
26 }
27
28 #[test]
29 #[ignore]
30 #[cfg(feature = "internal")]
31 fn run_metadata_collection_lint() {
32     use std::fs::File;
33     use std::time::SystemTime;
34
35     // Setup for validation
36     let metadata_output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("util/gh-pages/lints.json");
37     let start_time = SystemTime::now();
38
39     // Run collection as is
40     std::env::set_var("ENABLE_METADATA_COLLECTION", "1");
41     run_clippy_for_package("clippy_lints", &["-A", "unfulfilled_lint_expectations"]);
42
43     // Check if cargo caching got in the way
44     if let Ok(file) = File::open(metadata_output_path) {
45         if let Ok(metadata) = file.metadata() {
46             if let Ok(last_modification) = metadata.modified() {
47                 if last_modification > start_time {
48                     // The output file has been modified. Most likely by a hungry
49                     // metadata collection monster. So We'll return.
50                     return;
51                 }
52             }
53         }
54     }
55
56     // Force cargo to invalidate the caches
57     filetime::set_file_mtime(
58         PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("clippy_lints/src/lib.rs"),
59         filetime::FileTime::now(),
60     )
61     .unwrap();
62
63     // Running the collection again
64     run_clippy_for_package("clippy_lints", &["-A", "unfulfilled_lint_expectations"]);
65 }
66
67 fn run_clippy_for_package(project: &str, args: &[&str]) {
68     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
69
70     let mut command = Command::new(&*test_utils::CARGO_CLIPPY_PATH);
71
72     command
73         .current_dir(root_dir.join(project))
74         .env("CARGO_INCREMENTAL", "0")
75         .arg("clippy")
76         .arg("--all-targets")
77         .arg("--all-features");
78
79     if let Ok(dogfood_args) = std::env::var("__CLIPPY_DOGFOOD_ARGS") {
80         for arg in dogfood_args.split_whitespace() {
81             command.arg(arg);
82         }
83     }
84
85     command.arg("--").args(args);
86     command.arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
87
88     if cfg!(feature = "internal") {
89         // internal lints only exist if we build with the internal feature
90         command.args(["-D", "clippy::internal"]);
91     } else {
92         // running a clippy built without internal lints on the clippy source
93         // that contains e.g. `allow(clippy::invalid_paths)`
94         command.args(["-A", "unknown_lints"]);
95     }
96
97     let output = command.output().unwrap();
98
99     println!("status: {}", output.status);
100     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
101     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
102
103     assert!(output.status.success());
104 }