]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Fix redundant closure bugs
[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 // Dogfood cannot run on Windows
7 #![cfg(not(windows))]
8 #![feature(once_cell)]
9 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
10 #![warn(rust_2018_idioms, unused_lifetimes)]
11
12 use std::lazy::SyncLazy;
13 use std::path::PathBuf;
14 use std::process::Command;
15
16 mod cargo;
17
18 static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
19
20 #[test]
21 fn dogfood_clippy() {
22     // run clippy on itself and fail the test if lint warnings are reported
23     if cargo::is_rustc_test_suite() {
24         return;
25     }
26     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
27
28     let mut command = Command::new(&*CLIPPY_PATH);
29     command
30         .current_dir(root_dir)
31         .env("CLIPPY_DOGFOOD", "1")
32         .env("CARGO_INCREMENTAL", "0")
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("--no-deps")
82         .arg("--")
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
161     // NOTE: `path_dep` crate is omitted on purpose here
162     for project in &[
163         "clippy_workspace_tests",
164         "clippy_workspace_tests/src",
165         "clippy_workspace_tests/subcrate",
166         "clippy_workspace_tests/subcrate/src",
167         "clippy_dev",
168         "clippy_lints",
169         "clippy_utils",
170         "rustc_tools_util",
171     ] {
172         run_clippy_for_project(project);
173     }
174
175     // NOTE: Since tests run in parallel we can't run cargo commands on the same workspace at the
176     // same time, so we test this immediately after the dogfood for workspaces.
177     test_no_deps_ignores_path_deps_in_workspaces();
178 }
179
180 #[test]
181 #[ignore]
182 #[cfg(feature = "metadata-collector-lint")]
183 fn run_metadata_collection_lint() {
184     use std::fs::File;
185     use std::time::SystemTime;
186
187     // Setup for validation
188     let metadata_output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("util/gh-pages/lints.json");
189     let start_time = SystemTime::now();
190
191     // Run collection as is
192     std::env::set_var("ENABLE_METADATA_COLLECTION", "1");
193     run_clippy_for_project("clippy_lints");
194
195     // Check if cargo caching got in the way
196     if let Ok(file) = File::open(metadata_output_path) {
197         if let Ok(metadata) = file.metadata() {
198             if let Ok(last_modification) = metadata.modified() {
199                 if last_modification > start_time {
200                     // The output file has been modified. Most likely by a hungry
201                     // metadata collection monster. So We'll return.
202                     return;
203                 }
204             }
205         }
206     }
207
208     // Force cargo to invalidate the caches
209     filetime::set_file_mtime(
210         PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("clippy_lints/src/lib.rs"),
211         filetime::FileTime::now(),
212     )
213     .unwrap();
214
215     // Running the collection again
216     run_clippy_for_project("clippy_lints");
217 }
218
219 fn run_clippy_for_project(project: &str) {
220     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
221
222     let mut command = Command::new(&*CLIPPY_PATH);
223
224     command
225         .current_dir(root_dir.join(project))
226         .env("CLIPPY_DOGFOOD", "1")
227         .env("CARGO_INCREMENTAL", "0")
228         .arg("clippy")
229         .arg("--all-targets")
230         .arg("--all-features")
231         .arg("--")
232         .args(&["-D", "clippy::all"])
233         .args(&["-D", "clippy::pedantic"])
234         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
235
236     // internal lints only exist if we build with the internal-lints feature
237     if cfg!(feature = "internal-lints") {
238         command.args(&["-D", "clippy::internal"]);
239     }
240
241     let output = command.output().unwrap();
242
243     println!("status: {}", output.status);
244     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
245     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
246
247     assert!(output.status.success());
248 }