]> git.proxmox.com Git - rustc.git/blob - src/tools/cargotest/main.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / tools / cargotest / main.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::env;
12 use std::process::Command;
13 use std::path::{Path, PathBuf};
14 use std::fs::File;
15 use std::io::Write;
16
17 struct Test {
18 repo: &'static str,
19 name: &'static str,
20 sha: &'static str,
21 lock: Option<&'static str>,
22 }
23
24 const TEST_REPOS: &'static [Test] = &[
25 Test {
26 name: "iron",
27 repo: "https://github.com/iron/iron",
28 sha: "21c7dae29c3c214c08533c2a55ac649b418f2fe3",
29 lock: Some(include_str!("lockfiles/iron-Cargo.lock")),
30 },
31 Test {
32 name: "ripgrep",
33 repo: "https://github.com/BurntSushi/ripgrep",
34 sha: "b65bb37b14655e1a89c7cd19c8b011ef3e312791",
35 lock: None,
36 },
37 Test {
38 name: "tokei",
39 repo: "https://github.com/Aaronepower/tokei",
40 sha: "5e11c4852fe4aa086b0e4fe5885822fbe57ba928",
41 lock: None,
42 },
43 Test {
44 name: "treeify",
45 repo: "https://github.com/dzamlo/treeify",
46 sha: "999001b223152441198f117a68fb81f57bc086dd",
47 lock: None,
48 },
49 Test {
50 name: "xsv",
51 repo: "https://github.com/BurntSushi/xsv",
52 sha: "a9a7163f2a2953cea426fee1216bec914fe2f56a",
53 lock: None,
54 },
55 ];
56
57 fn main() {
58 let args = env::args().collect::<Vec<_>>();
59 let ref cargo = args[1];
60 let out_dir = Path::new(&args[2]);
61 let ref cargo = Path::new(cargo);
62
63 for test in TEST_REPOS.iter().rev() {
64 test_repo(cargo, out_dir, test);
65 }
66 }
67
68 fn test_repo(cargo: &Path, out_dir: &Path, test: &Test) {
69 println!("testing {}", test.repo);
70 let dir = clone_repo(test, out_dir);
71 if let Some(lockfile) = test.lock {
72 File::create(&dir.join("Cargo.lock"))
73 .expect("")
74 .write_all(lockfile.as_bytes())
75 .expect("");
76 }
77 if !run_cargo_test(cargo, &dir) {
78 panic!("tests failed for {}", test.repo);
79 }
80 }
81
82 fn clone_repo(test: &Test, out_dir: &Path) -> PathBuf {
83 let out_dir = out_dir.join(test.name);
84
85 if !out_dir.join(".git").is_dir() {
86 let status = Command::new("git")
87 .arg("init")
88 .arg(&out_dir)
89 .status()
90 .expect("");
91 assert!(status.success());
92 }
93
94 // Try progressively deeper fetch depths to find the commit
95 let mut found = false;
96 for depth in &[0, 1, 10, 100, 1000, 100000] {
97 if *depth > 0 {
98 let status = Command::new("git")
99 .arg("fetch")
100 .arg(test.repo)
101 .arg("master")
102 .arg(&format!("--depth={}", depth))
103 .current_dir(&out_dir)
104 .status()
105 .expect("");
106 assert!(status.success());
107 }
108
109 let status = Command::new("git")
110 .arg("reset")
111 .arg(test.sha)
112 .arg("--hard")
113 .current_dir(&out_dir)
114 .status()
115 .expect("");
116
117 if status.success() {
118 found = true;
119 break;
120 }
121 }
122
123 if !found {
124 panic!("unable to find commit {}", test.sha)
125 }
126 let status = Command::new("git")
127 .arg("clean")
128 .arg("-fdx")
129 .current_dir(&out_dir)
130 .status()
131 .unwrap();
132 assert!(status.success());
133
134 out_dir
135 }
136
137 fn run_cargo_test(cargo_path: &Path, crate_path: &Path) -> bool {
138 let status = Command::new(cargo_path)
139 .arg("test")
140 // Disable rust-lang/cargo's cross-compile tests
141 .env("CFG_DISABLE_CROSS_TESTS", "1")
142 .current_dir(crate_path)
143 .status()
144 .expect("");
145
146 status.success()
147 }