]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Run dogfood on windows
[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::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(|| {
17     let mut path = std::env::current_exe().unwrap();
18     assert!(path.pop()); // deps
19     path.set_file_name("cargo-clippy");
20     path
21 });
22
23 #[test]
24 fn dogfood_clippy() {
25     // run clippy on itself and fail the test if lint warnings are reported
26     if cargo::is_rustc_test_suite() {
27         return;
28     }
29     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
30
31     let mut command = Command::new(&*CLIPPY_PATH);
32     command
33         .current_dir(root_dir)
34         .env("CARGO_INCREMENTAL", "0")
35         .arg("clippy")
36         .arg("--all-targets")
37         .arg("--all-features")
38         .arg("--")
39         .args(&["-D", "clippy::all"])
40         .args(&["-D", "clippy::pedantic"])
41         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
42
43     // internal lints only exist if we build with the internal feature
44     if cfg!(feature = "internal") {
45         command.args(&["-D", "clippy::internal"]);
46     }
47
48     let output = command.output().unwrap();
49
50     println!("status: {}", output.status);
51     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
52     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
53
54     assert!(output.status.success());
55 }
56
57 fn test_no_deps_ignores_path_deps_in_workspaces() {
58     if cargo::is_rustc_test_suite() {
59         return;
60     }
61     let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
62     let target_dir = root.join("target").join("dogfood");
63     let cwd = root.join("clippy_workspace_tests");
64
65     // Make sure we start with a clean state
66     Command::new("cargo")
67         .current_dir(&cwd)
68         .env("CARGO_TARGET_DIR", &target_dir)
69         .arg("clean")
70         .args(&["-p", "subcrate"])
71         .args(&["-p", "path_dep"])
72         .output()
73         .unwrap();
74
75     // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
76     // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
77     let output = Command::new(&*CLIPPY_PATH)
78         .current_dir(&cwd)
79         .env("CARGO_INCREMENTAL", "0")
80         .arg("clippy")
81         .args(&["-p", "subcrate"])
82         .arg("--no-deps")
83         .arg("--")
84         .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
85         .args(&["--cfg", r#"feature="primary_package_test""#])
86         .output()
87         .unwrap();
88     println!("status: {}", output.status);
89     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
90     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
91
92     assert!(output.status.success());
93
94     let lint_path_dep = || {
95         // Test that without the `--no-deps` argument, `path_dep` is linted.
96         let output = Command::new(&*CLIPPY_PATH)
97             .current_dir(&cwd)
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("CARGO_INCREMENTAL", "0")
125             .arg("clippy")
126             .args(&["-p", "subcrate"])
127             .arg("--")
128             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
129             .output()
130             .unwrap();
131         println!("status: {}", output.status);
132         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
133         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
134
135         assert!(output.status.success());
136
137         output
138     };
139
140     // Trigger a sucessful build, so Cargo would like to cache the build result.
141     successful_build();
142
143     // Make sure there's no spurious rebuild when nothing changes.
144     let stderr = String::from_utf8(successful_build().stderr).unwrap();
145     assert!(!stderr.contains("Compiling"));
146     assert!(!stderr.contains("Checking"));
147     assert!(stderr.contains("Finished"));
148
149     // Make sure Cargo is aware of the new `--cfg` flag.
150     lint_path_dep();
151 }
152
153 #[test]
154 fn dogfood_subprojects() {
155     // run clippy on remaining subprojects and fail the test if lint warnings are reported
156     if cargo::is_rustc_test_suite() {
157         return;
158     }
159
160     // NOTE: `path_dep` crate is omitted on purpose here
161     for project in &[
162         "clippy_workspace_tests",
163         "clippy_workspace_tests/src",
164         "clippy_workspace_tests/subcrate",
165         "clippy_workspace_tests/subcrate/src",
166         "clippy_dev",
167         "clippy_lints",
168         "clippy_utils",
169         "rustc_tools_util",
170     ] {
171         run_clippy_for_project(project);
172     }
173
174     // NOTE: Since tests run in parallel we can't run cargo commands on the same workspace at the
175     // same time, so we test this immediately after the dogfood for workspaces.
176     test_no_deps_ignores_path_deps_in_workspaces();
177 }
178
179 #[test]
180 #[ignore]
181 #[cfg(feature = "internal")]
182 fn run_metadata_collection_lint() {
183     use std::fs::File;
184     use std::time::SystemTime;
185
186     // Setup for validation
187     let metadata_output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("util/gh-pages/lints.json");
188     let start_time = SystemTime::now();
189
190     // Run collection as is
191     std::env::set_var("ENABLE_METADATA_COLLECTION", "1");
192     run_clippy_for_project("clippy_lints");
193
194     // Check if cargo caching got in the way
195     if let Ok(file) = File::open(metadata_output_path) {
196         if let Ok(metadata) = file.metadata() {
197             if let Ok(last_modification) = metadata.modified() {
198                 if last_modification > start_time {
199                     // The output file has been modified. Most likely by a hungry
200                     // metadata collection monster. So We'll return.
201                     return;
202                 }
203             }
204         }
205     }
206
207     // Force cargo to invalidate the caches
208     filetime::set_file_mtime(
209         PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("clippy_lints/src/lib.rs"),
210         filetime::FileTime::now(),
211     )
212     .unwrap();
213
214     // Running the collection again
215     run_clippy_for_project("clippy_lints");
216 }
217
218 fn run_clippy_for_project(project: &str) {
219     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
220
221     let mut command = Command::new(&*CLIPPY_PATH);
222
223     command
224         .current_dir(root_dir.join(project))
225         .env("CARGO_INCREMENTAL", "0")
226         .arg("clippy")
227         .arg("--all-targets")
228         .arg("--all-features")
229         .arg("--")
230         .args(&["-D", "clippy::all"])
231         .args(&["-D", "clippy::pedantic"])
232         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
233
234     // internal lints only exist if we build with the internal feature
235     if cfg!(feature = "internal") {
236         command.args(&["-D", "clippy::internal"]);
237     }
238
239     let output = command.output().unwrap();
240
241     println!("status: {}", output.status);
242     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
243     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
244
245     assert!(output.status.success());
246 }