]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Move `test_no_deps_ignores_path_deps_in_workspaces()` out of `dogfood_subprojects()`
[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 fn test_no_deps_ignores_path_deps_in_workspaces() {
50     if cargo::is_rustc_test_suite() {
51         return;
52     }
53     let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
54     let target_dir = root.join("target").join("dogfood");
55     let cwd = root.join("clippy_workspace_tests");
56
57     // Make sure we start with a clean state
58     Command::new("cargo")
59         .current_dir(&cwd)
60         .env("CARGO_TARGET_DIR", &target_dir)
61         .arg("clean")
62         .args(&["-p", "subcrate"])
63         .args(&["-p", "path_dep"])
64         .output()
65         .unwrap();
66
67     // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
68     // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
69     let output = Command::new(&*CLIPPY_PATH)
70         .current_dir(&cwd)
71         .env("CLIPPY_DOGFOOD", "1")
72         .env("CARGO_INCREMENTAL", "0")
73         .arg("clippy")
74         .args(&["-p", "subcrate"])
75         .arg("--")
76         .arg("--no-deps")
77         .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
78         .args(&["--cfg", r#"feature="primary_package_test""#])
79         .output()
80         .unwrap();
81     println!("status: {}", output.status);
82     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
83     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
84
85     assert!(output.status.success());
86
87     let lint_path_dep = || {
88         // Test that without the `--no-deps` argument, `path_dep` is linted.
89         let output = Command::new(&*CLIPPY_PATH)
90             .current_dir(&cwd)
91             .env("CLIPPY_DOGFOOD", "1")
92             .env("CARGO_INCREMENTAL", "0")
93             .arg("clippy")
94             .args(&["-p", "subcrate"])
95             .arg("--")
96             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
97             .args(&["--cfg", r#"feature="primary_package_test""#])
98             .output()
99             .unwrap();
100         println!("status: {}", output.status);
101         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
102         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
103
104         assert!(!output.status.success());
105         assert!(
106             String::from_utf8(output.stderr)
107                 .unwrap()
108                 .contains("error: empty `loop {}` wastes CPU cycles")
109         );
110     };
111
112     // Make sure Cargo is aware of the removal of `--no-deps`.
113     lint_path_dep();
114
115     let successful_build = || {
116         let output = Command::new(&*CLIPPY_PATH)
117             .current_dir(&cwd)
118             .env("CLIPPY_DOGFOOD", "1")
119             .env("CARGO_INCREMENTAL", "0")
120             .arg("clippy")
121             .args(&["-p", "subcrate"])
122             .arg("--")
123             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
124             .output()
125             .unwrap();
126         println!("status: {}", output.status);
127         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
128         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
129
130         assert!(output.status.success());
131
132         output
133     };
134
135     // Trigger a sucessful build, so Cargo would like to cache the build result.
136     successful_build();
137
138     // Make sure there's no spurious rebuild when nothing changes.
139     let stderr = String::from_utf8(successful_build().stderr).unwrap();
140     assert!(!stderr.contains("Compiling"));
141     assert!(!stderr.contains("Checking"));
142     assert!(stderr.contains("Finished"));
143
144     // Make sure Cargo is aware of the new `--cfg` flag.
145     lint_path_dep();
146 }
147
148 #[test]
149 fn dogfood_subprojects() {
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 }