2017-10-25 21:09:27 +03:00
|
|
|
//! System tests for compiling Rust code with cargo.
|
|
|
|
//!
|
|
|
|
//! Any copyright is dedicated to the Public Domain.
|
|
|
|
//! http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
|
2018-07-26 22:52:50 +03:00
|
|
|
extern crate assert_cmd;
|
2017-10-25 21:09:27 +03:00
|
|
|
extern crate chrono;
|
|
|
|
extern crate env_logger;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-07-26 22:52:50 +03:00
|
|
|
extern crate predicates;
|
2017-10-25 21:09:27 +03:00
|
|
|
extern crate tempdir;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2018-07-26 22:52:50 +03:00
|
|
|
use std::path::Path;
|
|
|
|
use assert_cmd::prelude::*;
|
2017-10-25 21:09:27 +03:00
|
|
|
use env_logger::LogBuilder;
|
|
|
|
use chrono::Local;
|
2018-07-26 22:52:50 +03:00
|
|
|
use predicates::prelude::*;
|
|
|
|
use std::process::{Command, Stdio};
|
2017-10-25 21:09:27 +03:00
|
|
|
use tempdir::TempDir;
|
|
|
|
|
2018-07-26 17:16:55 +03:00
|
|
|
fn stop() {
|
|
|
|
trace!("sccache --stop-server");
|
2018-07-26 22:52:50 +03:00
|
|
|
drop(Command::main_binary().unwrap()
|
|
|
|
.arg("--stop-server")
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.status());
|
2017-10-25 21:09:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test that building a simple Rust crate with cargo using sccache results in a cache hit
|
|
|
|
/// when built a second time.
|
|
|
|
#[test]
|
2017-10-30 19:53:11 +03:00
|
|
|
#[cfg(not(target_os="macos"))] // test currently fails on macos
|
2017-10-25 21:09:27 +03:00
|
|
|
fn test_rust_cargo() {
|
|
|
|
drop(LogBuilder::new()
|
|
|
|
.format(|record| {
|
|
|
|
format!("{} [{}] - {}",
|
|
|
|
Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
|
|
|
|
record.level(),
|
|
|
|
record.args())
|
|
|
|
})
|
|
|
|
.parse(&env::var("RUST_LOG").unwrap_or_default())
|
|
|
|
.init());
|
|
|
|
let cargo = env!("CARGO");
|
|
|
|
debug!("cargo: {}", cargo);
|
2018-07-26 22:52:50 +03:00
|
|
|
let sccache = assert_cmd::cargo::main_binary_path().unwrap();
|
2017-10-25 21:09:27 +03:00
|
|
|
debug!("sccache: {:?}", sccache);
|
|
|
|
let crate_dir = Path::new(file!()).parent().unwrap().join("test-crate");
|
|
|
|
// Ensure there's no existing sccache server running.
|
2018-07-26 17:16:55 +03:00
|
|
|
stop();
|
2017-10-25 21:09:27 +03:00
|
|
|
// Create a temp directory to use for the disk cache.
|
|
|
|
let tempdir = TempDir::new("sccache_test_rust_cargo").unwrap();
|
|
|
|
let cache_dir = tempdir.path().join("cache");
|
|
|
|
fs::create_dir(&cache_dir).unwrap();
|
|
|
|
let cargo_dir = tempdir.path().join("cargo");
|
|
|
|
fs::create_dir(&cargo_dir).unwrap();
|
|
|
|
// Start a new sccache server.
|
|
|
|
trace!("sccache --start-server");
|
2018-07-26 22:52:50 +03:00
|
|
|
Command::main_binary().unwrap()
|
|
|
|
.arg("--start-server")
|
|
|
|
.env("SCCACHE_DIR", &cache_dir)
|
|
|
|
.assert()
|
|
|
|
.success();
|
2017-10-25 21:09:27 +03:00
|
|
|
// `cargo clean` first, just to be sure there's no leftover build objects.
|
2018-07-26 22:52:50 +03:00
|
|
|
let envs = vec![("RUSTC_WRAPPER", &sccache),
|
|
|
|
("CARGO_TARGET_DIR", &cargo_dir)];
|
|
|
|
Command::new(&cargo)
|
|
|
|
.args(&["clean"])
|
|
|
|
.envs(envs.iter().map(|v| *v))
|
2018-07-26 17:16:55 +03:00
|
|
|
.current_dir(&crate_dir)
|
2018-07-26 22:52:50 +03:00
|
|
|
.assert()
|
|
|
|
.success();
|
2017-10-25 21:09:27 +03:00
|
|
|
// Now build the crate with cargo.
|
2018-07-26 22:52:50 +03:00
|
|
|
Command::new(&cargo)
|
|
|
|
.args(&["build", "--color=never"])
|
|
|
|
.envs(envs.iter().map(|v| *v))
|
|
|
|
.current_dir(&crate_dir)
|
|
|
|
.assert()
|
|
|
|
.stderr(predicates::str::contains("\x1b[").from_utf8().not())
|
|
|
|
.success();
|
2017-10-25 21:09:27 +03:00
|
|
|
// Clean it so we can build it again.
|
2018-07-26 22:52:50 +03:00
|
|
|
Command::new(&cargo)
|
|
|
|
.args(&["clean"])
|
|
|
|
.envs(envs.iter().map(|v| *v))
|
2018-07-26 17:16:55 +03:00
|
|
|
.current_dir(&crate_dir)
|
2018-07-26 22:52:50 +03:00
|
|
|
.assert()
|
|
|
|
.success();
|
|
|
|
Command::new(&cargo)
|
|
|
|
.args(&["build", "--color=always"])
|
|
|
|
.envs(envs.iter().map(|v| *v))
|
2018-07-26 17:16:55 +03:00
|
|
|
.current_dir(&crate_dir)
|
2018-07-26 22:52:50 +03:00
|
|
|
.assert()
|
|
|
|
.stderr(predicates::str::contains("\x1b[").from_utf8())
|
|
|
|
.success();
|
2017-10-25 21:09:27 +03:00
|
|
|
// Now get the stats and ensure that we had a cache hit for the second build.
|
2018-07-03 15:02:13 +03:00
|
|
|
// Ideally we'd check the stats more usefully here--the test crate has one dependency (itoa)
|
|
|
|
// so there are two separate compilations, but cargo will build the test crate with
|
|
|
|
// incremental compilation enabled, so sccache will not cache it.
|
2017-10-25 21:09:27 +03:00
|
|
|
trace!("sccache --show-stats");
|
2018-07-26 22:52:50 +03:00
|
|
|
Command::main_binary().unwrap()
|
|
|
|
.args(&["--show-stats", "--stats-format=json"])
|
|
|
|
.assert()
|
|
|
|
.stdout(predicates::str::contains(r#""cache_hits":1"#).from_utf8())
|
|
|
|
.success();
|
2018-07-26 17:16:55 +03:00
|
|
|
stop();
|
2017-10-25 21:09:27 +03:00
|
|
|
}
|