From 6a9a6b2f4b42a7ddb70c2e6918f1c0919863d2ac Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 6 Nov 2024 20:09:40 -0600 Subject: [PATCH] feat(redact): Expose clear_unused --- crates/snapbox/src/filter/pattern.rs | 2 +- crates/snapbox/src/filter/redactions.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/snapbox/src/filter/pattern.rs b/crates/snapbox/src/filter/pattern.rs index fc4de65b..a6200914 100644 --- a/crates/snapbox/src/filter/pattern.rs +++ b/crates/snapbox/src/filter/pattern.rs @@ -585,7 +585,7 @@ fn line_matches(mut actual: &str, expected: &str, redactions: &Redactions) -> bo return true; } - let expected = redactions.clear(expected); + let expected = redactions.clear_unused(expected); let mut sections = expected.split("[..]").peekable(); while let Some(section) = sections.next() { if let Some(remainder) = actual.strip_prefix(section) { diff --git a/crates/snapbox/src/filter/redactions.rs b/crates/snapbox/src/filter/redactions.rs index 6585c4db..6eed6d1b 100644 --- a/crates/snapbox/src/filter/redactions.rs +++ b/crates/snapbox/src/filter/redactions.rs @@ -133,7 +133,12 @@ impl Redactions { input } - pub(crate) fn clear<'v>(&self, pattern: &'v str) -> Cow<'v, str> { + /// Clear unused redactions from expected data + /// + /// Some redactions can be conditionally present, like redacting [`std::env::consts::EXE_SUFFIX`]. + /// When the redaction is not present, it needs to be removed from the expected data so it can + /// be matched against the actual data. + pub fn clear_unused<'v>(&self, pattern: &'v str) -> Cow<'v, str> { if !self.unused.as_ref().map(|s| s.is_empty()).unwrap_or(false) && pattern.contains('[') { let mut pattern = pattern.to_owned(); replace_many(