Lines
86.67 %
Functions
71.43 %
Branches
100 %
use std::io::{self, Write};
use std::process::Command;
use std::sync::{Arc, Mutex};
use server::telemetry::{init_logging, init_with_writer, resolve_filter};
use tracing::level_filters::LevelFilter;
const CHILD_ENV: &str = "NOMISYNC_LOGGING_TEST_CHILD";
const MARKER: &str = "marker-from-log-record";
#[derive(Clone, Default)]
struct Capture(Arc<Mutex<Vec<u8>>>);
impl Write for Capture {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0
.lock()
.map_err(|_| io::Error::other("capture poisoned"))?
.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
#[test]
fn log_record_reaches_the_subscriber() -> Result<(), Box<dyn std::error::Error>> {
let capture = Capture::default();
let sink = capture.clone();
init_with_writer(
resolve_filter(Some(LevelFilter::INFO), None, LevelFilter::WARN),
move || sink.clone(),
false,
)?;
log::info!("{MARKER}");
let captured = String::from_utf8(capture.0.lock().map_err(|_| "poisoned")?.clone())?;
assert!(captured.contains(MARKER), "captured: {captured:?}");
fn child_logs_through_init_logging() -> Result<(), Box<dyn std::error::Error>> {
if std::env::var_os(CHILD_ENV).is_none() {
return Ok(());
init_logging(Some(LevelFilter::INFO), LevelFilter::WARN)?;
fn logging_writes_stderr_and_keeps_stdout_clean() -> Result<(), Box<dyn std::error::Error>> {
let out = Command::new(std::env::current_exe()?)
.args(["--exact", "child_logs_through_init_logging", "--nocapture"])
.env(CHILD_ENV, "1")
.env("RUST_LOG", "off")
.output()?;
let stderr = String::from_utf8(out.stderr)?;
assert!(out.status.success());
assert!(!String::from_utf8(out.stdout)?.contains(MARKER));
assert!(stderr.contains(MARKER));
assert!(
!stderr.contains('\u{1b}'),
"a piped stderr must carry no colour codes: {stderr:?}"
);