Approval-based snapshot testing library for Go (mirror)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: fix scrubber json formatting issues

+18 -105
+3 -3
__snapshots__/test_complex_nested_structure.snap
··· 24 24 "user", 25 25 }, 26 26 Metadata: map[string]interface{}{ 27 - "language": "en-utf-8", 27 + "language": "en", 28 28 "notifications": true, 29 29 "preferences": map[string]interface{}{ 30 - "email_frequency": "bi-weekly", 30 + "email_frequency": "weekly", 31 31 "notifications": true, 32 32 }, 33 - "theme": "darker", 33 + "theme": "dark", 34 34 }, 35 35 }, 36 36 Tags: []string{
-87
__snapshots__/test_complex_nested_structure.snap.new
··· 1 - --- 2 - title: Complex Nested Structure 3 - test_name: TestComplexNestedStructure 4 - file_name: freeze_test.go 5 - version: 0.1.0 6 - --- 7 - freeze_test.Post{ 8 - ID: 100, 9 - Title: "Introduction to Go Snapshot Testing", 10 - Content: "This is a comprehensive guide to snapshot testing in Go...", 11 - Author: freeze_test.User{ 12 - ID: 1, 13 - Username: "john_doe", 14 - Email: "john@example.com", 15 - Active: true, 16 - CreatedAt: time.Time{ 17 - wall: 0x0, 18 - ext: 63809375400, 19 - loc: (*time.Location)(nil), 20 - }, 21 - Roles: []string{ 22 - "admin", 23 - "moderator", 24 - "user", 25 - }, 26 - Metadata: map[string]interface{}{ 27 - "language": "en", 28 - "notifications": true, 29 - "preferences": map[string]interface{}{ 30 - "email_frequency": "weekly", 31 - "notifications": true, 32 - }, 33 - "theme": "dark", 34 - }, 35 - }, 36 - Tags: []string{ 37 - "go", 38 - "testing", 39 - "snapshots", 40 - "best-practices", 41 - }, 42 - Comments: []freeze_test.Comment{ 43 - { 44 - ID: 1, 45 - Author: "alice", 46 - Content: "Great post!", 47 - CreatedAt: time.Time{ 48 - wall: 0x0, 49 - ext: 63810858120, 50 - loc: (*time.Location)(nil), 51 - }, 52 - Replies: []freeze_test.Comment{ 53 - { 54 - ID: 2, 55 - Author: "bob", 56 - Content: "I agree!", 57 - CreatedAt: time.Time{ 58 - wall: 0x0, 59 - ext: 63810863100, 60 - loc: (*time.Location)(nil), 61 - }, 62 - Replies: []freeze_test.Comment{ 63 - }, 64 - }, 65 - }, 66 - }, 67 - { 68 - ID: 3, 69 - Author: "charlie", 70 - Content: "Thanks for sharing!", 71 - CreatedAt: time.Time{ 72 - wall: 0x0, 73 - ext: 63810927000, 74 - loc: (*time.Location)(nil), 75 - }, 76 - Replies: []freeze_test.Comment{ 77 - }, 78 - }, 79 - }, 80 - Likes: 42, 81 - Published: true, 82 - CreatedAt: time.Time{ 83 - wall: 0x0, 84 - ext: 63809802000, 85 - loc: (*time.Location)(nil), 86 - }, 87 - }
+3 -3
__snapshots__/test_credit_card_scrubbing.snap
··· 5 5 version: 0.1.0 6 6 --- 7 7 { 8 - "another_4532123456789010<CREDIT_CARD>", 9 - "backup_4532 1234 5678 9010<CREDIT_CARD>", 10 - "card_number": "4532-1234-5678-9010", 8 + "another_card": "<CREDIT_CARD>", 9 + "backup_card": "<CREDIT_CARD>", 10 + "card_number": "<CREDIT_CARD>", 11 11 "name": "John Doe" 12 12 }
+3 -3
__snapshots__/test_unix_timestamp_scrubbing.snap
··· 5 5 version: 0.1.0 6 6 --- 7 7 { 8 - "1699999999<UNIX_TS>, 9 - "deleted": 1700000000, 8 + "created": <UNIX_TS>, 9 + "deleted": <UNIX_TS>, 10 10 "name": "Test Event", 11 - "1700000000000<UNIX_TS> 11 + "updated": <UNIX_TS> 12 12 }
+9 -9
scrubbers.go
··· 54 54 uuidPattern = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`) 55 55 iso8601Pattern = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?`) 56 56 emailPattern = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`) 57 - // More conservative Unix timestamp pattern - requires context markers 58 - // Matches timestamps with common prefixes/suffixes to avoid false positives on IDs 59 - unixTsPattern = regexp.MustCompile(`(?:timestamp|time|ts|created|updated|at)["\s:=]+(\d{10,13})\b`) 57 + // Unix timestamp pattern - matches 10-13 digit numbers (Unix timestamps in seconds or milliseconds) 58 + // Note: This is aggressive and may match other numbers. Use with caution or customize. 59 + unixTsPattern = regexp.MustCompile(`\b\d{10,13}\b`) 60 60 // IPv4 pattern with basic range validation (not perfect, but better) 61 61 ipv4Pattern = regexp.MustCompile(`\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b`) 62 - // Credit card pattern - more conservative, requires context 63 - creditCardPattern = regexp.MustCompile(`(?:card|cc|payment)["\s:=]+(\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4})\b`) 62 + // Credit card pattern - matches 16 digit numbers with optional separators 63 + creditCardPattern = regexp.MustCompile(`\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b`) 64 64 jwtPattern = regexp.MustCompile(`eyJ[a-zA-Z0-9_-]*\.eyJ[a-zA-Z0-9_-]*\.[a-zA-Z0-9_-]*`) 65 65 ) 66 66 ··· 89 89 } 90 90 91 91 // ScrubUnixTimestamps replaces Unix timestamps (10-13 digits) with "<UNIX_TS>". 92 - // Note: This uses a conservative pattern that requires context keywords to avoid 93 - // false positives on IDs and other numbers. For aggressive scrubbing, use a custom regex. 92 + // Note: This is aggressive and may match other long numbers. For more conservative 93 + // scrubbing with context keywords, use a custom regex. 94 94 func ScrubUnixTimestamps() SnapshotOption { 95 95 return WithScrubber(&regexScrubber{ 96 96 pattern: unixTsPattern, 97 - replacement: "$1<UNIX_TS>", 97 + replacement: "<UNIX_TS>", 98 98 }) 99 99 } 100 100 ··· 112 112 func ScrubCreditCards() SnapshotOption { 113 113 return WithScrubber(&regexScrubber{ 114 114 pattern: creditCardPattern, 115 - replacement: "$1<CREDIT_CARD>", 115 + replacement: "<CREDIT_CARD>", 116 116 }) 117 117 } 118 118