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;
|
2018-10-23 10:33:42 +03:00
|
|
|
extern crate escargot;
|
2018-09-05 12:05:01 +03:00
|
|
|
#[cfg(not(target_os="macos"))]
|
2017-10-25 21:09:27 +03:00
|
|
|
#[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;
|
|
|
|
|
|
|
|
/// 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() {
|
2018-10-05 19:12:40 +03:00
|
|
|
test_rust_cargo_cmd("check");
|
|
|
|
test_rust_cargo_cmd("build");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os="macos"))] // test currently fails on macos
|
|
|
|
fn test_rust_cargo_cmd(cmd: &str) {
|
2018-09-05 12:05:01 +03:00
|
|
|
use std::env;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use chrono::Local;
|
2018-10-23 10:33:42 +03:00
|
|
|
use escargot::CargoBuild;
|
2018-09-05 12:05:01 +03:00
|
|
|
use predicates::prelude::*;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
use tempdir::TempDir;
|
|
|
|
|
2018-10-23 10:33:42 +03:00
|
|
|
fn sccache_command() -> Command {
|
|
|
|
CargoBuild::new()
|
|
|
|
.bin("sccache")
|
|
|
|
.current_release()
|
|
|
|
.current_target()
|
|
|
|
.run()
|
|
|
|
.unwrap()
|
|
|
|
.command()
|
|
|
|
}
|
|
|
|
|
2018-09-05 12:05:01 +03:00
|
|
|
fn stop() {
|
|
|
|
trace!("sccache --stop-server");
|
2018-10-23 10:33:42 +03:00
|
|
|
drop(sccache_command()
|
2018-09-05 12:05:01 +03:00
|
|
|
.arg("--stop-server")
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.status());
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:41:11 +03:00
|
|
|
drop(env_logger::Builder::new()
|
|
|
|
.format(|f, record| {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{} [{}] - {}",
|
|
|
|
Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
|
|
|
|
record.level(),
|
|
|
|
record.args()
|
|
|
|
)
|
2017-10-25 21:09:27 +03:00
|
|
|
})
|
|
|
|
.parse(&env::var("RUST_LOG").unwrap_or_default())
|
2018-10-05 19:12:40 +03:00
|
|
|
.try_init());
|
2017-10-25 21:09:27 +03:00
|
|
|
let cargo = env!("CARGO");
|
|
|
|
debug!("cargo: {}", cargo);
|
2018-08-28 14:29:00 +03:00
|
|
|
#[allow(deprecated)]
|
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-10-23 10:33:42 +03:00
|
|
|
sccache_command()
|
2018-07-26 22:52:50 +03:00
|
|
|
.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)
|
2018-10-05 19:12:40 +03:00
|
|
|
.args(&[cmd, "--color=never"])
|
2018-07-26 22:52:50 +03:00
|
|
|
.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)
|
2018-10-05 19:12:40 +03:00
|
|
|
.args(&[cmd, "--color=always"])
|
2018-07-26 22:52:50 +03:00
|
|
|
.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-10-23 10:33:42 +03:00
|
|
|
sccache_command()
|
2018-07-26 22:52:50 +03:00
|
|
|
.args(&["--show-stats", "--stats-format=json"])
|
|
|
|
.assert()
|
2019-01-10 07:50:55 +03:00
|
|
|
.stdout(predicates::str::contains(r#""cache_hits":{"counts":{"Rust":1}}"#).from_utf8())
|
2018-07-26 22:52:50 +03:00
|
|
|
.success();
|
2018-07-26 17:16:55 +03:00
|
|
|
stop();
|
2017-10-25 21:09:27 +03:00
|
|
|
}
|