From e33f57601f40a2d5b94e4aa9ee14b85bb71baecf Mon Sep 17 00:00:00 2001 From: Rain Date: Wed, 3 Apr 2024 00:17:37 -0700 Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?= =?UTF-8?q?l=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.6-beta.1 --- test-utils/src/dev/test_cmds.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/test-utils/src/dev/test_cmds.rs b/test-utils/src/dev/test_cmds.rs index 89bf022369..6500eaddfd 100644 --- a/test-utils/src/dev/test_cmds.rs +++ b/test-utils/src/dev/test_cmds.rs @@ -126,11 +126,20 @@ pub fn error_for_enoent() -> String { /// /// This allows use to use expectorate to verify the shape of the CLI output. pub fn redact_variable(input: &str, extra_redactions: &[&str]) -> String { + // Perform extra redactions at the beginning, not the end. This is because + // some of the built-in redactions below might match a substring of + // something that should be handled by extra_redactions (e.g. a temporary + // path). + let mut s = input.to_owned(); + for r in extra_redactions { + s = s.replace(r, ""); + } + // Replace TCP port numbers. We include the localhost characters to avoid // catching any random sequence of numbers. let s = regex::Regex::new(r"\[::1\]:\d{4,5}") .unwrap() - .replace_all(input, "[::1]:REDACTED_PORT") + .replace_all(&s, "[::1]:REDACTED_PORT") .to_string(); let s = regex::Regex::new(r"\[::ffff:127.0.0.1\]:\d{4,5}") .unwrap() @@ -173,7 +182,7 @@ pub fn redact_variable(input: &str, extra_redactions: &[&str]) -> String { .replace_all(&s, "ms") .to_string(); - let mut s = regex::Regex::new( + let s = regex::Regex::new( r"note: database schema version matches expected \(\d+\.\d+\.\d+\)", ) .unwrap() @@ -184,9 +193,18 @@ pub fn redact_variable(input: &str, extra_redactions: &[&str]) -> String { ) .to_string(); - for r in extra_redactions { - s = s.replace(r, ""); - } - s } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_redact_variable() { + // Ens + let input = "time: 123ms, path: /var/tmp/tmp.456ms123s"; + let actual = redact_variable(input, &["/var/tmp/tmp.456ms123s"]); + assert_eq!(actual, "time: ms, path: "); + } +}