Add a way for tests to log to a file
Occasionally it is useful to see some information from running tests without making everything noisy from `--nocapture`. Add a function to log this kind of output to a file, and print the file as part of CI.
This commit is contained in:
parent
4b15d9e5e3
commit
b7b22b54c0
3 changed files with 73 additions and 0 deletions
|
|
@ -113,6 +113,11 @@ jobs:
|
|||
rustup target add x86_64-unknown-linux-musl
|
||||
cargo generate-lockfile && ./ci/run-docker.sh ${{ matrix.target }}
|
||||
|
||||
- name: Print test logs if available
|
||||
if: always()
|
||||
run: if [ -f "target/test-log.txt" ]; then cat target/test-log.txt; fi
|
||||
shell: bash
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-24.04
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ pub struct Config {
|
|||
pub manifest_dir: PathBuf,
|
||||
pub out_dir: PathBuf,
|
||||
pub opt_level: u8,
|
||||
pub cargo_features: Vec<String>,
|
||||
pub target_arch: String,
|
||||
pub target_env: String,
|
||||
pub target_family: Option<String>,
|
||||
|
|
@ -22,11 +23,16 @@ impl Config {
|
|||
let target_features = env::var("CARGO_CFG_TARGET_FEATURE")
|
||||
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect())
|
||||
.unwrap_or_default();
|
||||
let cargo_features = env::vars()
|
||||
.filter_map(|(name, _value)| name.strip_prefix("CARGO_FEATURE_").map(ToOwned::to_owned))
|
||||
.map(|s| s.to_lowercase().replace("_", "-"))
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
|
||||
out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()),
|
||||
opt_level: env::var("OPT_LEVEL").unwrap().parse().unwrap(),
|
||||
cargo_features,
|
||||
target_arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
|
||||
target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
|
||||
target_family: env::var("CARGO_CFG_TARGET_FAMILY").ok(),
|
||||
|
|
@ -45,6 +51,7 @@ pub fn emit_libm_config(cfg: &Config) {
|
|||
emit_arch_cfg();
|
||||
emit_optimization_cfg(cfg);
|
||||
emit_cfg_shorthands(cfg);
|
||||
emit_cfg_env(cfg);
|
||||
emit_f16_f128_cfg(cfg);
|
||||
}
|
||||
|
||||
|
|
@ -53,6 +60,7 @@ pub fn emit_libm_config(cfg: &Config) {
|
|||
pub fn emit_test_config(cfg: &Config) {
|
||||
emit_optimization_cfg(cfg);
|
||||
emit_cfg_shorthands(cfg);
|
||||
emit_cfg_env(cfg);
|
||||
emit_f16_f128_cfg(cfg);
|
||||
}
|
||||
|
||||
|
|
@ -97,6 +105,13 @@ fn emit_cfg_shorthands(cfg: &Config) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Reemit config that we make use of for test logging.
|
||||
fn emit_cfg_env(cfg: &Config) {
|
||||
println!("cargo:rustc-env=CFG_CARGO_FEATURES={:?}", cfg.cargo_features);
|
||||
println!("cargo:rustc-env=CFG_OPT_LEVEL={}", cfg.opt_level);
|
||||
println!("cargo:rustc-env=CFG_TARGET_FEATURES={:?}", cfg.target_features);
|
||||
}
|
||||
|
||||
/// Configure whether or not `f16` and `f128` support should be enabled.
|
||||
fn emit_f16_f128_cfg(cfg: &Config) {
|
||||
println!("cargo:rustc-check-cfg=cfg(f16_enabled)");
|
||||
|
|
|
|||
|
|
@ -13,6 +13,13 @@ mod precision;
|
|||
mod run_cfg;
|
||||
mod test_traits;
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub use f8_impl::f8;
|
||||
pub use libm::support::{Float, Int, IntTy, MinInt};
|
||||
pub use num::{FloatExt, logspace};
|
||||
|
|
@ -42,3 +49,49 @@ pub const fn ci() -> bool {
|
|||
Some(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Print to stderr and additionally log it to `target/test-log.txt`. This is useful for saving
|
||||
/// output that would otherwise be consumed by the test harness.
|
||||
pub fn test_log(s: &str) {
|
||||
// Handle to a file opened in append mode, unless a suitable path can't be determined.
|
||||
static OUTFILE: LazyLock<Option<File>> = LazyLock::new(|| {
|
||||
// If the target directory is overridden, use that environment variable. Otherwise, save
|
||||
// at the default path `{workspace_root}/target`.
|
||||
let target_dir = match env::var("CARGO_TARGET_DIR") {
|
||||
Ok(s) => PathBuf::from(s),
|
||||
Err(_) => {
|
||||
let Ok(x) = env::var("CARGO_MANIFEST_DIR") else {
|
||||
return None;
|
||||
};
|
||||
|
||||
PathBuf::from(x).parent().unwrap().parent().unwrap().join("target")
|
||||
}
|
||||
};
|
||||
let outfile = target_dir.join("test-log.txt");
|
||||
|
||||
let mut f = File::options()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(outfile)
|
||||
.expect("failed to open logfile");
|
||||
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
|
||||
|
||||
writeln!(f, "\n\nTest run at {}", now.as_secs()).unwrap();
|
||||
writeln!(f, "arch: {}", env::consts::ARCH).unwrap();
|
||||
writeln!(f, "os: {}", env::consts::OS).unwrap();
|
||||
writeln!(f, "bits: {}", usize::BITS).unwrap();
|
||||
writeln!(f, "emulated: {}", emulated()).unwrap();
|
||||
writeln!(f, "ci: {}", ci()).unwrap();
|
||||
writeln!(f, "cargo features: {}", env!("CFG_CARGO_FEATURES")).unwrap();
|
||||
writeln!(f, "opt level: {}", env!("CFG_OPT_LEVEL")).unwrap();
|
||||
writeln!(f, "target features: {}", env!("CFG_TARGET_FEATURES")).unwrap();
|
||||
|
||||
Some(f)
|
||||
});
|
||||
|
||||
eprintln!("{s}");
|
||||
|
||||
if let Some(mut f) = OUTFILE.as_ref() {
|
||||
writeln!(f, "{s}").unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue