]> git.lizzy.rs Git - rust.git/blob - src/tools/cargotest/main.rs
Auto merge of #69898 - spastorino:rename-rustc-guide2, r=Xanewok
[rust.git] / src / tools / cargotest / main.rs
1 #![deny(warnings)]
2
3 use std::env;
4 use std::fs;
5 use std::path::{Path, PathBuf};
6 use std::process::Command;
7
8 struct Test {
9     repo: &'static str,
10     name: &'static str,
11     sha: &'static str,
12     lock: Option<&'static str>,
13     packages: &'static [&'static str],
14 }
15
16 const TEST_REPOS: &'static [Test] = &[
17     Test {
18         name: "iron",
19         repo: "https://github.com/iron/iron",
20         sha: "cf056ea5e8052c1feea6141e40ab0306715a2c33",
21         lock: None,
22         packages: &[],
23     },
24     Test {
25         name: "ripgrep",
26         repo: "https://github.com/BurntSushi/ripgrep",
27         sha: "ad9befbc1d3b5c695e7f6b6734ee1b8e683edd41",
28         lock: None,
29         packages: &[],
30     },
31     Test {
32         name: "tokei",
33         repo: "https://github.com/XAMPPRocky/tokei",
34         sha: "5e11c4852fe4aa086b0e4fe5885822fbe57ba928",
35         lock: None,
36         packages: &[],
37     },
38     Test {
39         name: "treeify",
40         repo: "https://github.com/dzamlo/treeify",
41         sha: "999001b223152441198f117a68fb81f57bc086dd",
42         lock: None,
43         packages: &[],
44     },
45     Test {
46         name: "xsv",
47         repo: "https://github.com/BurntSushi/xsv",
48         sha: "66956b6bfd62d6ac767a6b6499c982eae20a2c9f",
49         lock: None,
50         packages: &[],
51     },
52     Test {
53         name: "servo",
54         repo: "https://github.com/servo/servo",
55         sha: "caac107ae8145ef2fd20365e2b8fadaf09c2eb3b",
56         lock: None,
57         // Only test Stylo a.k.a. Quantum CSS, the parts of Servo going into Firefox.
58         // This takes much less time to build than all of Servo and supports stable Rust.
59         packages: &["selectors"],
60     },
61 ];
62
63 fn main() {
64     let args = env::args().collect::<Vec<_>>();
65     let ref cargo = args[1];
66     let out_dir = Path::new(&args[2]);
67     let ref cargo = Path::new(cargo);
68
69     for test in TEST_REPOS.iter().rev() {
70         test_repo(cargo, out_dir, test);
71     }
72 }
73
74 fn test_repo(cargo: &Path, out_dir: &Path, test: &Test) {
75     println!("testing {}", test.repo);
76     let dir = clone_repo(test, out_dir);
77     if let Some(lockfile) = test.lock {
78         fs::write(&dir.join("Cargo.lock"), lockfile).unwrap();
79     }
80     if !run_cargo_test(cargo, &dir, test.packages) {
81         panic!("tests failed for {}", test.repo);
82     }
83 }
84
85 fn clone_repo(test: &Test, out_dir: &Path) -> PathBuf {
86     let out_dir = out_dir.join(test.name);
87
88     if !out_dir.join(".git").is_dir() {
89         let status = Command::new("git").arg("init").arg(&out_dir).status().expect("");
90         assert!(status.success());
91     }
92
93     // Try progressively deeper fetch depths to find the commit
94     let mut found = false;
95     for depth in &[0, 1, 10, 100, 1000, 100000] {
96         if *depth > 0 {
97             let status = Command::new("git")
98                 .arg("fetch")
99                 .arg(test.repo)
100                 .arg("master")
101                 .arg(&format!("--depth={}", depth))
102                 .current_dir(&out_dir)
103                 .status()
104                 .expect("");
105             assert!(status.success());
106         }
107
108         let status = Command::new("git")
109             .arg("reset")
110             .arg(test.sha)
111             .arg("--hard")
112             .current_dir(&out_dir)
113             .status()
114             .expect("");
115
116         if status.success() {
117             found = true;
118             break;
119         }
120     }
121
122     if !found {
123         panic!("unable to find commit {}", test.sha)
124     }
125     let status =
126         Command::new("git").arg("clean").arg("-fdx").current_dir(&out_dir).status().unwrap();
127     assert!(status.success());
128
129     out_dir
130 }
131
132 fn run_cargo_test(cargo_path: &Path, crate_path: &Path, packages: &[&str]) -> bool {
133     let mut command = Command::new(cargo_path);
134     command.arg("test");
135     for name in packages {
136         command.arg("-p").arg(name);
137     }
138     let status = command
139         // Disable rust-lang/cargo's cross-compile tests
140         .env("CFG_DISABLE_CROSS_TESTS", "1")
141         // Relax #![deny(warnings)] in some crates
142         .env("RUSTFLAGS", "--cap-lints warn")
143         .current_dir(crate_path)
144         .status()
145         .expect("");
146
147     status.success()
148 }