Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable redacting timestamps from insta snapshots #532

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading