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