]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Auto merge of #6709 - rust-lang:flip1995-patch-1, r=matthiaskrgr
[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::{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         .arg("--")
30         .args(&["-D", "clippy::all"])
31         .args(&["-D", "clippy::pedantic"])
32         .arg("-Cdebuginfo=0"); // disable debuginfo to generate less data in the target dir
33
34     // internal lints only exist if we build with the internal-lints feature
35     if cfg!(feature = "internal-lints") {
36         command.args(&["-D", "clippy::internal"]);
37     }
38
39     let output = command.output().unwrap();
40
41     println!("status: {}", output.status);
42     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
43     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
44
45     assert!(output.status.success());
46 }
47
48 #[test]
49 fn dogfood_subprojects() {
50     fn test_no_deps_ignores_path_deps_in_workspaces() {
51         fn clean(cwd: &Path, target_dir: &Path) {
52             Command::new("cargo")
53                 .current_dir(cwd)
54                 .env("CARGO_TARGET_DIR", target_dir)
55                 .arg("clean")
56                 .args(&["-p", "subcrate"])
57                 .args(&["-p", "path_dep"])
58                 .output()
59                 .unwrap();
60         }
61
62         if cargo::is_rustc_test_suite() {
63             return;
64         }
65         let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
66         let target_dir = root.join("target").join("dogfood");
67         let cwd = root.join("clippy_workspace_tests");
68
69         // Make sure we start with a clean state
70         clean(&cwd, &target_dir);
71
72         // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
73         // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
74         let output = Command::new(&*CLIPPY_PATH)
75             .current_dir(&cwd)
76             .env("CLIPPY_DOGFOOD", "1")
77             .env("CARGO_INCREMENTAL", "0")
78             .arg("clippy")
79             .args(&["-p", "subcrate"])
80             .arg("--")
81             .arg("--no-deps")
82             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
83             .args(&["--cfg", r#"feature="primary_package_test""#])
84             .output()
85             .unwrap();
86         println!("status: {}", output.status);
87         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
88         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
89
90         assert!(output.status.success());
91
92         // Make sure we start with a clean state
93         clean(&cwd, &target_dir);
94
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("CLIPPY_DOGFOOD", "1")
99             .env("CARGO_INCREMENTAL", "0")
100             .arg("clippy")
101             .args(&["-p", "subcrate"])
102             .arg("--")
103             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
104             .args(&["--cfg", r#"feature="primary_package_test""#])
105             .output()
106             .unwrap();
107         println!("status: {}", output.status);
108         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
109         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
110
111         assert!(!output.status.success());
112     }
113
114     // run clippy on remaining subprojects and fail the test if lint warnings are reported
115     if cargo::is_rustc_test_suite() {
116         return;
117     }
118     let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
119
120     // NOTE: `path_dep` crate is omitted on purpose here
121     for d in &[
122         "clippy_workspace_tests",
123         "clippy_workspace_tests/src",
124         "clippy_workspace_tests/subcrate",
125         "clippy_workspace_tests/subcrate/src",
126         "clippy_dev",
127         "rustc_tools_util",
128     ] {
129         let output = Command::new(&*CLIPPY_PATH)
130             .current_dir(root_dir.join(d))
131             .env("CLIPPY_DOGFOOD", "1")
132             .env("CARGO_INCREMENTAL", "0")
133             .arg("clippy")
134             .arg("--")
135             .args(&["-D", "clippy::all"])
136             .args(&["-D", "clippy::pedantic"])
137             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
138             .output()
139             .unwrap();
140         println!("status: {}", output.status);
141         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
142         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
143
144         assert!(output.status.success());
145     }
146
147     // NOTE: Since tests run in parallel we can't run cargo commands on the same workspace at the
148     // same time, so we test this immediately after the dogfood for workspaces.
149     test_no_deps_ignores_path_deps_in_workspaces();
150 }