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