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
6 // Dogfood cannot run on Windows
10 use std::lazy::SyncLazy;
11 use std::path::PathBuf;
12 use std::process::Command;
16 static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
20 // run clippy on itself and fail the test if lint warnings are reported
21 if cargo::is_rustc_test_suite() {
24 let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
26 let mut command = Command::new(&*CLIPPY_PATH);
28 .current_dir(root_dir)
29 .env("CLIPPY_DOGFOOD", "1")
30 .env("CARGO_INCREMENTAL", "0")
33 .arg("--all-features")
35 .args(&["-D", "clippy::all"])
36 .args(&["-D", "clippy::pedantic"])
37 .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
39 // internal lints only exist if we build with the internal-lints feature
40 if cfg!(feature = "internal-lints") {
41 command.args(&["-D", "clippy::internal"]);
44 let output = command.output().unwrap();
46 println!("status: {}", output.status);
47 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
48 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
50 assert!(output.status.success());
53 fn test_no_deps_ignores_path_deps_in_workspaces() {
54 if cargo::is_rustc_test_suite() {
57 let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
58 let target_dir = root.join("target").join("dogfood");
59 let cwd = root.join("clippy_workspace_tests");
61 // Make sure we start with a clean state
64 .env("CARGO_TARGET_DIR", &target_dir)
66 .args(&["-p", "subcrate"])
67 .args(&["-p", "path_dep"])
71 // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
72 // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
73 let output = Command::new(&*CLIPPY_PATH)
75 .env("CLIPPY_DOGFOOD", "1")
76 .env("CARGO_INCREMENTAL", "0")
78 .args(&["-p", "subcrate"])
81 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
82 .args(&["--cfg", r#"feature="primary_package_test""#])
85 println!("status: {}", output.status);
86 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
87 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
89 assert!(output.status.success());
91 let lint_path_dep = || {
92 // Test that without the `--no-deps` argument, `path_dep` is linted.
93 let output = Command::new(&*CLIPPY_PATH)
95 .env("CLIPPY_DOGFOOD", "1")
96 .env("CARGO_INCREMENTAL", "0")
98 .args(&["-p", "subcrate"])
100 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
101 .args(&["--cfg", r#"feature="primary_package_test""#])
104 println!("status: {}", output.status);
105 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
106 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
108 assert!(!output.status.success());
110 String::from_utf8(output.stderr)
112 .contains("error: empty `loop {}` wastes CPU cycles")
116 // Make sure Cargo is aware of the removal of `--no-deps`.
119 let successful_build = || {
120 let output = Command::new(&*CLIPPY_PATH)
122 .env("CLIPPY_DOGFOOD", "1")
123 .env("CARGO_INCREMENTAL", "0")
125 .args(&["-p", "subcrate"])
127 .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
130 println!("status: {}", output.status);
131 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
132 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
134 assert!(output.status.success());
139 // Trigger a sucessful build, so Cargo would like to cache the build result.
142 // Make sure there's no spurious rebuild when nothing changes.
143 let stderr = String::from_utf8(successful_build().stderr).unwrap();
144 assert!(!stderr.contains("Compiling"));
145 assert!(!stderr.contains("Checking"));
146 assert!(stderr.contains("Finished"));
148 // Make sure Cargo is aware of the new `--cfg` flag.
153 fn dogfood_subprojects() {
154 // run clippy on remaining subprojects and fail the test if lint warnings are reported
155 if cargo::is_rustc_test_suite() {
158 let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
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 let mut command = Command::new(&*CLIPPY_PATH);
173 .current_dir(root_dir.join(d))
174 .env("CLIPPY_DOGFOOD", "1")
175 .env("CARGO_INCREMENTAL", "0")
177 .arg("--all-targets")
178 .arg("--all-features")
180 .args(&["-D", "clippy::all"])
181 .args(&["-D", "clippy::pedantic"])
182 .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
184 // internal lints only exist if we build with the internal-lints feature
185 if cfg!(feature = "internal-lints") {
186 command.args(&["-D", "clippy::internal"]);
189 let output = command.output().unwrap();
191 println!("status: {}", output.status);
192 println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
193 println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
195 assert!(output.status.success());
198 // NOTE: Since tests run in parallel we can't run cargo commands on the same workspace at the
199 // same time, so we test this immediately after the dogfood for workspaces.
200 test_no_deps_ignores_path_deps_in_workspaces();