Skip to content

Commit

Permalink
refactor(filter): Perform a distint redaction pass
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 23, 2024
1 parent 8d465cf commit f08fd7a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
50 changes: 45 additions & 5 deletions crates/snapbox/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ impl Filter for FilterNewlines {
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_json_string(&mut value, normalize_lines);
normalize_json_string(&mut value, &normalize_lines);
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_json_string(&mut value, normalize_lines);
normalize_json_string(&mut value, &normalize_lines);
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
Expand Down Expand Up @@ -82,13 +82,13 @@ impl Filter for FilterPaths {
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_json_string(&mut value, normalize_paths);
normalize_json_string(&mut value, &normalize_paths);
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_json_string(&mut value, normalize_paths);
normalize_json_string(&mut value, &normalize_paths);
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
Expand Down Expand Up @@ -119,8 +119,48 @@ fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item
data.map(|c| if c == '\\' { '/' } else { c })
}

struct NormalizeRedactions<'r> {
redactions: &'r Redactions,
}
impl Filter for NormalizeRedactions<'_> {
fn filter(&self, data: Data) -> Data {
let source = data.source;
let filters = data.filters;
let inner = match data.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let lines = self.redactions.redact(&text);
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_json_string(&mut value, &|s| self.redactions.redact(s));
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_json_string(&mut value, &|s| self.redactions.redact(s));
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = normalize_lines(&text);
DataInner::TermSvg(lines)
}
};
Data {
inner,
source,
filters,
}
}
}

#[cfg(feature = "structured-data")]
fn normalize_json_string(value: &mut serde_json::Value, op: fn(&str) -> String) {
fn normalize_json_string(value: &mut serde_json::Value, op: &dyn Fn(&str) -> String) {
match value {
serde_json::Value::String(str) => {
*str = op(str);
Expand Down
11 changes: 6 additions & 5 deletions crates/snapbox/src/filter/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Redactions;
use super::{Filter, NormalizeRedactions, Redactions};
use crate::data::DataInner;
use crate::Data;

Expand Down Expand Up @@ -43,6 +43,10 @@ impl<'a> NormalizeToExpected<'a> {
let Some(substitutions) = self.substitutions else {
return actual;
};
let actual = NormalizeRedactions {
redactions: substitutions,
}
.filter(actual);
normalize_data_to_redactions(actual, expected, substitutions)
}
}
Expand Down Expand Up @@ -160,7 +164,6 @@ fn normalize_value_to_redactions(
let has_key_wildcard =
exp.get(KEY_WILDCARD).and_then(|v| v.as_str()) == Some(VALUE_WILDCARD);
for (actual_key, mut actual_value) in std::mem::replace(act, serde_json::Map::new()) {
let actual_key = substitutions.redact(&actual_key);
if let Some(expected_value) = exp.get(&actual_key) {
normalize_value_to_redactions(&mut actual_value, expected_value, substitutions)
} else if has_key_wildcard {
Expand All @@ -181,11 +184,9 @@ fn normalize_str_to_redactions(input: &str, pattern: &str, redactions: &Redactio
return input.to_owned();
}

let input = redactions.redact(input);

let mut normalized: Vec<&str> = Vec::new();
let mut input_index = 0;
let input_lines: Vec<_> = crate::utils::LinesWithTerminator::new(&input).collect();
let input_lines: Vec<_> = crate::utils::LinesWithTerminator::new(input).collect();
let mut pattern_lines = crate::utils::LinesWithTerminator::new(pattern).peekable();
'outer: while let Some(pattern_line) = pattern_lines.next() {
if is_line_elide(pattern_line) {
Expand Down

0 comments on commit f08fd7a

Please sign in to comment.