]> git.lizzy.rs Git - rust.git/blob - src/tools/cargotest/main.rs
Do not show `::constructor` on tuple struct diagnostics
[rust.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: "cargo",
27         repo: "https://github.com/rust-lang/cargo",
28         sha: "0e1e34be7540bdaed4918457654fbf028cf69e56",
29         lock: None,
30     },
31     Test {
32         name: "iron",
33         repo: "https://github.com/iron/iron",
34         sha: "21c7dae29c3c214c08533c2a55ac649b418f2fe3",
35         lock: Some(include_str!("lockfiles/iron-Cargo.lock")),
36     },
37     Test {
38         name: "ripgrep",
39         repo: "https://github.com/BurntSushi/ripgrep",
40         sha: "b65bb37b14655e1a89c7cd19c8b011ef3e312791",
41         lock: None,
42     },
43     Test {
44         name: "tokei",
45         repo: "https://github.com/Aaronepower/tokei",
46         sha: "5e11c4852fe4aa086b0e4fe5885822fbe57ba928",
47         lock: None,
48     },
49     Test {
50         name: "treeify",
51         repo: "https://github.com/dzamlo/treeify",
52         sha: "999001b223152441198f117a68fb81f57bc086dd",
53         lock: None,
54     },
55     Test {
56         name: "xsv",
57         repo: "https://github.com/BurntSushi/xsv",
58         sha: "a9a7163f2a2953cea426fee1216bec914fe2f56a",
59         lock: None,
60     },
61 ];
62
63 fn main() {
64     // One of the projects being tested here is Cargo, and when being tested
65     // Cargo will at some point call `nmake.exe` on Windows MSVC. Unfortunately
66     // `nmake` will read these two environment variables below and try to
67     // intepret them. We're likely being run, however, from MSYS `make` which
68     // uses the same variables.
69     //
70     // As a result, to prevent confusion and errors, we remove these variables
71     // from our environment to prevent passing MSYS make flags to nmake, causing
72     // it to blow up.
73     if cfg!(target_env = "msvc") {
74         env::remove_var("MAKE");
75         env::remove_var("MAKEFLAGS");
76     }
77
78     let args = env::args().collect::<Vec<_>>();
79     let ref cargo = args[1];
80     let out_dir = Path::new(&args[2]);
81     let ref cargo = Path::new(cargo);
82
83     for test in TEST_REPOS.iter().rev() {
84         test_repo(cargo, out_dir, test);
85     }
86 }
87
88 fn test_repo(cargo: &Path, out_dir: &Path, test: &Test) {
89     println!("testing {}", test.repo);
90     let dir = clone_repo(test, out_dir);
91     if let Some(lockfile) = test.lock {
92         File::create(&dir.join("Cargo.lock"))
93             .expect("")
94             .write_all(lockfile.as_bytes())
95             .expect("");
96     }
97     if !run_cargo_test(cargo, &dir) {
98         panic!("tests failed for {}", test.repo);
99     }
100 }
101
102 fn clone_repo(test: &Test, out_dir: &Path) -> PathBuf {
103     let out_dir = out_dir.join(test.name);
104
105     if !out_dir.join(".git").is_dir() {
106         let status = Command::new("git")
107                          .arg("init")
108                          .arg(&out_dir)
109                          .status()
110                          .expect("");
111         assert!(status.success());
112     }
113
114     // Try progressively deeper fetch depths to find the commit
115     let mut found = false;
116     for depth in &[0, 1, 10, 100, 1000, 100000] {
117         if *depth > 0 {
118             let status = Command::new("git")
119                              .arg("fetch")
120                              .arg(test.repo)
121                              .arg("master")
122                              .arg(&format!("--depth={}", depth))
123                              .current_dir(&out_dir)
124                              .status()
125                              .expect("");
126             assert!(status.success());
127         }
128
129         let status = Command::new("git")
130                          .arg("reset")
131                          .arg(test.sha)
132                          .arg("--hard")
133                          .current_dir(&out_dir)
134                          .status()
135                          .expect("");
136
137         if status.success() {
138             found = true;
139             break;
140         }
141     }
142
143     if !found {
144         panic!("unable to find commit {}", test.sha)
145     }
146     let status = Command::new("git")
147                      .arg("clean")
148                      .arg("-fdx")
149                      .current_dir(&out_dir)
150                      .status()
151                      .unwrap();
152     assert!(status.success());
153
154     out_dir
155 }
156
157 fn run_cargo_test(cargo_path: &Path, crate_path: &Path) -> bool {
158     let status = Command::new(cargo_path)
159         .arg("test")
160         // Disable rust-lang/cargo's cross-compile tests
161         .env("CFG_DISABLE_CROSS_TESTS", "1")
162         .current_dir(crate_path)
163         .status()
164         .expect("");
165
166     status.success()
167 }