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