Skip to content

Commit 8f543eb

Browse files
committed
🐛 fix deleted missing tests
1 parent 93d4c2f commit 8f543eb

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

src/logger.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,72 @@ pub fn debug(message: &str) {
120120

121121
#[cfg(test)]
122122
mod tests {
123-
// ... your tests ...
123+
use super::*;
124+
use std::io;
125+
use std::sync::{Arc, Mutex};
126+
use tracing_subscriber::{filter::LevelFilter, fmt, layer::SubscriberExt, registry};
127+
128+
#[derive(Clone)]
129+
struct TestWriter {
130+
buf: Arc<Mutex<Vec<u8>>>,
131+
}
132+
133+
impl TestWriter {
134+
fn new() -> Self {
135+
Self {
136+
buf: Arc::new(Mutex::new(Vec::new())),
137+
}
138+
}
139+
140+
fn get_contents(&self) -> String {
141+
let mut buf = self.buf.lock().unwrap();
142+
let output = String::from_utf8(buf.clone()).expect("Logs should be valid UTF-8");
143+
buf.clear();
144+
output
145+
}
146+
}
147+
148+
#[cfg_attr(coverage, coverage(off))]
149+
impl io::Write for TestWriter {
150+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
151+
self.buf.lock().unwrap().write(buf)
152+
}
153+
154+
fn flush(&mut self) -> io::Result<()> {
155+
self.buf.lock().unwrap().flush()
156+
}
157+
}
158+
159+
#[test]
160+
fn it_logs_all_levels() {
161+
let writer = TestWriter::new();
162+
let writer_clone = writer.clone();
163+
164+
// Build a subscriber that captures all log levels for this test
165+
let subscriber = registry()
166+
.with(
167+
fmt::layer()
168+
.with_writer(move || writer_clone.clone())
169+
.with_ansi(false),
170+
)
171+
// Explicitly set the filter to capture all levels down to TRACE
172+
.with(LevelFilter::TRACE);
173+
174+
// Run the test code with our temporary subscriber
175+
tracing::subscriber::with_default(subscriber, || {
176+
log("hello world");
177+
info("this is info");
178+
warn("a warning message");
179+
error("an error message");
180+
debug("a debug message");
181+
});
182+
183+
let output = writer.get_contents();
184+
185+
assert!(output.contains("INFO") && output.contains("hello world"));
186+
assert!(output.contains("INFO") && output.contains("this is info"));
187+
assert!(output.contains("WARN") && output.contains("a warning message"));
188+
assert!(output.contains("ERROR") && output.contains("an error message"));
189+
assert!(output.contains("DEBUG") && output.contains("a debug message"));
190+
}
124191
}

0 commit comments

Comments
 (0)