Documentation
¶
Index ¶
- func AcceptAll() error
- func RejectAll() error
- func Review() error
- func Snap(t snapshots.T, title string, value any, opts ...Option)
- func SnapJSON(t snapshots.T, title string, jsonStr string, opts ...Option)
- func SnapMany(t snapshots.T, title string, values []any, opts ...Option)
- func SnapString(t snapshots.T, title string, content string, opts ...Option)
- type IgnorePattern
- func IgnoreEmpty() IgnorePattern
- func IgnoreKey(keys ...string) IgnorePattern
- func IgnoreKeyMatching(pattern string) IgnorePattern
- func IgnoreKeyPattern(keyPattern, valuePattern string) IgnorePattern
- func IgnoreKeyValue(key, value string) IgnorePattern
- func IgnoreNull() IgnorePattern
- func IgnoreSensitive() IgnorePattern
- func IgnoreValue(values ...string) IgnorePattern
- func IgnoreWith(ignoreFunc func(key, value string) bool) IgnorePattern
- type Option
- type Scrubber
- func ScrubAPIKey() Scrubber
- func ScrubCreditCard() Scrubber
- func ScrubDate() Scrubber
- func ScrubEmail() Scrubber
- func ScrubExact(match string, replacement string) Scrubber
- func ScrubIP() Scrubber
- func ScrubJWT() Scrubber
- func ScrubRegex(pattern string, replacement string) Scrubber
- func ScrubTimestamp() Scrubber
- func ScrubUUID() Scrubber
- func ScrubUnixTimestamp() Scrubber
- func ScrubWith(scrubFunc func(string) string) Scrubber
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AcceptAll ¶
func AcceptAll() error
AcceptAll accepts all pending snapshot changes without review.
func RejectAll ¶
func RejectAll() error
RejectAll rejects all pending snapshot changes without review.
func Review ¶
func Review() error
Review launches an interactive review session to accept or reject snapshot changes.
func Snap ¶
Snap takes a single value, formats it, and creates a snapshot with the given title. Complex types are formatted using a pretty-printer for readability.
Options can be provided to scrub sensitive or dynamic data before snapshotting. Only Scrubber options are supported; IgnorePattern options will cause an error.
Example:
user := User{ID: "123", Email: "user@example.com"}
shutter.Snap(t, "user data", user,
shutter.ScrubUUID(),
shutter.ScrubEmail(),
)
func SnapJSON ¶
SnapJSON takes a JSON string, validates it, and pretty-prints it with consistent formatting before snapshotting. This preserves the raw JSON format while ensuring valid JSON structure.
Options can be provided to apply both Scrubbers and IgnorePatterns. IgnorePatterns remove fields from the JSON structure before scrubbing. Scrubbers then transform the remaining content.
Example:
jsonStr := `{"id": "550e8400-...", "email": "user@example.com", "password": "secret"}`
shutter.SnapJSON(t, "user response", jsonStr,
shutter.IgnoreKey("password"), // First: remove password field
shutter.ScrubUUID(), // Second: scrub remaining UUIDs
shutter.ScrubEmail(), // Third: scrub emails
)
func SnapMany ¶
SnapMany takes multiple values, formats them, and creates a snapshot with the given title. This is useful when you want to snapshot multiple related values together.
Options can be provided to scrub sensitive or dynamic data before snapshotting. Only Scrubber options are supported; IgnorePattern options will cause an error.
Example:
shutter.SnapMany(t, "request and response",
[]any{request, response},
shutter.ScrubUUID(),
shutter.ScrubTimestamp(),
)
func SnapString ¶
SnapString takes a string value and creates a snapshot with the given title. This is useful for snapshotting generated text, logs, or other string content.
Options can be provided to scrub sensitive or dynamic data before snapshotting. Only Scrubber options are supported; IgnorePattern options will cause an error.
Example:
output := generateReport()
shutter.SnapString(t, "report output", output,
shutter.ScrubTimestamp(),
)
Types ¶
type IgnorePattern ¶
IgnorePattern determines whether a key-value pair should be excluded from JSON snapshots. This is useful for removing fields that change frequently or contain sensitive data.
IgnorePatterns only work with SnapJSON. Using them with Snap or SnapString will result in an error.
func IgnoreEmpty ¶
func IgnoreEmpty() IgnorePattern
IgnoreEmpty ignores fields with empty string values.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreEmpty(),
)
func IgnoreKey ¶
func IgnoreKey(keys ...string) IgnorePattern
IgnoreKey creates an ignore pattern that ignores the specified keys regardless of their values.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreKey("password", "secret", "token"),
)
func IgnoreKeyMatching ¶
func IgnoreKeyMatching(pattern string) IgnorePattern
IgnoreKeyMatching creates an ignore pattern that ignores keys matching the given regex pattern.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreKeyMatching(`^user_`),
)
func IgnoreKeyPattern ¶
func IgnoreKeyPattern(keyPattern, valuePattern string) IgnorePattern
IgnoreKeyPattern creates an ignore pattern using regex patterns for keys and values. Pass empty string for keyPattern or valuePattern to match any key or value.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreKeyPattern(`.*password.*`, ""),
shutter.IgnoreKeyPattern(`.*token.*`, ""),
)
func IgnoreKeyValue ¶
func IgnoreKeyValue(key, value string) IgnorePattern
IgnoreKeyValue creates an ignore pattern that matches exact key-value pairs. Use "*" as the value to ignore any value for the given key.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreKeyValue("password", "*"),
shutter.IgnoreKeyValue("status", "pending"),
)
func IgnoreNull ¶
func IgnoreNull() IgnorePattern
IgnoreNull ignores fields with null/nil values (represented as "null" in JSON).
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreNull(),
)
func IgnoreSensitive ¶
func IgnoreSensitive() IgnorePattern
IgnoreSensitive ignores common sensitive key names like password, token, etc.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreSensitive(),
)
func IgnoreValue ¶
func IgnoreValue(values ...string) IgnorePattern
IgnoreValue creates an ignore pattern that ignores the specified values regardless of their keys.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreValue("pending", "processing"),
)
func IgnoreWith ¶
func IgnoreWith(ignoreFunc func(key, value string) bool) IgnorePattern
IgnoreWith creates an ignore pattern using a custom function. The function receives the key and value and should return true if the key-value pair should be ignored.
This option only works with SnapJSON.
Example:
shutter.SnapJSON(t, "response", jsonStr,
shutter.IgnoreWith(func(key, value string) bool {
return strings.HasPrefix(key, "temp_")
}),
)
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a marker interface for all snapshot options. This allows compile-time type safety while supporting different option types.
type Scrubber ¶
Scrubber transforms content before snapshotting, typically to replace dynamic or sensitive data with stable placeholders.
Scrubbers are applied in the order they are provided. Later scrubbers can transform the output of earlier scrubbers.
Example:
shutter.Snap(t, "user data", user,
shutter.ScrubUUID(), // First: UUIDs -> <UUID>
shutter.ScrubEmail(), // Second: emails -> <EMAIL>
)
func ScrubAPIKey ¶
func ScrubAPIKey() Scrubber
ScrubAPIKey replaces common API key patterns with "<API_KEY>". Matches patterns like: sk_live_..., pk_test_..., api_key_...
Example:
shutter.Snap(t, "config", config, shutter.ScrubAPIKey())
func ScrubCreditCard ¶
func ScrubCreditCard() Scrubber
ScrubCreditCard replaces credit card numbers with "<CREDIT_CARD>".
Example:
shutter.Snap(t, "payment", payment, shutter.ScrubCreditCard())
func ScrubDate ¶
func ScrubDate() Scrubber
ScrubDate replaces various date formats with "<DATE>".
Example:
shutter.Snap(t, "data", data, shutter.ScrubDate())
func ScrubEmail ¶
func ScrubEmail() Scrubber
ScrubEmail replaces email addresses with "<EMAIL>".
Example:
shutter.Snap(t, "user", user, shutter.ScrubEmail())
func ScrubExact ¶
ScrubExact creates a scrubber that replaces exact string matches.
Example:
shutter.ScrubExact("secret_value", "<REDACTED>")
func ScrubIP ¶
func ScrubIP() Scrubber
ScrubIP replaces IPv4 addresses with "<IP>".
Example:
shutter.Snap(t, "request", request, shutter.ScrubIP())
func ScrubJWT ¶
func ScrubJWT() Scrubber
ScrubJWT replaces JWT tokens with "<JWT>".
Example:
shutter.Snap(t, "auth", authData, shutter.ScrubJWT())
func ScrubRegex ¶
ScrubRegex creates a scrubber that replaces all matches of the given regex pattern with the replacement string.
Example:
shutter.ScrubRegex(`user-\d+`, "<USER_ID>")
func ScrubTimestamp ¶
func ScrubTimestamp() Scrubber
ScrubTimestamp replaces ISO8601 timestamps with "<TIMESTAMP>".
Example:
shutter.Snap(t, "event", event, shutter.ScrubTimestamp())
func ScrubUUID ¶
func ScrubUUID() Scrubber
ScrubUUID replaces all UUIDs with "<UUID>".
Example:
shutter.Snap(t, "user", user, shutter.ScrubUUID())
func ScrubUnixTimestamp ¶
func ScrubUnixTimestamp() Scrubber
ScrubUnixTimestamp replaces Unix timestamps (10-13 digits) with "<UNIX_TS>". Note: This is aggressive and may match other long numbers. For more conservative scrubbing with context keywords, use ScrubRegex with a custom pattern.
Example:
shutter.Snap(t, "data", data, shutter.ScrubUnixTimestamp())
func ScrubWith ¶
ScrubWith creates a scrubber using a custom function. The function receives the snapshot content and should return the scrubbed content.
Example:
shutter.Snap(t, "data", data,
shutter.ScrubWith(func(content string) string {
return strings.ReplaceAll(content, "localhost", "<HOST>")
}),
)
