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