Skip to content

Commit 15f18a2

Browse files
committed
refactor(common): add define_test_logger macro for unified TestLogger creation
Also move this new and the existing test logging utilities to a `test::logging` module.
1 parent d58a9f3 commit 15f18a2

File tree

6 files changed

+62
-34
lines changed

6 files changed

+62
-34
lines changed

mithril-aggregator/src/services/aggregator_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ mod tests {
813813
mod warn_if_api_version_mismatch {
814814
use std::collections::HashMap;
815815

816-
use mithril_common::test::MemoryDrainForTestInspector;
816+
use mithril_common::test::logging::MemoryDrainForTestInspector;
817817

818818
use super::*;
819819

mithril-client/src/aggregator_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ mod tests {
877877
mod warn_if_api_version_mismatch {
878878
use http::response::Builder as HttpResponseBuilder;
879879

880-
use mithril_common::test::MemoryDrainForTestInspector;
880+
use mithril_common::test::logging::MemoryDrainForTestInspector;
881881

882882
use super::*;
883883

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Logging utilities for tests
2+
//!
3+
mod memory_logger;
4+
5+
pub use memory_logger::*;
6+
7+
/// Creates a test-specific `TestLogger` struct that can creates preconfigured logger instances.
8+
///
9+
/// This macro avoids direct `slog_async` and `slog_term` dependencies in library crates
10+
/// by letting dependents add them as `dev-dependencies` only.
11+
///
12+
/// ## Methods
13+
///
14+
/// - `TestLogger::stdout()` - Logger that outputs to stdout.
15+
/// - `TestLogger::memory()` - Logger that stores messages in memory for inspection.
16+
/// Returns `(Logger, MemoryDrainForTestInspector)` tuple
17+
///
18+
/// Requires: `slog`, `slog_async`, `slog_term`
19+
#[macro_export]
20+
macro_rules! define_test_logger {
21+
() => {
22+
#[cfg(test)]
23+
pub(crate) struct TestLogger;
24+
25+
#[cfg(test)]
26+
mod test_logger_impl {
27+
use std::io;
28+
use std::sync::Arc;
29+
30+
use slog::{Drain, Logger};
31+
use slog_async::Async;
32+
use slog_term::{CompactFormat, PlainDecorator};
33+
34+
use $crate::test::logging::{MemoryDrainForTest, MemoryDrainForTestInspector};
35+
36+
impl super::TestLogger {
37+
fn from_writer<W: io::Write + Send + 'static>(writer: W) -> Logger {
38+
let decorator = PlainDecorator::new(writer);
39+
let drain = CompactFormat::new(decorator).build().fuse();
40+
let drain = Async::new(drain).build().fuse();
41+
Logger::root(Arc::new(drain), slog::o!())
42+
}
43+
44+
pub(crate) fn stdout() -> Logger {
45+
Self::from_writer(slog_term::TestStdoutWriter)
46+
}
47+
48+
pub(crate) fn memory() -> (Logger, MemoryDrainForTestInspector) {
49+
let (drain, inspector) = MemoryDrainForTest::new();
50+
(Logger::root(drain.fuse(), slog::o!()), inspector)
51+
}
52+
}
53+
}
54+
};
55+
}
56+
pub use define_test_logger;

mithril-common/src/test/mod.rs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
99
pub mod builder;
1010
pub mod double;
11+
pub mod logging;
1112
pub mod mock_extensions;
1213

1314
mod assert;
14-
mod memory_logger;
1515
mod temp_dir;
1616

1717
pub use assert::*;
18-
pub use memory_logger::*;
1918
pub use temp_dir::*;
20-
#[cfg(test)]
21-
pub(crate) use utils::*;
19+
20+
logging::define_test_logger!();
2221

2322
/// Return the path of the given function.
2423
/// If the last function is `f`, it is removed.
@@ -66,37 +65,10 @@ pub use current_function_path;
6665

6766
#[cfg(test)]
6867
mod utils {
69-
use std::io;
7068
use std::path::Path;
71-
use std::sync::Arc;
72-
73-
use slog::{Drain, Logger};
74-
use slog_async::Async;
75-
use slog_term::{CompactFormat, PlainDecorator};
7669

7770
use super::*;
7871

79-
pub struct TestLogger;
80-
81-
#[cfg(test)]
82-
impl TestLogger {
83-
fn from_writer<W: io::Write + Send + 'static>(writer: W) -> Logger {
84-
let decorator = PlainDecorator::new(writer);
85-
let drain = CompactFormat::new(decorator).build().fuse();
86-
let drain = Async::new(drain).build().fuse();
87-
Logger::root(Arc::new(drain), slog::o!())
88-
}
89-
90-
pub fn stdout() -> Logger {
91-
Self::from_writer(slog_term::TestStdoutWriter)
92-
}
93-
94-
pub fn memory() -> (Logger, MemoryDrainForTestInspector) {
95-
let (drain, inspector) = MemoryDrainForTest::new();
96-
(Logger::root(drain.fuse(), slog::o!()), inspector)
97-
}
98-
}
99-
10072
#[test]
10173
fn test_current_function_extract_function_name() {
10274
let name = current_function!();

mithril-signer/src/services/aggregator_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ mod tests {
474474
use mithril_common::entities::Epoch;
475475
use mithril_common::messages::TryFromMessageAdapter;
476476
use mithril_common::test::{
477-
MemoryDrainForTestInspector,
478477
double::{Dummy, fake_data},
478+
logging::MemoryDrainForTestInspector,
479479
};
480480

481481
use crate::test_tools::TestLogger;

0 commit comments

Comments
 (0)