mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
Add the `log` feature with `craft_testkit::log`, the shared form of the
`test_log.rs` helper in craft-libs `docx` and `wordmodel` (and WordCraft
`doc` and `formats`) and of SoundCraft's process-wide logger in
`clap-host/tests/editor_main_thread.rs`:
- `warnings(f)` and `records(level, f)` return the closure's result with the
records the calling thread logged while it ran.
- `capture_global(level)` returns a handle whose `drain()` takes the records
logged on every thread.
`log::set_logger` can be called once per process, so the first capture
fixes the mode for the test binary, and a call for the other mode returns
`ModeConflict`. Compared with the copied helper:
- An earlier foreign logger returns `LoggerAlreadySet` instead of an empty
capture, and the closure does not run.
- A capture raises the global maximum level and never lowers it.
- A nested capture on the same thread returns `NestedCapture` instead of
clearing the outer capture's records.
- A panic in the closure ends the capture on that thread.
Each capture keeps at most 4,096 records and 1 MiB of text and returns
`Overflow { dropped }` past either limit. Message formatting stops at the
byte limit. Only one `GlobalCapture` is alive at a time; dropping it ends
the capture, and a record checked against one capture is not kept by the
next. The module and its tests are native only.
142 lines
4.7 KiB
Rust
142 lines
4.7 KiB
Rust
//! Per-thread capture. Every test in this binary captures per thread, so the logger runs in that
|
|
//! mode for the whole process.
|
|
#![cfg(not(target_arch = "wasm32"))]
|
|
|
|
use std::fmt;
|
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
|
|
use craft_testkit::log::{CaptureError, CapturedRecord, MAX_BYTES, MAX_RECORDS, capture_global, records, warnings};
|
|
use log::{Level, LevelFilter};
|
|
|
|
#[test]
|
|
fn warnings_returns_the_result_and_the_warnings_and_errors() {
|
|
let (out, lines) = warnings(|| {
|
|
log::info!("not a warning");
|
|
log::warn!("kept {} of {}", 3, 4);
|
|
log::error!("an error");
|
|
log::debug!("not a warning either");
|
|
7
|
|
})
|
|
.expect("capture");
|
|
assert_eq!(out, 7);
|
|
assert_eq!(lines, ["kept 3 of 4", "an error"]);
|
|
}
|
|
|
|
#[test]
|
|
fn records_keep_level_and_target_at_the_requested_level() {
|
|
let ((), got) = records(LevelFilter::Info, || {
|
|
log::info!(target: "craft::test", "opened");
|
|
log::debug!("dropped");
|
|
log::warn!("short");
|
|
})
|
|
.expect("capture");
|
|
assert_eq!(
|
|
got,
|
|
[
|
|
CapturedRecord { level: Level::Info, target: "craft::test".into(), message: "opened".into() },
|
|
CapturedRecord { level: Level::Warn, target: module_path!().into(), message: "short".into() },
|
|
]
|
|
);
|
|
}
|
|
|
|
// No test in this binary raises the maximum level above `Info`, so the nested-capture test can
|
|
// check that a refused `Trace` capture leaves it below `Trace`.
|
|
|
|
#[test]
|
|
fn a_capture_does_not_lower_a_raised_maximum_level() {
|
|
log::set_max_level(LevelFilter::Info);
|
|
let ((), lines) = warnings(|| log::info!("below warn")).expect("capture");
|
|
assert!(lines.is_empty());
|
|
assert_eq!(log::max_level(), LevelFilter::Info);
|
|
}
|
|
|
|
#[test]
|
|
fn a_nested_capture_is_refused_and_the_outer_one_keeps_its_records() {
|
|
let ran = AtomicBool::new(false);
|
|
let (inner, lines) = warnings(|| {
|
|
log::warn!("before");
|
|
let inner = warnings(|| ran.store(true, Ordering::SeqCst));
|
|
let deeper = records(LevelFilter::Trace, || ran.store(true, Ordering::SeqCst));
|
|
log::warn!("after");
|
|
(inner.map(|_| ()), deeper.map(|_| ()))
|
|
})
|
|
.expect("outer capture");
|
|
assert_eq!(inner, (Err(CaptureError::NestedCapture), Err(CaptureError::NestedCapture)));
|
|
assert!(!ran.load(Ordering::SeqCst), "a nested closure must not run");
|
|
assert!(log::max_level() < LevelFilter::Trace, "a refused capture must not raise the maximum level");
|
|
assert_eq!(lines, ["before", "after"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_panic_ends_the_capture_and_the_next_one_starts_empty() {
|
|
let caught = catch_unwind(AssertUnwindSafe(|| {
|
|
warnings(|| {
|
|
log::warn!("left behind");
|
|
panic!("closure failed");
|
|
})
|
|
}));
|
|
assert!(caught.is_err());
|
|
let ((), lines) = warnings(|| log::warn!("next")).expect("a new capture after the panic");
|
|
assert_eq!(lines, ["next"]);
|
|
}
|
|
|
|
#[test]
|
|
fn records_from_other_threads_are_not_captured() {
|
|
let ((), lines) = warnings(|| {
|
|
std::thread::spawn(|| log::warn!("elsewhere")).join().expect("join");
|
|
log::warn!("here");
|
|
})
|
|
.expect("capture");
|
|
assert_eq!(lines, ["here"]);
|
|
}
|
|
|
|
#[test]
|
|
fn records_over_the_count_limit_are_reported() {
|
|
let got = warnings(|| {
|
|
for i in 0..=MAX_RECORDS {
|
|
log::warn!("{i}");
|
|
}
|
|
});
|
|
assert_eq!(got.map(|_| ()), Err(CaptureError::Overflow { dropped: 1 }));
|
|
let ((), lines) = warnings(|| log::warn!("fits")).expect("a new capture after an overflow");
|
|
assert_eq!(lines, ["fits"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_message_over_the_byte_limit_is_reported() {
|
|
let long = "x".repeat(MAX_BYTES);
|
|
let got = warnings(|| {
|
|
log::warn!("short");
|
|
log::warn!("{long}");
|
|
});
|
|
assert_eq!(got.map(|_| ()), Err(CaptureError::Overflow { dropped: 1 }));
|
|
}
|
|
|
|
/// Writes one byte at a time until a write fails, counting the writes that succeed.
|
|
struct Endless<'a>(&'a AtomicUsize);
|
|
|
|
impl fmt::Display for Endless<'_> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
for _ in 0..2 * MAX_BYTES {
|
|
f.write_str("x")?;
|
|
self.0.fetch_add(1, Ordering::SeqCst);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn formatting_a_message_stops_at_the_byte_limit() {
|
|
let written = AtomicUsize::new(0);
|
|
let got = warnings(|| log::warn!("{}", Endless(&written)));
|
|
assert_eq!(got.map(|_| ()), Err(CaptureError::Overflow { dropped: 1 }));
|
|
assert_eq!(written.load(Ordering::SeqCst), MAX_BYTES);
|
|
}
|
|
|
|
#[test]
|
|
fn a_global_capture_is_refused_in_a_per_thread_process() {
|
|
warnings(|| ()).expect("per-thread capture installs the logger");
|
|
assert_eq!(capture_global(LevelFilter::Warn).map(|_| ()), Err(CaptureError::ModeConflict));
|
|
}
|