]> git.lizzy.rs Git - rust.git/blob - tests/dogfood.rs
Add sysroot gettinh code to dogfood tests.
[rust.git] / tests / dogfood.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use std::path::PathBuf;
11 use std::process::Command;
12
13 fn rustc_sysroot_path() -> PathBuf {
14     option_env!("SYSROOT")
15         .map(String::from)
16         .or_else(|| std::env::var("SYSROOT").ok())
17         .or_else(|| {
18             let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
19             let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
20             home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
21         })
22         .or_else(|| {
23             Command::new("rustc")
24                 .arg("--print")
25                 .arg("sysroot")
26                 .output()
27                 .ok()
28                 .and_then(|out| String::from_utf8(out.stdout).ok())
29                 .map(|s| s.trim().to_owned())
30         })
31         .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
32         .into()
33 }
34
35 #[test]
36 fn dogfood() {
37     if option_env!("RUSTC_TEST_SUITE").is_some() || cfg!(windows) {
38         return;
39     }
40     let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
41     let clippy_cmd = std::path::Path::new(&root_dir)
42         .join("target")
43         .join(env!("PROFILE"))
44         .join("cargo-clippy");
45
46     let output = std::process::Command::new(clippy_cmd)
47         .current_dir(root_dir)
48         .env("CLIPPY_DOGFOOD", "1")
49         .env("RUSTFLAGS", format!("--sysroot {}", rustc_sysroot_path().display()))
50         .arg("clippy")
51         .arg("--all-targets")
52         .arg("--all-features")
53         .arg("--")
54         .args(&["-D", "clippy::all"])
55         .args(&["-D", "clippy::internal"])
56         .args(&["-D", "clippy::pedantic"])
57         .output()
58         .unwrap();
59     println!("status: {}", output.status);
60     println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
61     println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
62
63     assert!(output.status.success());
64 }
65
66 #[test]
67 fn dogfood_tests() {
68     if option_env!("RUSTC_TEST_SUITE").is_some() || cfg!(windows) {
69         return;
70     }
71     let root_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
72     let clippy_cmd = std::path::Path::new(&root_dir)
73         .join("target")
74         .join(env!("PROFILE"))
75         .join("cargo-clippy");
76
77     for d in &[
78         "clippy_workspace_tests",
79         "clippy_workspace_tests/src",
80         "clippy_workspace_tests/subcrate",
81         "clippy_workspace_tests/subcrate/src",
82         "clippy_dev",
83         "rustc_tools_util",
84     ] {
85         let output = std::process::Command::new(&clippy_cmd)
86             .current_dir(root_dir.join(d))
87             .env("CLIPPY_DOGFOOD", "1")
88             .env("RUSTFLAGS", format!("--sysroot {}", rustc_sysroot_path().display()))
89             .arg("clippy")
90             .arg("--")
91             .args(&["-D", "clippy::all"])
92             .args(&["-D", "clippy::pedantic"])
93             .output()
94             .unwrap();
95         println!("status: {}", output.status);
96         println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
97         println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
98
99         assert!(output.status.success());
100     }
101 }