From 8f15229183ca8f436875ffdf8bae862ef7346565 Mon Sep 17 00:00:00 2001 From: "sarunas.gincas" Date: Fri, 21 Jun 2024 15:58:43 +0300 Subject: [PATCH 1/5] Add cache hit percentage to stats --- src/server.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/server.rs b/src/server.rs index 094dc4ac9..6eefb3e2d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -38,7 +38,7 @@ use futures::{future, stream, Sink, SinkExt, Stream, StreamExt, TryFutureExt}; use number_prefix::NumberPrefix; use serde::{Deserialize, Serialize}; use std::cell::Cell; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::env; use std::ffi::{OsStr, OsString}; use std::future::Future; @@ -1510,6 +1510,17 @@ impl Default for ServerStats { } } +macro_rules! set_percentage_stat { + ($vec:ident, $count:expr, $sum:expr, $name:expr) => { + if $sum == 0 { + $vec.push(($name.to_string(), "-".to_string(), 0)); + } else { + let ratio = $count as f64 / $sum as f64; + $vec.push(($name.to_string(), format!("{:.2} %", ratio * 100.0), 2)); + } + }; +} + impl ServerStats { /// Print stats to stdout in a human-readable format. /// @@ -1570,6 +1581,9 @@ impl ServerStats { set_lang_stat!(stats_vec, self.cache_hits, "Cache hits"); set_lang_stat!(stats_vec, self.cache_misses, "Cache misses"); } + + self.set_percentage_stats(&mut stats_vec, advanced); + set_stat!(stats_vec, self.cache_timeouts, "Cache timeouts"); set_stat!(stats_vec, self.cache_read_errors, "Cache read errors"); set_stat!(stats_vec, self.forced_recaches, "Forced recaches"); @@ -1666,6 +1680,57 @@ impl ServerStats { } (name_width, stat_width) } + + fn set_percentage_stats(&self, stats_vec: &mut Vec<(String, String, usize)>, advanced: bool) { + set_percentage_stat!( + stats_vec, + self.cache_hits.all(), + self.cache_misses.all() + self.cache_hits.all(), + "Cache hits rate" + ); + + let (stats_hits, stats_misses): (Vec<_>, Vec<_>) = if advanced { + ( + self.cache_hits.adv_counts.iter().collect(), + self.cache_misses.adv_counts.iter().collect(), + ) + } else { + ( + self.cache_hits.counts.iter().collect(), + self.cache_misses.counts.iter().collect(), + ) + }; + + let mut all_languages: HashSet<&String> = HashSet::new(); + for (lang, _) in &stats_hits { + all_languages.insert(lang); + } + for (lang, _) in &stats_misses { + all_languages.insert(lang); + } + + let mut all_languages: Vec<&String> = all_languages.into_iter().collect(); + all_languages.sort(); + + for lang in all_languages { + let count_hits = stats_hits + .iter() + .find(|&&(l, _)| l == lang) + .map_or(0, |&(_, &count)| count); + + let count_misses = stats_misses + .iter() + .find(|&&(l, _)| l == lang) + .map_or(0, |&(_, &count)| count); + + set_percentage_stat!( + stats_vec, + count_hits, + count_hits + count_misses, + format!("Cache hits rate ({})", lang) + ); + } + } } impl ServerInfo { From 665075a9773a5e0f38e924f1cae6be0acc317a64 Mon Sep 17 00:00:00 2001 From: "sarunas.gincas" Date: Fri, 28 Jun 2024 13:35:16 +0300 Subject: [PATCH 2/5] Change macro to a function --- src/server.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/server.rs b/src/server.rs index 6eefb3e2d..b3e88e828 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1510,17 +1510,6 @@ impl Default for ServerStats { } } -macro_rules! set_percentage_stat { - ($vec:ident, $count:expr, $sum:expr, $name:expr) => { - if $sum == 0 { - $vec.push(($name.to_string(), "-".to_string(), 0)); - } else { - let ratio = $count as f64 / $sum as f64; - $vec.push(($name.to_string(), format!("{:.2} %", ratio * 100.0), 2)); - } - }; -} - impl ServerStats { /// Print stats to stdout in a human-readable format. /// @@ -1682,11 +1671,11 @@ impl ServerStats { } fn set_percentage_stats(&self, stats_vec: &mut Vec<(String, String, usize)>, advanced: bool) { - set_percentage_stat!( + set_percentage_stat( stats_vec, self.cache_hits.all(), self.cache_misses.all() + self.cache_hits.all(), - "Cache hits rate" + "Cache hits rate", ); let (stats_hits, stats_misses): (Vec<_>, Vec<_>) = if advanced { @@ -1723,16 +1712,30 @@ impl ServerStats { .find(|&&(l, _)| l == lang) .map_or(0, |&(_, &count)| count); - set_percentage_stat!( + set_percentage_stat( stats_vec, count_hits, count_hits + count_misses, - format!("Cache hits rate ({})", lang) + &format!("Cache hits rate ({})", lang), ); } } } +fn set_percentage_stat( + vec: &mut Vec<(String, String, usize)>, + count_hits: u64, + total: u64, + name: &str, +) { + if total == 0 { + vec.push((name.to_string(), "-".to_string(), 0)); + } else { + let ratio = count_hits as f64 / total as f64; + vec.push((name.to_string(), format!("{:.2} %", ratio * 100.0), 2)); + } +} + impl ServerInfo { pub async fn new(stats: ServerStats, storage: Option<&dyn Storage>) -> Result { let cache_location; From f5b64f1b05482c1de41e6e38e0b0b7a647c37981 Mon Sep 17 00:00:00 2001 From: "sarunas.gincas" Date: Sat, 29 Jun 2024 15:10:38 +0300 Subject: [PATCH 3/5] Add tests --- src/server.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 14 deletions(-) diff --git a/src/server.rs b/src/server.rs index b3e88e828..0128a4adf 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1510,11 +1510,23 @@ impl Default for ServerStats { } } +pub trait ServerStatsWriter { + fn write(&mut self, text: &str); +} + +pub struct StdoutServerStatsWriter; + +impl ServerStatsWriter for StdoutServerStatsWriter { + fn write(&mut self, text: &str) { + println!("{text}"); + } +} + impl ServerStats { - /// Print stats to stdout in a human-readable format. + /// Print stats in a human-readable format. /// /// Return the formatted width of each of the (name, value) columns. - fn print(&self, advanced: bool) -> (usize, usize) { + fn print(&self, writer: &mut T, advanced: bool) -> (usize, usize) { macro_rules! set_stat { ($vec:ident, $var:expr, $name:expr) => {{ // name, value, suffix length @@ -1630,42 +1642,42 @@ impl ServerStats { let name_width = stats_vec.iter().map(|(n, _, _)| n.len()).max().unwrap(); let stat_width = stats_vec.iter().map(|(_, s, _)| s.len()).max().unwrap(); for (name, stat, suffix_len) in stats_vec { - println!( + writer.write(&format!( "{:stat_width$}", name, stat, name_width = name_width, stat_width = stat_width + suffix_len - ); + )); } if !self.dist_compiles.is_empty() { - println!("\nSuccessful distributed compiles"); + writer.write("\nSuccessful distributed compiles"); let mut counts: Vec<_> = self.dist_compiles.iter().collect(); counts.sort_by(|(_, c1), (_, c2)| c1.cmp(c2).reverse()); for (reason, count) in counts { - println!( + writer.write(&format!( " {:stat_width$}", reason, count, name_width = name_width - 2, - stat_width = stat_width - ); + stat_width = stat_width, + )); } } if !self.not_cached.is_empty() { - println!("\nNon-cacheable reasons:"); + writer.write("\nNon-cacheable reasons:"); let mut counts: Vec<_> = self.not_cached.iter().collect(); counts.sort_by(|(_, c1), (_, c2)| c1.cmp(c2).reverse()); for (reason, count) in counts { - println!( + writer.write(&format!( "{:stat_width$}", reason, count, name_width = name_width, - stat_width = stat_width - ); + stat_width = stat_width, + )); } - println!(); + writer.write(""); } (name_width, stat_width) } @@ -1768,7 +1780,7 @@ impl ServerInfo { /// Print info to stdout in a human-readable format. pub fn print(&self, advanced: bool) { - let (name_width, stat_width) = self.stats.print(advanced); + let (name_width, stat_width) = self.stats.print(&mut StdoutServerStatsWriter, advanced); println!( "{: StringWriter { + StringWriter { + buffer: String::new(), + } + } + + fn get_output(self) -> String { + self.buffer + } + } + + impl ServerStatsWriter for StringWriter { + fn write(&mut self, text: &str) { + self.buffer.push_str(&format!("{}\n", text)); + } + } + + #[test] + fn test_print_cache_hits_rate_default_server_stats() { + let stats = ServerStats::default(); + + let mut writer = StringWriter::new(); + stats.print(&mut writer, false); + + let output = writer.get_output(); + + assert!(output.contains("Cache hits rate -")); + } + + #[test] + fn test_print_cache_hits_rate_server_stats() { + let mut cache_hits_counts = HashMap::new(); + cache_hits_counts.insert("Rust".to_string(), 100); + cache_hits_counts.insert("C/C++".to_string(), 200); + + let mut cache_misses_counts = HashMap::new(); + cache_misses_counts.insert("Rust".to_string(), 50); + cache_misses_counts.insert("Cuda".to_string(), 300); + + let stats = ServerStats { + cache_hits: PerLanguageCount { + counts: cache_hits_counts, + ..Default::default() + }, + cache_misses: PerLanguageCount { + counts: cache_misses_counts, + ..Default::default() + }, + ..Default::default() + }; + + let mut writer = StringWriter::new(); + stats.print(&mut writer, false); + + let output = writer.get_output(); + + assert!(output.contains("Cache hits rate 46.15 %")); + assert!(output.contains("Cache hits rate (C/C++) 100.00 %")); + assert!(output.contains("Cache hits rate (Cuda) 0.00 %")); + assert!(output.contains("Cache hits rate (Rust) 66.67 %")); + } + + #[test] + fn test_print_cache_hits_rate_advanced_server_stats() { + let mut cache_hits_counts = HashMap::new(); + cache_hits_counts.insert("rust".to_string(), 50); + cache_hits_counts.insert("c/c++ [clang]".to_string(), 30); + + let mut cache_misses_counts = HashMap::new(); + cache_misses_counts.insert("rust".to_string(), 100); + cache_misses_counts.insert("cuda".to_string(), 70); + + let stats = ServerStats { + cache_hits: PerLanguageCount { + adv_counts: cache_hits_counts, + ..Default::default() + }, + cache_misses: PerLanguageCount { + adv_counts: cache_misses_counts, + ..Default::default() + }, + ..Default::default() + }; + + let mut writer = StringWriter::new(); + stats.print(&mut writer, true); + + let output = writer.get_output(); + + assert!(output.contains("Cache hits rate -")); + assert!(output.contains("Cache hits rate (c/c++ [clang]) 100.00 %")); + assert!(output.contains("Cache hits rate (cuda) 0.00 %")); + assert!(output.contains("Cache hits rate (rust) 33.33 %")); + } +} From dd35cfa52bb603036eaec49b0ee78bef9f84cc58 Mon Sep 17 00:00:00 2001 From: "sarunas.gincas" Date: Wed, 17 Jul 2024 16:42:50 +0300 Subject: [PATCH 4/5] Add integration tests --- tests/cache_hit_rate.rs | 118 +++++++++++++++++++++++++++++++++ tests/helpers/mod.rs | 142 ++++++++++++++++++++++++++++++++++++++++ tests/sccache_args.rs | 22 ++----- tests/sccache_cargo.rs | 128 ++---------------------------------- 4 files changed, 270 insertions(+), 140 deletions(-) create mode 100644 tests/cache_hit_rate.rs create mode 100644 tests/helpers/mod.rs diff --git a/tests/cache_hit_rate.rs b/tests/cache_hit_rate.rs new file mode 100644 index 000000000..58f5526f1 --- /dev/null +++ b/tests/cache_hit_rate.rs @@ -0,0 +1,118 @@ +pub mod helpers; + +use std::process::Command; + +use anyhow::Result; +use assert_cmd::assert::OutputAssertExt; +use helpers::{cargo_clean, SccacheTest, CARGO, CRATE_DIR}; +use predicates::{boolean::PredicateBooleanExt, str::PredicateStrExt}; +use serial_test::serial; + +#[test] +#[serial] +fn test_cache_hit_rate() -> Result<()> { + let test_info = SccacheTest::new(None)?; + + Command::new(CARGO.as_os_str()) + .args(["build", "--color=never"]) + .envs(test_info.env.iter().cloned()) + .current_dir(CRATE_DIR.as_os_str()) + .assert() + .try_stderr(predicates::str::contains("\x1b[").from_utf8().not())? + .try_success()?; + + test_info + .show_text_stats(false)? + .try_stdout( + predicates::str::is_match(r"Cache hits rate\s+0\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_stdout( + predicates::str::is_match(r"Cache hits rate \(Rust\)\s+0\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_success()?; + + // Clean it so we can build it again. + cargo_clean(&test_info)?; + + Command::new(CARGO.as_os_str()) + .args(["run", "--color=always"]) + .envs(test_info.env.iter().cloned()) + .current_dir(CRATE_DIR.as_os_str()) + .assert() + .try_stderr(predicates::str::contains("\x1b[").from_utf8())? + .try_success()?; + + test_info + .show_text_stats(false)? + .try_stdout( + predicates::str::is_match(r"Cache hits rate\s+50\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_stdout( + predicates::str::is_match(r"Cache hits rate \(Rust\)\s+50\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_success()?; + + Ok(()) +} + +#[test] +#[serial] +fn test_adv_cache_hit_rate() -> Result<()> { + let test_info = SccacheTest::new(None)?; + + Command::new(CARGO.as_os_str()) + .args(["build", "--color=never"]) + .envs(test_info.env.iter().cloned()) + .current_dir(CRATE_DIR.as_os_str()) + .assert() + .try_stderr(predicates::str::contains("\x1b[").from_utf8().not())? + .try_success()?; + + test_info + .show_text_stats(true)? + .try_stdout( + predicates::str::is_match(r"Cache hits rate\s+0\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_stdout( + predicates::str::is_match(r"Cache hits rate \(rust\)\s+0\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_success()?; + + cargo_clean(&test_info)?; + + Command::new(CARGO.as_os_str()) + .args(["run", "--color=always"]) + .envs(test_info.env.iter().cloned()) + .current_dir(CRATE_DIR.as_os_str()) + .assert() + .try_stderr(predicates::str::contains("\x1b[").from_utf8())? + .try_success()?; + + test_info + .show_text_stats(true)? + .try_stdout( + predicates::str::is_match(r"Cache hits rate\s+50\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_stdout( + predicates::str::is_match(r"Cache hits rate \(rust\)\s+50\.00\s%") + .unwrap() + .from_utf8(), + )? + .try_success()?; + + Ok(()) +} diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs new file mode 100644 index 000000000..3fc36bf10 --- /dev/null +++ b/tests/helpers/mod.rs @@ -0,0 +1,142 @@ +use anyhow::{Context, Result}; +use assert_cmd::assert::OutputAssertExt; +use chrono::Local; +use fs_err as fs; +use log::trace; +use once_cell::sync::Lazy; +use std::convert::Infallible; +use std::ffi::OsString; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +pub static CRATE_DIR: Lazy = + Lazy::new(|| Path::new(file!()).parent().unwrap().join("../test-crate")); +pub static CARGO: Lazy = Lazy::new(|| std::env::var_os("CARGO").unwrap()); +pub static SCCACHE_BIN: Lazy = Lazy::new(|| assert_cmd::cargo::cargo_bin("sccache")); +/// Ensures the logger is only initialized once. Panics if initialization fails. +static LOGGER: Lazy> = Lazy::new(|| { + env_logger::Builder::new() + .format(|f, record| { + writeln!( + f, + "{} [{}] - {}", + Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"), + record.level(), + record.args() + ) + }) + .parse_env("RUST_LOG") + .init(); + Ok(()) +}); + +/// Used as a test setup fixture. The drop implementation cleans up after a _successful_ test. +/// We catch the panic to ensure that the drop runs and the TempDir is cleaned up. +pub struct SccacheTest<'a> { + /// Tempdir used for Sccache cache and cargo output. It is kept in the struct only to have the + /// destructor run when SccacheTest goes out of scope, but is never used otherwise. + #[allow(dead_code)] + pub tempdir: tempfile::TempDir, + pub env: Vec<(&'a str, std::ffi::OsString)>, +} + +impl SccacheTest<'_> { + pub fn new(additional_envs: Option<&[(&'static str, std::ffi::OsString)]>) -> Result { + assert!(LOGGER.is_ok()); + + // Create a temp directory to use for the disk cache. + let tempdir = tempfile::Builder::new() + .prefix("sccache_test_rust_cargo") + .tempdir() + .context("Failed to create tempdir")?; + let cache_dir = tempdir.path().join("cache"); + fs::create_dir(&cache_dir)?; + let cargo_dir = tempdir.path().join("cargo"); + fs::create_dir(&cargo_dir)?; + + // Ensure there's no existing sccache server running. + stop_sccache()?; + + trace!("sccache --start-server"); + + Command::new(SCCACHE_BIN.as_os_str()) + .arg("--start-server") + .env("SCCACHE_DIR", &cache_dir) + .assert() + .try_success() + .context("Failed to start sccache server")?; + + let mut env = vec![ + ("CARGO_TARGET_DIR", cargo_dir.as_os_str().to_owned()), + ("RUSTC_WRAPPER", SCCACHE_BIN.as_os_str().to_owned()), + // Explicitly disable incremental compilation because sccache is unable to cache it at + // the time of writing. + ("CARGO_INCREMENTAL", OsString::from("0")), + ("TEST_ENV_VAR", OsString::from("1")), + ]; + + if let Some(vec) = additional_envs { + env.extend_from_slice(vec); + } + + Ok(SccacheTest { + tempdir, + env: env.to_owned(), + }) + } + + /// Show the statistics for sccache. This will be called at the end of a test and making this + /// an associated function will ensure that the struct lives until the end of the test. + pub fn show_stats(&self) -> assert_cmd::assert::AssertResult { + trace!("sccache --show-stats"); + + Command::new(SCCACHE_BIN.as_os_str()) + .args(["--show-stats", "--stats-format=json"]) + .assert() + .try_success() + } + + pub fn show_text_stats(&self, advanced: bool) -> assert_cmd::assert::AssertResult { + let cmd = if advanced { + "--show-adv-stats" + } else { + "--show-stats" + }; + + trace!("sccache {cmd}"); + + Command::new(SCCACHE_BIN.as_os_str()) + .args([cmd, "--stats-format=text"]) + .assert() + .try_success() + } +} + +impl Drop for SccacheTest<'_> { + fn drop(&mut self) { + stop_sccache().expect("Stopping Sccache server failed"); + } +} + +pub fn stop_sccache() -> Result<()> { + trace!("sccache --stop-server"); + + Command::new(SCCACHE_BIN.as_os_str()) + .arg("--stop-server") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .context("Failed to stop sccache server")?; + Ok(()) +} + +pub fn cargo_clean(test_info: &SccacheTest) -> Result<()> { + Command::new(CARGO.as_os_str()) + .args(["clean"]) + .envs(test_info.env.iter().cloned()) + .current_dir(CRATE_DIR.as_os_str()) + .assert() + .try_success()?; + Ok(()) +} diff --git a/tests/sccache_args.rs b/tests/sccache_args.rs index 12b4bb89f..82f91b35a 100644 --- a/tests/sccache_args.rs +++ b/tests/sccache_args.rs @@ -2,32 +2,18 @@ //! //! Any copyright is dedicated to the Public Domain. //! http://creativecommons.org/publicdomain/zero/1.0/ +pub mod helpers; -use anyhow::{Context, Result}; +use anyhow::Result; use assert_cmd::prelude::*; -use once_cell::sync::Lazy; +use helpers::{stop_sccache, SCCACHE_BIN}; use predicates::prelude::*; use serial_test::serial; -use std::path::PathBuf; -use std::process::{Command, Stdio}; +use std::process::Command; #[macro_use] extern crate log; -static SCCACHE_BIN: Lazy = Lazy::new(|| assert_cmd::cargo::cargo_bin("sccache")); - -fn stop_sccache() -> Result<()> { - trace!("sccache --stop-server"); - - Command::new(SCCACHE_BIN.as_os_str()) - .arg("--stop-server") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .context("Failed to stop sccache server")?; - Ok(()) -} - #[test] #[serial] #[cfg(feature = "gcs")] diff --git a/tests/sccache_cargo.rs b/tests/sccache_cargo.rs index cafec343f..72863d452 100644 --- a/tests/sccache_cargo.rs +++ b/tests/sccache_cargo.rs @@ -3,128 +3,22 @@ //! Any copyright is dedicated to the Public Domain. //! http://creativecommons.org/publicdomain/zero/1.0/ +pub mod helpers; + use anyhow::{Context, Result}; -use once_cell::sync::Lazy; +use helpers::{cargo_clean, stop_sccache, CARGO, CRATE_DIR}; use assert_cmd::prelude::*; -use chrono::Local; use fs_err as fs; +use helpers::{SccacheTest, SCCACHE_BIN}; use predicates::prelude::*; use serial_test::serial; -use std::convert::Infallible; -use std::ffi::OsString; -use std::io::Write; -use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; +use std::path::Path; +use std::process::Command; #[macro_use] extern crate log; -static SCCACHE_BIN: Lazy = Lazy::new(|| assert_cmd::cargo::cargo_bin("sccache")); -static CARGO: Lazy = Lazy::new(|| std::env::var_os("CARGO").unwrap()); -static CRATE_DIR: Lazy = - Lazy::new(|| Path::new(file!()).parent().unwrap().join("test-crate")); -/// Ensures the logger is only initialized once. Panics if initialization fails. -static LOGGER: Lazy> = Lazy::new(|| { - env_logger::Builder::new() - .format(|f, record| { - writeln!( - f, - "{} [{}] - {}", - Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"), - record.level(), - record.args() - ) - }) - .parse_env("RUST_LOG") - .init(); - Ok(()) -}); - -/// Used as a test setup fixture. The drop implementation cleans up after a _successful_ test. -/// We catch the panic to ensure that the drop runs and the TempDir is cleaned up. -struct SccacheTest<'a> { - /// Tempdir used for Sccache cache and cargo output. It is kept in the struct only to have the - /// destructor run when SccacheTest goes out of scope, but is never used otherwise. - #[allow(dead_code)] - tempdir: tempfile::TempDir, - env: Vec<(&'a str, std::ffi::OsString)>, -} - -impl SccacheTest<'_> { - fn new(additional_envs: Option<&[(&'static str, std::ffi::OsString)]>) -> Result { - assert!(LOGGER.is_ok()); - - // Create a temp directory to use for the disk cache. - let tempdir = tempfile::Builder::new() - .prefix("sccache_test_rust_cargo") - .tempdir() - .context("Failed to create tempdir")?; - let cache_dir = tempdir.path().join("cache"); - fs::create_dir(&cache_dir)?; - let cargo_dir = tempdir.path().join("cargo"); - fs::create_dir(&cargo_dir)?; - - // Ensure there's no existing sccache server running. - stop_sccache()?; - - trace!("sccache --start-server"); - - Command::new(SCCACHE_BIN.as_os_str()) - .arg("--start-server") - .env("SCCACHE_DIR", &cache_dir) - .assert() - .try_success() - .context("Failed to start sccache server")?; - - let mut env = vec![ - ("CARGO_TARGET_DIR", cargo_dir.as_os_str().to_owned()), - ("RUSTC_WRAPPER", SCCACHE_BIN.as_os_str().to_owned()), - // Explicitly disable incremental compilation because sccache is unable to cache it at - // the time of writing. - ("CARGO_INCREMENTAL", OsString::from("0")), - ("TEST_ENV_VAR", OsString::from("1")), - ]; - - if let Some(vec) = additional_envs { - env.extend_from_slice(vec); - } - - Ok(SccacheTest { - tempdir, - env: env.to_owned(), - }) - } - - /// Show the statistics for sccache. This will be called at the end of a test and making this - /// an associated function will ensure that the struct lives until the end of the test. - fn show_stats(&self) -> assert_cmd::assert::AssertResult { - trace!("sccache --show-stats"); - Command::new(SCCACHE_BIN.as_os_str()) - .args(["--show-stats", "--stats-format=json"]) - .assert() - .try_success() - } -} - -impl Drop for SccacheTest<'_> { - fn drop(&mut self) { - stop_sccache().expect("Stopping Sccache server failed"); - } -} - -fn stop_sccache() -> Result<()> { - trace!("sccache --stop-server"); - - Command::new(SCCACHE_BIN.as_os_str()) - .arg("--stop-server") - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status() - .context("Failed to stop sccache server")?; - Ok(()) -} - #[test] #[serial] fn test_rust_cargo_check() -> Result<()> { @@ -236,16 +130,6 @@ fn test_rust_cargo_build_nightly_readonly() -> Result<()> { ) } -fn cargo_clean(test_info: &SccacheTest) -> Result<()> { - Command::new(CARGO.as_os_str()) - .args(["clean"]) - .envs(test_info.env.iter().cloned()) - .current_dir(CRATE_DIR.as_os_str()) - .assert() - .try_success()?; - Ok(()) -} - /// Test that building a simple Rust crate with cargo using sccache results in a cache hit /// when built a second time and a cache miss, when the environment variable referenced via /// env! is changed. From 1234cef560fd5c77665fddec65de4e51c8ca37d1 Mon Sep 17 00:00:00 2001 From: "sarunas.gincas" Date: Wed, 17 Jul 2024 17:47:51 +0300 Subject: [PATCH 5/5] Fix undeclared types --- tests/sccache_cargo.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/sccache_cargo.rs b/tests/sccache_cargo.rs index 72863d452..126f1e515 100644 --- a/tests/sccache_cargo.rs +++ b/tests/sccache_cargo.rs @@ -94,6 +94,8 @@ fn test_rust_cargo_run_with_env_dep_parsing() -> Result<()> { #[test] #[serial] fn test_rust_cargo_check_nightly() -> Result<()> { + use std::ffi::OsString; + test_rust_cargo_cmd( "check", SccacheTest::new(Some(&[("RUSTFLAGS", OsString::from("-Zprofile"))]))?, @@ -104,6 +106,8 @@ fn test_rust_cargo_check_nightly() -> Result<()> { #[test] #[serial] fn test_rust_cargo_check_nightly_readonly() -> Result<()> { + use std::ffi::OsString; + test_rust_cargo_cmd_readonly( "check", SccacheTest::new(Some(&[("RUSTFLAGS", OsString::from("-Zprofile"))]))?, @@ -114,6 +118,8 @@ fn test_rust_cargo_check_nightly_readonly() -> Result<()> { #[test] #[serial] fn test_rust_cargo_build_nightly() -> Result<()> { + use std::ffi::OsString; + test_rust_cargo_cmd( "build", SccacheTest::new(Some(&[("RUSTFLAGS", OsString::from("-Zprofile"))]))?, @@ -124,6 +130,8 @@ fn test_rust_cargo_build_nightly() -> Result<()> { #[test] #[serial] fn test_rust_cargo_build_nightly_readonly() -> Result<()> { + use std::ffi::OsString; + test_rust_cargo_cmd_readonly( "build", SccacheTest::new(Some(&[("RUSTFLAGS", OsString::from("-Zprofile"))]))?,