Skip to content

Commit

Permalink
feat: Enable redacting timestamps from insta snapshots (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski authored Dec 23, 2024
1 parent 2f5bdf9 commit 002d4bc
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/testing/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const BEARER_TOKEN_REGEX: &str = r"Bearer [\w\.-]+";
const POSTGRES_URI_REGEX: &str = r"postgres://(\w|\d|@|:|\/|\.)+";
const REDIS_URI_REGEX: &str = r"redis://(\w|\d|@|:|\/|\.)+";
const SMTP_URI_REGEX: &str = r"smtp://(\w|\d|@|:|\/|\.)+";
// https://stackoverflow.com/questions/3143070/regex-to-match-an-iso-8601-datetime-string
const TIMESTAMP_REGEX: &str = r"(\d{4}-[01]\d-[0-3]\d\s?T?\s?[0-2]\d:[0-5]\d:[0-5]\d\.\d+\s?([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\d\s?T?\s?[0-2]\d:[0-5]\d:[0-5]\d\s?([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\d\s?T?\s?[0-2]\d:[0-5]\d\s?([+-][0-2]\d:[0-5]\d|Z))";

/// Configure which settings to apply on the snapshot [Settings].
///
Expand Down Expand Up @@ -91,24 +93,29 @@ pub struct TestCaseConfig {
#[builder(default = true)]
pub redact_auth_tokens: bool,

/// Whether to Postgres URIs from snapshots. This is useful for tests involving
/// Whether to redact Postgres URIs from snapshots. This is useful for tests involving
/// dynamically created Postgres instances that will be different on every test run, or involve
/// real Postgres instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_postgres_uri: bool,

/// Whether to Redis URIs from snapshots. This is useful for tests involving
/// Whether to redact Redis URIs from snapshots. This is useful for tests involving
/// dynamically created Redis instances that will be different on every test run, or involve
/// real Redis instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_redis_uri: bool,

/// Whether to SMTP URIs from snapshots. This is useful for tests involving
/// Whether to redact SMTP URIs from snapshots. This is useful for tests involving
/// dynamically created SMTP instances that will be different on every test run, or involve
/// real SMTP instances that you don't want leaked in your source code.
#[builder(default = true)]
pub redact_smtp_uri: bool,

/// Whether to redact timestamps. This is useful for tests involving
/// dynamically created timestamps that will be different on every test run.
#[builder(default = true)]
pub redact_timestamp: bool,

/// Whether to automatically bind the [Settings] to the current scope. If `true`, the settings
/// will be automatically applied for the test in which the [TestCase] was built. If `false`,
/// the settings will only be applied after manually calling [Settings::bind_to_scope], or
Expand Down Expand Up @@ -206,6 +213,9 @@ impl From<TestCaseConfig> for TestCase {
if value.redact_smtp_uri {
snapshot_redact_smtp_uri(&mut settings);
}
if value.redact_timestamp {
snapshot_redact_timestamp(&mut settings);
}

let _settings_guard = if value.bind_scope {
Some(settings.bind_to_scope())
Expand Down Expand Up @@ -265,6 +275,13 @@ pub fn snapshot_redact_smtp_uri(settings: &mut Settings) -> &mut Settings {
settings
}

/// Redact instances of timestamps in snapshots. Applies a filter on the [Settings] to replace
/// sub-strings matching [`TIMESTAMP_REGEX`] with `[timestamp]`.
pub fn snapshot_redact_timestamp(settings: &mut Settings) -> &mut Settings {
settings.add_filter(TIMESTAMP_REGEX, "[timestamp]");
settings
}

/// Extract the last segment of the current thread name to use as the test case description.
///
/// See: <https://github.com/adriangb/pgpq/blob/b0b0f8c77c862c0483d81571e76f3a2b746136fc/pgpq/src/lib.rs#L649-L669>
Expand Down

0 comments on commit 002d4bc

Please sign in to comment.