]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Bless clippy.
[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);
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");
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");
65 }
66
67 fn run_clippy_for_package(project: &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         .arg("--")
79         .args(&["-D", "clippy::all"])
80         .args(&["-D", "clippy::pedantic"])
81         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
82
83     // internal lints only exist if we build with the internal feature
84     if cfg!(feature = "internal") {
85         command.args(&["-D", "clippy::internal"]);
86     }
87
88     let output = command.output().unwrap();
89
90     println!("status: {}", output.status);
91     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
92     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
93
94     assert!(output.status.success());
95 }