Skip to content

Commit

Permalink
Fix config log matching
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Oct 10, 2023
1 parent fbc6b7d commit 6ee578d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5779,6 +5779,7 @@ dependencies = [
"humantime",
"indexmap 2.0.1",
"insta",
"itertools 0.11.0",
"lazy_static",
"once_cell",
"owo-colors",
Expand Down
1 change: 1 addition & 0 deletions zebra-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hex = "0.4.3"
indexmap = "2.0.1"
lazy_static = "1.4.0"
insta = "1.33.0"
itertools = "0.11.0"
proptest = "1.3.1"
once_cell = "1.18.0"
rand = "0.8.5"
Expand Down
27 changes: 14 additions & 13 deletions zebra-test/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ use tracing::instrument;

#[macro_use]
mod arguments;

pub mod to_regex;

pub use self::arguments::Arguments;
use self::to_regex::{CollectRegexSet, ToRegex, ToRegexSet};
use self::to_regex::{CollectRegexSet, ToRegexSet};

/// A super-trait for [`Iterator`] + [`Debug`].
pub trait IteratorDebug: Iterator + Debug {}
Expand Down Expand Up @@ -791,7 +792,7 @@ impl<T> TestChild<T> {
#[allow(clippy::unwrap_in_result)]
pub fn expect_stdout_line_matches<R>(&mut self, success_regex: R) -> Result<String>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
self.apply_failure_regexes_to_outputs();

Expand Down Expand Up @@ -823,7 +824,7 @@ impl<T> TestChild<T> {
#[allow(clippy::unwrap_in_result)]
pub fn expect_stderr_line_matches<R>(&mut self, success_regex: R) -> Result<String>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
self.apply_failure_regexes_to_outputs();

Expand Down Expand Up @@ -855,7 +856,7 @@ impl<T> TestChild<T> {
#[allow(clippy::unwrap_in_result)]
pub fn expect_stdout_line_matches_silent<R>(&mut self, success_regex: R) -> Result<String>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
self.apply_failure_regexes_to_outputs();

Expand Down Expand Up @@ -887,7 +888,7 @@ impl<T> TestChild<T> {
#[allow(clippy::unwrap_in_result)]
pub fn expect_stderr_line_matches_silent<R>(&mut self, success_regex: R) -> Result<String>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
self.apply_failure_regexes_to_outputs();

Expand Down Expand Up @@ -1246,9 +1247,9 @@ impl<T> TestOutput<T> {
#[allow(clippy::unwrap_in_result)]
pub fn stdout_matches<R>(&self, regex: R) -> Result<&Self>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
let re = regex.to_regex().expect("regex must be valid");
let re = regex.to_regex_set().expect("regex must be valid");

self.output_check(
|stdout| re.is_match(stdout),
Expand All @@ -1270,9 +1271,9 @@ impl<T> TestOutput<T> {
#[allow(clippy::unwrap_in_result)]
pub fn stdout_line_matches<R>(&self, regex: R) -> Result<&Self>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
let re = regex.to_regex().expect("regex must be valid");
let re = regex.to_regex_set().expect("regex must be valid");

self.any_output_line(
|line| re.is_match(line),
Expand Down Expand Up @@ -1300,9 +1301,9 @@ impl<T> TestOutput<T> {
#[allow(clippy::unwrap_in_result)]
pub fn stderr_matches<R>(&self, regex: R) -> Result<&Self>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
let re = regex.to_regex().expect("regex must be valid");
let re = regex.to_regex_set().expect("regex must be valid");

self.output_check(
|stderr| re.is_match(stderr),
Expand All @@ -1324,9 +1325,9 @@ impl<T> TestOutput<T> {
#[allow(clippy::unwrap_in_result)]
pub fn stderr_line_matches<R>(&self, regex: R) -> Result<&Self>
where
R: ToRegex + Debug,
R: ToRegexSet + Debug,
{
let re = regex.to_regex().expect("regex must be valid");
let re = regex.to_regex_set().expect("regex must be valid");

self.any_output_line(
|line| re.is_match(line),
Expand Down
14 changes: 10 additions & 4 deletions zebra-test/src/command/to_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::iter;

use itertools::Itertools;
use regex::{Error, Regex, RegexBuilder, RegexSet, RegexSetBuilder};

/// A trait for converting a value to a [`Regex`].
Expand Down Expand Up @@ -135,15 +136,20 @@ pub trait CollectRegexSet {
impl<I> CollectRegexSet for I
where
I: IntoIterator,
I::Item: ToRegex,
I::Item: ToRegexSet,
{
fn collect_regex_set(self) -> Result<RegexSet, Error> {
let regexes: Result<Vec<Regex>, Error> =
self.into_iter().map(|item| item.to_regex()).collect();
let regexes: Result<Vec<RegexSet>, Error> = self
.into_iter()
.map(|item| item.to_regex_set())
.try_collect();
let regexes = regexes?;

// This conversion discards flags and limits from Regex and RegexBuilder.
let regexes = regexes.iter().map(|regex| regex.as_str());
let regexes = regexes
.iter()
.map(|regex_set| regex_set.patterns())
.flatten();

Check failure on line 152 in zebra-test/src/command/to_regex.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable) Results

called `map(..).flatten()` on `Iterator`

error: called `map(..).flatten()` on `Iterator` --> zebra-test/src/command/to_regex.rs:151:14 | 151 | .map(|regex_set| regex_set.patterns()) | ______________^ 152 | | .flatten(); | |______________________^ help: try replacing `map` with `flat_map` and remove the `.flatten()`: `flat_map(|regex_set| regex_set.patterns())` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-D clippy::map-flatten` implied by `-D warnings`

RegexSet::new(regexes)
}
Expand Down
37 changes: 26 additions & 11 deletions zebrad/tests/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ use zebra_network::constants::PORT_IN_USE_ERROR;
use zebra_node_services::rpc_client::RpcRequestClient;
use zebra_state::{constants::LOCK_FILE_ERROR, database_format_version_in_code};

use zebra_test::{args, command::ContextFrom, net::random_known_port, prelude::*};
use zebra_test::{
args,
command::{to_regex::CollectRegexSet, ContextFrom},
net::random_known_port,
prelude::*,
};

mod common;

Expand Down Expand Up @@ -925,17 +930,27 @@ fn stored_configs_work() -> Result<()> {
let mut child =
run_dir.spawn_child(args!["-c", stored_config_path.to_str().unwrap(), "start"])?;

let success_regexes = [
// When logs are sent to the terminal, we see the config loading message and path.
format!(
"loaded zebra config.*config_path.*=.*{}",
regex::escape(
stored_config_path
.file_name()
.expect("config path must have a file name")
.to_str()
.expect("config paths are valid unicode")
)
),
// If they are sent to a file, we just see a log file message.
"Sending logs to".to_string(),
]
.iter()
.collect_regex_set()
.expect("regexes are valid");

// Zebra was able to start with the stored config.
// When logs are sent to the terminal, we see the config loading message and path.
// If they are sent to a file, we just see a log file message.
child.expect_stdout_line_matches(format!(
"(loaded zebra config.*config_path.*=.*{})|(Sending logs to)",
regex::escape(
stored_config_path
.to_str()
.expect("config paths are valid unicode")
)
))?;
child.expect_stdout_line_matches(success_regexes)?;

// finish
child.kill(false)?;
Expand Down

0 comments on commit 6ee578d

Please sign in to comment.