Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventMatcher::with_message_regex #42

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tokio-bin-process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ itertools = "0.13.0"
once_cell = "1.17.1"
chrono = "0.4.24"
cargo_metadata = "0.18.0"
regex-lite = "0.1.6"

[dev-dependencies]
tokio = { version = "1.25.0", features = ["macros", "rt-multi-thread", "time"] }
tokio = { version = "1.25.0", features = ["macros", "rt-multi-thread", "time"] }
7 changes: 7 additions & 0 deletions tokio-bin-process/single-crate-test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tokio-bin-process/src/event_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt::Display;

use crate::event::{Event, Level};
use itertools::Itertools;
use regex_lite::Regex;

/// Use to check for any matching [`Event`]'s among a list of events.
#[derive(Debug)]
Expand Down Expand Up @@ -63,6 +64,7 @@ impl Display for Events {
pub struct EventMatcher {
level: Matcher<Level>,
message: Matcher<String>,
message_regex: RegexMatcher,
target: Matcher<String>,
pub(crate) count: Count,
}
Expand Down Expand Up @@ -91,6 +93,12 @@ impl EventMatcher {
self
}

/// Sets the matcher to only match an [`Event`] when it matches the provided regex pattern
pub fn with_message_regex(mut self, regex_pattern: &str) -> EventMatcher {
self.message_regex = RegexMatcher::new(regex_pattern);
self
}

/// Defines how many times the matcher must match to pass an assertion
///
/// This is not used internally i.e. it has no effect on [`EventMatcher::matches`]
Expand All @@ -104,6 +112,7 @@ impl EventMatcher {
pub fn matches(&self, event: &Event) -> bool {
self.level.matches(&event.level)
&& self.message.matches(&event.fields.message)
&& self.message_regex.matches(&event.fields.message)
&& self.target.matches(&event.target)
}
}
Expand Down Expand Up @@ -145,3 +154,25 @@ impl<T: PartialEq> Matcher<T> {
}
}
}

#[derive(Debug, Default)]
enum RegexMatcher {
Matches(Regex),
#[default]
Any,
}

impl RegexMatcher {
fn new(pattern: &str) -> Self {
RegexMatcher::Matches(regex_lite::Regex::new(pattern).unwrap())
}
}

impl RegexMatcher {
fn matches(&self, value: &str) -> bool {
match self {
RegexMatcher::Matches(regex) => regex.is_match(value),
RegexMatcher::Any => true,
}
}
}
41 changes: 41 additions & 0 deletions tokio-bin-process/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,47 @@ async fn test_cooldb_by_binary_name_bench_profile() {
cooldb.shutdown_and_then_consume_events(&[]).await;
}

#[tokio::test(flavor = "multi_thread")]
async fn test_message_regex_match() {
// Setup cooldb with custom profile
let mut cooldb = cooldb(Some("bench")).await;

// Assert that some functionality occured.
// Use a timeout to prevent the test hanging if no events occur.
timeout(Duration::from_secs(5), cooldb.consume_events(1, &[]))
.await
.unwrap()
.assert_contains(
&EventMatcher::new()
.with_level(Level::Info)
.with_message_regex("some .* occurs"),
);

// Shutdown cooldb asserting that it encountered no errors
cooldb.shutdown_and_then_consume_events(&[]).await;
}

#[should_panic]
#[tokio::test(flavor = "multi_thread")]
async fn test_message_regex_no_match() {
// Setup cooldb with custom profile
let mut cooldb = cooldb(Some("bench")).await;

// Assert that some functionality occured.
// Use a timeout to prevent the test hanging if no events occur.
timeout(Duration::from_secs(5), cooldb.consume_events(1, &[]))
.await
.unwrap()
.assert_contains(
&EventMatcher::new()
.with_level(Level::Info)
.with_message_regex("some .* does not occur"),
);

// Shutdown cooldb asserting that it encountered no errors
cooldb.shutdown_and_then_consume_events(&[]).await;
}

async fn cooldb(profile: Option<&'static str>) -> BinProcess {
let mut cooldb =
BinProcess::start_binary_name("cooldb", "cooldb", &["--log-format", "json"], profile).await;
Expand Down