]> git.lizzy.rs Git - rust.git/blob - tests/integration.rs
Use pattern matching instead of manually checking condition
[rust.git] / tests / integration.rs
1 #![cfg(feature = "integration")]
2
3 use std::env;
4 use std::process::Command;
5
6 #[cfg_attr(feature = "integration", test)]
7 fn integration_test() {
8     let repo_name = env::var("INTEGRATION").expect("`INTEGRATION` var not set");
9     let repo_url = format!("https://github.com/{}", repo_name);
10     let crate_name = repo_name
11         .split('/')
12         .nth(1)
13         .expect("repo name should have format `<org>/<name>`");
14
15     let repo_dir = tempfile::tempdir()
16         .expect("couldn't create temp dir")
17         .path()
18         .join(crate_name);
19
20     let st = Command::new("git")
21         .args(&["clone", "--depth=1", &repo_url, repo_dir.to_str().unwrap()])
22         .status()
23         .expect("unable to run git");
24     assert!(st.success());
25
26     let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
27     let target_dir = std::path::Path::new(&root_dir).join("target");
28     let clippy_binary = target_dir.join(env!("PROFILE")).join("cargo-clippy");
29
30     let output = Command::new(clippy_binary)
31         .current_dir(repo_dir)
32         .env("RUST_BACKTRACE", "full")
33         .env("CARGO_TARGET_DIR", target_dir)
34         .args(&[
35             "clippy",
36             "--all-targets",
37             "--all-features",
38             "--",
39             "--cap-lints",
40             "warn",
41             "-Wclippy::pedantic",
42             "-Wclippy::nursery",
43         ])
44         .output()
45         .expect("unable to run clippy");
46
47     let stderr = String::from_utf8_lossy(&output.stderr);
48     if stderr.contains("internal compiler error") {
49         let backtrace_start = stderr
50             .find("thread 'rustc' panicked at")
51             .expect("start of backtrace not found");
52         let backtrace_end = stderr
53             .rfind("error: internal compiler error")
54             .expect("end of backtrace not found");
55
56         panic!(
57             "internal compiler error\nBacktrace:\n\n{}",
58             &stderr[backtrace_start..backtrace_end]
59         );
60     } else if stderr.contains("query stack during panic") {
61         panic!("query stack during panic in the output");
62     } else if stderr.contains("E0463") {
63         panic!("error: E0463");
64     } else if stderr.contains("E0514") {
65         panic!("incompatible crate versions");
66     } else if stderr.contains("failed to run `rustc` to learn about target-specific information") {
67         panic!("couldn't find librustc_driver, consider setting `LD_LIBRARY_PATH`");
68     }
69
70     match output.status.code() {
71         Some(0) => println!("Compilation successful"),
72         Some(code) => eprintln!("Compilation failed. Exit code: {}", code),
73         None => panic!("Process terminated by signal"),
74     }
75 }