1 //! This test is a part of quality control and makes clippy eat what it produces. Awesome lints and
2 //! long error messages
4 //! See [Eating your own dog food](https://en.wikipedia.org/wiki/Eating_your_own_dog_food) for context
7 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
8 #![warn(rust_2018_idioms, unused_lifetimes)]
10 use std::lazy::SyncLazy;
11 use std::path::PathBuf;
12 use std::process::Command;
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");
25 // run clippy on itself and fail the test if lint warnings are reported
26 if cargo::is_rustc_test_suite() {
29 let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
31 let mut command = Command::new(&*CLIPPY_PATH);
33 .current_dir(root_dir)
34 .env("CARGO_INCREMENTAL", "0")
37 .arg("--all-features")
39 .args(&["-D", "clippy::all"])
40 .args(&["-D", "clippy::pedantic"])
41 .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
43 // internal lints only exist if we build with the internal feature
44 if cfg!(feature = "internal") {
45 command.args(&["-D", "clippy::internal"]);
48 let output = command.output().unwrap();
50 println!("status: {}", output.status);
51 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
52 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
54 assert!(output.status.success());
57 fn test_no_deps_ignores_path_deps_in_workspaces() {
58 if cargo::is_rustc_test_suite() {
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");
65 // Make sure we start with a clean state
68 .env("CARGO_TARGET_DIR", &target_dir)
70 .args(&["-p", "subcrate"])
71 .args(&["-p", "path_dep"])
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)
79 .env("CARGO_INCREMENTAL", "0")
81 .args(&["-p", "subcrate"])
84 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
85 .args(&["--cfg", r#"feature="primary_package_test""#])
88 println!("status: {}", output.status);
89 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
90 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
92 assert!(output.status.success());
94 let lint_path_dep = || {
95 // Test that without the `--no-deps` argument, `path_dep` is linted.
96 let output = Command::new(&*CLIPPY_PATH)
98 .env("CARGO_INCREMENTAL", "0")
100 .args(&["-p", "subcrate"])
102 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
103 .args(&["--cfg", r#"feature="primary_package_test""#])
106 println!("status: {}", output.status);
107 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
108 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
110 assert!(!output.status.success());
112 String::from_utf8(output.stderr)
114 .contains("error: empty `loop {}` wastes CPU cycles")
118 // Make sure Cargo is aware of the removal of `--no-deps`.
121 let successful_build = || {
122 let output = Command::new(&*CLIPPY_PATH)
124 .env("CARGO_INCREMENTAL", "0")
126 .args(&["-p", "subcrate"])
128 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
131 println!("status: {}", output.status);
132 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
133 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
135 assert!(output.status.success());
140 // Trigger a sucessful build, so Cargo would like to cache the build result.
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"));
149 // Make sure Cargo is aware of the new `--cfg` flag.
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() {
160 // NOTE: `path_dep` crate is omitted on purpose here
162 "clippy_workspace_tests",
163 "clippy_workspace_tests/src",
164 "clippy_workspace_tests/subcrate",
165 "clippy_workspace_tests/subcrate/src",
171 run_clippy_for_project(project);
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();
181 #[cfg(feature = "internal")]
182 fn run_metadata_collection_lint() {
184 use std::time::SystemTime;
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();
190 // Run collection as is
191 std::env::set_var("ENABLE_METADATA_COLLECTION", "1");
192 run_clippy_for_project("clippy_lints");
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.
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(),
214 // Running the collection again
215 run_clippy_for_project("clippy_lints");
218 fn run_clippy_for_project(project: &str) {
219 let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
221 let mut command = Command::new(&*CLIPPY_PATH);
224 .current_dir(root_dir.join(project))
225 .env("CARGO_INCREMENTAL", "0")
227 .arg("--all-targets")
228 .arg("--all-features")
230 .args(&["-D", "clippy::all"])
231 .args(&["-D", "clippy::pedantic"])
232 .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
234 // internal lints only exist if we build with the internal feature
235 if cfg!(feature = "internal") {
236 command.args(&["-D", "clippy::internal"]);
239 let output = command.output().unwrap();
241 println!("status: {}", output.status);
242 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
243 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
245 assert!(output.status.success());