]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/dogfood.rs
Rollup merge of #84461 - jyn514:remove-strip-item, r=GuillaumeGomez
[rust.git] / src / tools / clippy / 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 // Dogfood cannot run on Windows
7 #![cfg(not(windows))]
8 #![feature(once_cell)]
9
10 use std::lazy::SyncLazy;
11 use std::path::PathBuf;
12 use std::process::Command;
13
14 mod cargo;
15
16 static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
17
18 #[test]
19 fn dogfood_clippy() {
20     // run clippy on itself and fail the test if lint warnings are reported
21     if cargo::is_rustc_test_suite() {
22         return;
23     }
24     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
25     let enable_metadata_collection = std::env::var("ENABLE_METADATA_COLLECTION").unwrap_or_else(|_| "0".to_string());
26
27     let mut command = Command::new(&*CLIPPY_PATH);
28     command
29         .current_dir(root_dir)
30         .env("CLIPPY_DOGFOOD", "1")
31         .env("CARGO_INCREMENTAL", "0")
32         .env("ENABLE_METADATA_COLLECTION", &enable_metadata_collection)
33         .arg("clippy")
34         .arg("--all-targets")
35         .arg("--all-features")
36         .arg("--")
37         .args(&["-D", "clippy::all"])
38         .args(&["-D", "clippy::pedantic"])
39         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
40
41     // internal lints only exist if we build with the internal-lints feature
42     if cfg!(feature = "internal-lints") {
43         command.args(&["-D", "clippy::internal"]);
44     }
45
46     let output = command.output().unwrap();
47
48     println!("status: {}", output.status);
49     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
50     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
51
52     assert!(output.status.success());
53 }
54
55 fn test_no_deps_ignores_path_deps_in_workspaces() {
56     if cargo::is_rustc_test_suite() {
57         return;
58     }
59     let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
60     let target_dir = root.join("target").join("dogfood");
61     let cwd = root.join("clippy_workspace_tests");
62
63     // Make sure we start with a clean state
64     Command::new("cargo")
65         .current_dir(&cwd)
66         .env("CARGO_TARGET_DIR", &target_dir)
67         .arg("clean")
68         .args(&["-p", "subcrate"])
69         .args(&["-p", "path_dep"])
70         .output()
71         .unwrap();
72
73     // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
74     // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
75     let output = Command::new(&*CLIPPY_PATH)
76         .current_dir(&cwd)
77         .env("CLIPPY_DOGFOOD", "1")
78         .env("CARGO_INCREMENTAL", "0")
79         .arg("clippy")
80         .args(&["-p", "subcrate"])
81         .arg("--")
82         .arg("--no-deps")
83         .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
84         .args(&["--cfg", r#"feature="primary_package_test""#])
85         .output()
86         .unwrap();
87     println!("status: {}", output.status);
88     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
89     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
90
91     assert!(output.status.success());
92
93     let lint_path_dep = || {
94         // Test that without the `--no-deps` argument, `path_dep` is linted.
95         let output = Command::new(&*CLIPPY_PATH)
96             .current_dir(&cwd)
97             .env("CLIPPY_DOGFOOD", "1")
98             .env("CARGO_INCREMENTAL", "0")
99             .arg("clippy")
100             .args(&["-p", "subcrate"])
101             .arg("--")
102             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
103             .args(&["--cfg", r#"feature="primary_package_test""#])
104             .output()
105             .unwrap();
106         println!("status: {}", output.status);
107         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
108         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
109
110         assert!(!output.status.success());
111         assert!(
112             String::from_utf8(output.stderr)
113                 .unwrap()
114                 .contains("error: empty `loop {}` wastes CPU cycles")
115         );
116     };
117
118     // Make sure Cargo is aware of the removal of `--no-deps`.
119     lint_path_dep();
120
121     let successful_build = || {
122         let output = Command::new(&*CLIPPY_PATH)
123             .current_dir(&cwd)
124             .env("CLIPPY_DOGFOOD", "1")
125             .env("CARGO_INCREMENTAL", "0")
126             .arg("clippy")
127             .args(&["-p", "subcrate"])
128             .arg("--")
129             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
130             .output()
131             .unwrap();
132         println!("status: {}", output.status);
133         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
134         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
135
136         assert!(output.status.success());
137
138         output
139     };
140
141     // Trigger a sucessful build, so Cargo would like to cache the build result.
142     successful_build();
143
144     // Make sure there's no spurious rebuild when nothing changes.
145     let stderr = String::from_utf8(successful_build().stderr).unwrap();
146     assert!(!stderr.contains("Compiling"));
147     assert!(!stderr.contains("Checking"));
148     assert!(stderr.contains("Finished"));
149
150     // Make sure Cargo is aware of the new `--cfg` flag.
151     lint_path_dep();
152 }
153
154 #[test]
155 fn dogfood_subprojects() {
156     // run clippy on remaining subprojects and fail the test if lint warnings are reported
157     if cargo::is_rustc_test_suite() {
158         return;
159     }
160     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
161
162     // NOTE: `path_dep` crate is omitted on purpose here
163     for d in &[
164         "clippy_workspace_tests",
165         "clippy_workspace_tests/src",
166         "clippy_workspace_tests/subcrate",
167         "clippy_workspace_tests/subcrate/src",
168         "clippy_dev",
169         "clippy_lints",
170         "clippy_utils",
171         "rustc_tools_util",
172     ] {
173         let mut command = Command::new(&*CLIPPY_PATH);
174         command
175             .current_dir(root_dir.join(d))
176             .env("CLIPPY_DOGFOOD", "1")
177             .env("CARGO_INCREMENTAL", "0")
178             .arg("clippy")
179             .arg("--all-targets")
180             .arg("--all-features")
181             .arg("--")
182             .args(&["-D", "clippy::all"])
183             .args(&["-D", "clippy::pedantic"])
184             .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
185
186         // internal lints only exist if we build with the internal-lints feature
187         if cfg!(feature = "internal-lints") {
188             command.args(&["-D", "clippy::internal"]);
189         }
190
191         let output = command.output().unwrap();
192
193         println!("status: {}", output.status);
194         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
195         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
196
197         assert!(output.status.success());
198     }
199
200     // NOTE: Since tests run in parallel we can't run cargo commands on the same workspace at the
201     // same time, so we test this immediately after the dogfood for workspaces.
202     test_no_deps_ignores_path_deps_in_workspaces();
203 }