]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/workspace.rs
Preparing for merge from rustc
[rust.git] / src / tools / clippy / tests / workspace.rs
1 #![feature(once_cell)]
2
3 use std::path::PathBuf;
4 use std::process::Command;
5 use test_utils::{CARGO_CLIPPY_PATH, IS_RUSTC_TEST_SUITE};
6
7 mod test_utils;
8
9 #[test]
10 fn test_no_deps_ignores_path_deps_in_workspaces() {
11     if IS_RUSTC_TEST_SUITE {
12         return;
13     }
14     let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
15     let target_dir = root.join("target").join("workspace_test");
16     let cwd = root.join("tests/workspace_test");
17
18     // Make sure we start with a clean state
19     Command::new("cargo")
20         .current_dir(&cwd)
21         .env("CARGO_TARGET_DIR", &target_dir)
22         .arg("clean")
23         .args(["-p", "subcrate"])
24         .args(["-p", "path_dep"])
25         .output()
26         .unwrap();
27
28     // `path_dep` is a path dependency of `subcrate` that would trigger a denied lint.
29     // Make sure that with the `--no-deps` argument Clippy does not run on `path_dep`.
30     let output = Command::new(&*CARGO_CLIPPY_PATH)
31         .current_dir(&cwd)
32         .env("CARGO_INCREMENTAL", "0")
33         .env("CARGO_TARGET_DIR", &target_dir)
34         .arg("clippy")
35         .args(["-p", "subcrate"])
36         .arg("--no-deps")
37         .arg("--")
38         .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
39         .args(["--cfg", r#"feature="primary_package_test""#])
40         .output()
41         .unwrap();
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     let lint_path_dep = || {
49         // Test that without the `--no-deps` argument, `path_dep` is linted.
50         let output = Command::new(&*CARGO_CLIPPY_PATH)
51             .current_dir(&cwd)
52             .env("CARGO_INCREMENTAL", "0")
53             .env("CARGO_TARGET_DIR", &target_dir)
54             .arg("clippy")
55             .args(["-p", "subcrate"])
56             .arg("--")
57             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
58             .args(["--cfg", r#"feature="primary_package_test""#])
59             .output()
60             .unwrap();
61         println!("status: {}", output.status);
62         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
63         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
64
65         assert!(!output.status.success());
66         assert!(
67             String::from_utf8(output.stderr)
68                 .unwrap()
69                 .contains("error: empty `loop {}` wastes CPU cycles")
70         );
71     };
72
73     // Make sure Cargo is aware of the removal of `--no-deps`.
74     lint_path_dep();
75
76     let successful_build = || {
77         let output = Command::new(&*CARGO_CLIPPY_PATH)
78             .current_dir(&cwd)
79             .env("CARGO_INCREMENTAL", "0")
80             .env("CARGO_TARGET_DIR", &target_dir)
81             .arg("clippy")
82             .args(["-p", "subcrate"])
83             .arg("--")
84             .arg("-Cdebuginfo=0") // disable debuginfo to generate less data in the target dir
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         output
94     };
95
96     // Trigger a successful build, so Cargo would like to cache the build result.
97     successful_build();
98
99     // Make sure there's no spurious rebuild when nothing changes.
100     let stderr = String::from_utf8(successful_build().stderr).unwrap();
101     assert!(!stderr.contains("Compiling"));
102     assert!(!stderr.contains("Checking"));
103     assert!(stderr.contains("Finished"));
104
105     // Make sure Cargo is aware of the new `--cfg` flag.
106     lint_path_dep();
107 }