/src/tools/cargotest/main.rs
Rust | 139 lines | 120 code | 14 blank | 5 comment | 9 complexity | 8a2fe863c00b9d385b451cd4369e17d9 MD5 | raw file
Possible License(s): Apache-2.0, 0BSD, JSON, MIT
- use std::env;
- use std::fs;
- use std::path::{Path, PathBuf};
- use std::process::Command;
- struct Test {
- repo: &'static str,
- name: &'static str,
- sha: &'static str,
- lock: Option<&'static str>,
- packages: &'static [&'static str],
- }
- const TEST_REPOS: &[Test] = &[
- Test {
- name: "iron",
- repo: "https://github.com/iron/iron",
- sha: "cf056ea5e8052c1feea6141e40ab0306715a2c33",
- lock: None,
- packages: &[],
- },
- Test {
- name: "ripgrep",
- repo: "https://github.com/BurntSushi/ripgrep",
- sha: "ad9befbc1d3b5c695e7f6b6734ee1b8e683edd41",
- lock: None,
- packages: &[],
- },
- Test {
- name: "tokei",
- repo: "https://github.com/XAMPPRocky/tokei",
- sha: "a950ff128d5a435a8083b1c7577c0431f98360ca",
- lock: None,
- packages: &[],
- },
- Test {
- name: "xsv",
- repo: "https://github.com/BurntSushi/xsv",
- sha: "66956b6bfd62d6ac767a6b6499c982eae20a2c9f",
- lock: None,
- packages: &[],
- },
- Test {
- name: "servo",
- repo: "https://github.com/servo/servo",
- sha: "caac107ae8145ef2fd20365e2b8fadaf09c2eb3b",
- lock: None,
- // Only test Stylo a.k.a. Quantum CSS, the parts of Servo going into Firefox.
- // This takes much less time to build than all of Servo and supports stable Rust.
- packages: &["selectors"],
- },
- ];
- fn main() {
- let args = env::args().collect::<Vec<_>>();
- let cargo = &args[1];
- let out_dir = Path::new(&args[2]);
- let cargo = &Path::new(cargo);
- for test in TEST_REPOS.iter().rev() {
- test_repo(cargo, out_dir, test);
- }
- }
- fn test_repo(cargo: &Path, out_dir: &Path, test: &Test) {
- println!("testing {}", test.repo);
- let dir = clone_repo(test, out_dir);
- if let Some(lockfile) = test.lock {
- fs::write(&dir.join("Cargo.lock"), lockfile).unwrap();
- }
- if !run_cargo_test(cargo, &dir, test.packages) {
- panic!("tests failed for {}", test.repo);
- }
- }
- fn clone_repo(test: &Test, out_dir: &Path) -> PathBuf {
- let out_dir = out_dir.join(test.name);
- if !out_dir.join(".git").is_dir() {
- let status = Command::new("git").arg("init").arg(&out_dir).status().unwrap();
- assert!(status.success());
- }
- // Try progressively deeper fetch depths to find the commit
- let mut found = false;
- for depth in &[0, 1, 10, 100, 1000, 100000] {
- if *depth > 0 {
- let status = Command::new("git")
- .arg("fetch")
- .arg(test.repo)
- .arg("master")
- .arg(&format!("--depth={}", depth))
- .current_dir(&out_dir)
- .status()
- .unwrap();
- assert!(status.success());
- }
- let status = Command::new("git")
- .arg("reset")
- .arg(test.sha)
- .arg("--hard")
- .current_dir(&out_dir)
- .status()
- .unwrap();
- if status.success() {
- found = true;
- break;
- }
- }
- if !found {
- panic!("unable to find commit {}", test.sha)
- }
- let status =
- Command::new("git").arg("clean").arg("-fdx").current_dir(&out_dir).status().unwrap();
- assert!(status.success());
- out_dir
- }
- fn run_cargo_test(cargo_path: &Path, crate_path: &Path, packages: &[&str]) -> bool {
- let mut command = Command::new(cargo_path);
- command.arg("test");
- for name in packages {
- command.arg("-p").arg(name);
- }
- let status = command
- // Disable rust-lang/cargo's cross-compile tests
- .env("CFG_DISABLE_CROSS_TESTS", "1")
- // Relax #![deny(warnings)] in some crates
- .env("RUSTFLAGS", "--cap-lints warn")
- .current_dir(crate_path)
- .status()
- .unwrap();
- status.success()
- }