Skip to content

Commit eaafb1f

Browse files
authored
Add EventMatcher::with_message_regex (#42)
1 parent b78abde commit eaafb1f

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tokio-bin-process/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ itertools = "0.13.0"
2121
once_cell = "1.17.1"
2222
chrono = "0.4.24"
2323
cargo_metadata = "0.18.0"
24+
regex-lite = "0.1.6"
2425

2526
[dev-dependencies]
26-
tokio = { version = "1.25.0", features = ["macros", "rt-multi-thread", "time"] }
27+
tokio = { version = "1.25.0", features = ["macros", "rt-multi-thread", "time"] }

tokio-bin-process/single-crate-test/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tokio-bin-process/src/event_matcher.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt::Display;
44

55
use crate::event::{Event, Level};
66
use itertools::Itertools;
7+
use regex_lite::Regex;
78

89
/// Use to check for any matching [`Event`]'s among a list of events.
910
#[derive(Debug)]
@@ -63,6 +64,7 @@ impl Display for Events {
6364
pub struct EventMatcher {
6465
level: Matcher<Level>,
6566
message: Matcher<String>,
67+
message_regex: RegexMatcher,
6668
target: Matcher<String>,
6769
pub(crate) count: Count,
6870
}
@@ -91,6 +93,12 @@ impl EventMatcher {
9193
self
9294
}
9395

96+
/// Sets the matcher to only match an [`Event`] when it matches the provided regex pattern
97+
pub fn with_message_regex(mut self, regex_pattern: &str) -> EventMatcher {
98+
self.message_regex = RegexMatcher::new(regex_pattern);
99+
self
100+
}
101+
94102
/// Defines how many times the matcher must match to pass an assertion
95103
///
96104
/// This is not used internally i.e. it has no effect on [`EventMatcher::matches`]
@@ -104,6 +112,7 @@ impl EventMatcher {
104112
pub fn matches(&self, event: &Event) -> bool {
105113
self.level.matches(&event.level)
106114
&& self.message.matches(&event.fields.message)
115+
&& self.message_regex.matches(&event.fields.message)
107116
&& self.target.matches(&event.target)
108117
}
109118
}
@@ -145,3 +154,25 @@ impl<T: PartialEq> Matcher<T> {
145154
}
146155
}
147156
}
157+
158+
#[derive(Debug, Default)]
159+
enum RegexMatcher {
160+
Matches(Regex),
161+
#[default]
162+
Any,
163+
}
164+
165+
impl RegexMatcher {
166+
fn new(pattern: &str) -> Self {
167+
RegexMatcher::Matches(regex_lite::Regex::new(pattern).unwrap())
168+
}
169+
}
170+
171+
impl RegexMatcher {
172+
fn matches(&self, value: &str) -> bool {
173+
match self {
174+
RegexMatcher::Matches(regex) => regex.is_match(value),
175+
RegexMatcher::Any => true,
176+
}
177+
}
178+
}

tokio-bin-process/tests/test.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,47 @@ async fn test_cooldb_by_binary_name_bench_profile() {
4444
cooldb.shutdown_and_then_consume_events(&[]).await;
4545
}
4646

47+
#[tokio::test(flavor = "multi_thread")]
48+
async fn test_message_regex_match() {
49+
// Setup cooldb with custom profile
50+
let mut cooldb = cooldb(Some("bench")).await;
51+
52+
// Assert that some functionality occured.
53+
// Use a timeout to prevent the test hanging if no events occur.
54+
timeout(Duration::from_secs(5), cooldb.consume_events(1, &[]))
55+
.await
56+
.unwrap()
57+
.assert_contains(
58+
&EventMatcher::new()
59+
.with_level(Level::Info)
60+
.with_message_regex("some .* occurs"),
61+
);
62+
63+
// Shutdown cooldb asserting that it encountered no errors
64+
cooldb.shutdown_and_then_consume_events(&[]).await;
65+
}
66+
67+
#[should_panic]
68+
#[tokio::test(flavor = "multi_thread")]
69+
async fn test_message_regex_no_match() {
70+
// Setup cooldb with custom profile
71+
let mut cooldb = cooldb(Some("bench")).await;
72+
73+
// Assert that some functionality occured.
74+
// Use a timeout to prevent the test hanging if no events occur.
75+
timeout(Duration::from_secs(5), cooldb.consume_events(1, &[]))
76+
.await
77+
.unwrap()
78+
.assert_contains(
79+
&EventMatcher::new()
80+
.with_level(Level::Info)
81+
.with_message_regex("some .* does not occur"),
82+
);
83+
84+
// Shutdown cooldb asserting that it encountered no errors
85+
cooldb.shutdown_and_then_consume_events(&[]).await;
86+
}
87+
4788
async fn cooldb(profile: Option<&'static str>) -> BinProcess {
4889
let mut cooldb =
4990
BinProcess::start_binary_name("cooldb", "cooldb", &["--log-format", "json"], profile).await;

0 commit comments

Comments
 (0)