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