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