]> git.lizzy.rs Git - rust.git/blob - tests/integration.rs
Make Clippy test no longer unsound
[rust.git] / tests / integration.rs
1 #![cfg(feature = "integration")]
2 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
3 #![warn(rust_2018_idioms, unused_lifetimes)]
4
5 use std::env;
6 use std::ffi::OsStr;
7 use std::process::Command;
8
9 #[cfg(not(windows))]
10 const CARGO_CLIPPY: &str = "cargo-clippy";
11 #[cfg(windows)]
12 const CARGO_CLIPPY: &str = "cargo-clippy.exe";
13
14 #[cfg_attr(feature = "integration", test)]
15 fn integration_test() {
16     let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
17     let repo_url = format!("https://github.com/{repo_name}");
18     let crate_name = repo_name
19         .split('/')
20         .nth(1)
21         .expect("repo name should have format `<org>/<name>`");
22
23     let mut repo_dir = tempfile::tempdir().expect("couldn't create temp dir").into_path();
24     repo_dir.push(crate_name);
25
26     let st = Command::new("git")
27         .args([
28             OsStr::new("clone"),
29             OsStr::new("--depth=1"),
30             OsStr::new(&repo_url),
31             OsStr::new(&repo_dir),
32         ])
33         .status()
34         .expect("unable to run git");
35     assert!(st.success());
36
37     let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
38     let target_dir = std::path::Path::new(&root_dir).join("target");
39     let clippy_binary = target_dir.join(env!("PROFILE")).join(CARGO_CLIPPY);
40
41     let output = Command::new(clippy_binary)
42         .current_dir(repo_dir)
43         .env("RUST_BACKTRACE", "full")
44         .env("CARGO_TARGET_DIR", target_dir)
45         .args([
46             "clippy",
47             "--all-targets",
48             "--all-features",
49             "--",
50             "--cap-lints",
51             "warn",
52             "-Wclippy::pedantic",
53             "-Wclippy::nursery",
54         ])
55         .output()
56         .expect("unable to run clippy");
57
58     let stderr = String::from_utf8_lossy(&output.stderr);
59     if let Some(backtrace_start) = stderr.find("error: internal compiler error") {
60         static BACKTRACE_END_MSG: &str = "end of query stack";
61         let backtrace_end = stderr[backtrace_start..]
62             .find(BACKTRACE_END_MSG)
63             .expect("end of backtrace not found");
64
65         panic!(
66             "internal compiler error\nBacktrace:\n\n{}",
67             &stderr[backtrace_start..backtrace_start + backtrace_end + BACKTRACE_END_MSG.len()]
68         );
69     } else if stderr.contains("query stack during panic") {
70         panic!("query stack during panic in the output");
71     } else if stderr.contains("E0463") {
72         // Encountering E0463 (can't find crate for `x`) did _not_ cause the build to fail in the
73         // past. Even though it should have. That's why we explicitly panic here.
74         // See PR #3552 and issue #3523 for more background.
75         panic!("error: E0463");
76     } else if stderr.contains("E0514") {
77         panic!("incompatible crate versions");
78     } else if stderr.contains("failed to run `rustc` to learn about target-specific information") {
79         panic!("couldn't find librustc_driver, consider setting `LD_LIBRARY_PATH`");
80     } else {
81         assert!(
82             !stderr.contains("toolchain") || !stderr.contains("is not installed"),
83             "missing required toolchain"
84         );
85     }
86
87     match output.status.code() {
88         Some(0) => println!("Compilation successful"),
89         Some(code) => eprintln!("Compilation failed. Exit code: {code}"),
90         None => panic!("Process terminated by signal"),
91     }
92 }