Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 1b555ca

Browse files
committed
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.
1 parent 110c6f7 commit 1b555ca

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ jobs:
113113
rustup target add x86_64-unknown-linux-musl
114114
cargo generate-lockfile && ./ci/run-docker.sh ${{ matrix.target }}
115115
116+
- name: Print test logs if available
117+
if: always()
118+
run: if [ -f "target/test-log.txt" ]; then cat target/test-log.txt; fi
119+
shell: bash
120+
116121
clippy:
117122
name: Clippy
118123
runs-on: ubuntu-24.04

configure.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Config {
88
pub manifest_dir: PathBuf,
99
pub out_dir: PathBuf,
1010
pub opt_level: u8,
11+
pub cargo_features: Vec<String>,
1112
pub target_arch: String,
1213
pub target_env: String,
1314
pub target_family: Option<String>,
@@ -22,11 +23,16 @@ impl Config {
2223
let target_features = env::var("CARGO_CFG_TARGET_FEATURE")
2324
.map(|feats| feats.split(',').map(ToOwned::to_owned).collect())
2425
.unwrap_or_default();
26+
let cargo_features = env::vars()
27+
.filter_map(|(name, _value)| name.strip_prefix("CARGO_FEATURE_").map(ToOwned::to_owned))
28+
.map(|s| s.to_lowercase().replace("_", "-"))
29+
.collect();
2530

2631
Self {
2732
manifest_dir: PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()),
2833
out_dir: PathBuf::from(env::var("OUT_DIR").unwrap()),
2934
opt_level: env::var("OPT_LEVEL").unwrap().parse().unwrap(),
35+
cargo_features,
3036
target_arch: env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
3137
target_env: env::var("CARGO_CFG_TARGET_ENV").unwrap(),
3238
target_family: env::var("CARGO_CFG_TARGET_FAMILY").ok(),
@@ -45,6 +51,7 @@ pub fn emit_libm_config(cfg: &Config) {
4551
emit_arch_cfg();
4652
emit_optimization_cfg(cfg);
4753
emit_cfg_shorthands(cfg);
54+
emit_cfg_env(cfg);
4855
emit_f16_f128_cfg(cfg);
4956
}
5057

@@ -53,6 +60,7 @@ pub fn emit_libm_config(cfg: &Config) {
5360
pub fn emit_test_config(cfg: &Config) {
5461
emit_optimization_cfg(cfg);
5562
emit_cfg_shorthands(cfg);
63+
emit_cfg_env(cfg);
5664
emit_f16_f128_cfg(cfg);
5765
}
5866

@@ -97,6 +105,13 @@ fn emit_cfg_shorthands(cfg: &Config) {
97105
}
98106
}
99107

108+
/// Reemit config that we make use of for test logging.
109+
fn emit_cfg_env(cfg: &Config) {
110+
println!("cargo:rustc-env=CFG_CARGO_FEATURES={:?}", cfg.cargo_features);
111+
println!("cargo:rustc-env=CFG_OPT_LEVEL={}", cfg.opt_level);
112+
println!("cargo:rustc-env=CFG_TARGET_FEATURES={:?}", cfg.target_features);
113+
}
114+
100115
/// Configure whether or not `f16` and `f128` support should be enabled.
101116
fn emit_f16_f128_cfg(cfg: &Config) {
102117
println!("cargo:rustc-check-cfg=cfg(f16_enabled)");

crates/libm-test/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ mod precision;
1313
mod run_cfg;
1414
mod test_traits;
1515

16+
use std::env;
17+
use std::fs::File;
18+
use std::io::Write;
19+
use std::path::PathBuf;
20+
use std::sync::LazyLock;
21+
use std::time::SystemTime;
22+
1623
pub use f8_impl::f8;
1724
pub use libm::support::{Float, Int, IntTy, MinInt};
1825
pub use num::{FloatExt, logspace};
@@ -42,3 +49,49 @@ pub const fn ci() -> bool {
4249
Some(_) => true,
4350
}
4451
}
52+
53+
/// Print to stderr and additionally log it to `target/test-log.txt`. This is useful for saving
54+
/// output that would otherwise be consumed by the test harness.
55+
pub fn test_log(s: &str) {
56+
// Handle to a file opened in append mode, unless a suitable path can't be determined.
57+
static OUTFILE: LazyLock<Option<File>> = LazyLock::new(|| {
58+
// If the target directory is overridden, use that environment variable. Otherwise, save
59+
// at the default path `{workspace_root}/target`.
60+
let target_dir = match env::var("CARGO_TARGET_DIR") {
61+
Ok(s) => PathBuf::from(s),
62+
Err(_) => {
63+
let Ok(x) = env::var("CARGO_MANIFEST_DIR") else {
64+
return None;
65+
};
66+
67+
PathBuf::from(x).parent().unwrap().parent().unwrap().join("target")
68+
}
69+
};
70+
let outfile = target_dir.join("test-log.txt");
71+
72+
let mut f = File::options()
73+
.create(true)
74+
.append(true)
75+
.open(outfile)
76+
.expect("failed to open logfile");
77+
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
78+
79+
writeln!(f, "\n\nTest run at {}", now.as_secs()).unwrap();
80+
writeln!(f, "arch: {}", env::consts::ARCH).unwrap();
81+
writeln!(f, "os: {}", env::consts::OS).unwrap();
82+
writeln!(f, "bits: {}", usize::BITS).unwrap();
83+
writeln!(f, "emulated: {}", emulated()).unwrap();
84+
writeln!(f, "ci: {}", ci()).unwrap();
85+
writeln!(f, "cargo features: {}", env!("CFG_CARGO_FEATURES")).unwrap();
86+
writeln!(f, "opt level: {}", env!("CFG_OPT_LEVEL")).unwrap();
87+
writeln!(f, "target features: {}", env!("CFG_TARGET_FEATURES")).unwrap();
88+
89+
Some(f)
90+
});
91+
92+
eprintln!("{s}");
93+
94+
if let Some(mut f) = OUTFILE.as_ref() {
95+
writeln!(f, "{s}").unwrap();
96+
}
97+
}

0 commit comments

Comments
 (0)