From 6b76b3870e774fc9ad75ffdd705274ec9649f0d8 Mon Sep 17 00:00:00 2001 From: Stephen Young Date: Wed, 15 Jan 2025 12:51:22 -0500 Subject: [PATCH] ! F added Noop and MultiScrubber, refactored scrubbers --- approvals.go | 39 +++++++++++++++++++-------------------- scrubber.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 scrubber.go diff --git a/approvals.go b/approvals.go index e0e1947..ef6d9b5 100644 --- a/approvals.go +++ b/approvals.go @@ -8,7 +8,6 @@ import ( "io" "os" "reflect" - "regexp" "strings" "github.com/approvals/go-approval-tests/reporters" @@ -260,8 +259,6 @@ func UseFolder(f string) { defaultFolder = f } -type scrubber func(s string) string - // verifyOptions can be accessed via the approvals.Options() API enabling configuration of scrubbers type verifyOptions struct { fields map[string]interface{} @@ -313,6 +310,25 @@ func Options() verifyOptions { return verifyOptions{} } +// WithScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder +func (v verifyOptions) WithScrubbers(scrubbers ...scrubber) verifyOptions { + return NewVerifyOptions(v.fields, "scrubbers", scrubbers) +} + +// AddScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder +func (v verifyOptions) AddScrubber(scrubfn scrubber) verifyOptions { + newScrubbers := append(v.getField("scrubbers", []scrubber{}).([]scrubber), scrubfn) + return v.WithScrubbers(newScrubbers...) +} + +// WithExtension overrides the default file extension (.txt) for approval files. +func (f fileOptions) WithExtension(extensionWithDot string) verifyOptions { + if !strings.HasPrefix(extensionWithDot, ".") { + extensionWithDot = "." + extensionWithDot + } + return NewVerifyOptions(f.fields, "extWithDot", extensionWithDot) +} + func (v verifyOptions) Scrub(reader io.Reader) (io.Reader, error) { b, err := io.ReadAll(reader) if err != nil { @@ -327,23 +343,6 @@ func (v verifyOptions) Scrub(reader io.Reader) (io.Reader, error) { return strings.NewReader(result), nil } -// WithRegexScrubber allows you to 'scrub' dynamic data such as timestamps within your test input -// and replace it with a static placeholder -func (v verifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string) verifyOptions { - newScrubbers := append(v.getField("scrubbers", []scrubber{}).([]scrubber), func(s string) string { - return regex.ReplaceAllString(s, replacer) - }) - return NewVerifyOptions(v.fields, "scrubbers", newScrubbers) -} - -// WithExtension overrides the default file extension (.txt) for approval files. -func (f fileOptions) WithExtension(extensionWithDot string) verifyOptions { - if !strings.HasPrefix(extensionWithDot, ".") { - extensionWithDot = "." + extensionWithDot - } - return NewVerifyOptions(f.fields, "extWithDot", extensionWithDot) -} - func NewVerifyOptions(fields map[string]interface{}, key string, value interface{}) verifyOptions { // Make a copy of the fields map, but with the new key and value newFields := make(map[string]interface{}, len(fields)) diff --git a/scrubber.go b/scrubber.go new file mode 100644 index 0000000..8e0d1eb --- /dev/null +++ b/scrubber.go @@ -0,0 +1,35 @@ +package approvals + +import "regexp" + +type scrubber func(s string) string + +// Deprecated: WithRegexScrubber allows you to 'scrub' dynamic data such as timestamps within your test input +// and replace it with a static placeholder +func (v verifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string) verifyOptions { + return v.AddScrubber(func(s string) string { + return regex.ReplaceAllString(s, replacer) + }) +} + +// CreateRegexScrubber allows you to create a scrubber that uses a regular expression to scrub data +func CreateRegexScrubber(regex *regexp.Regexp, replacer string) scrubber { + return func(s string) string { + return regex.ReplaceAllString(s, replacer) + } +} + +// NoopScrubber is a scrubber that does nothing +func NoopScrubber(s string) string { + return s +} + +// CreateMultiScrubber allows you to chain multiple scrubbers together +func CreateMultiScrubber(scrubbers ...scrubber) scrubber { + return func(s string) string { + for _, scrubber := range scrubbers { + s = scrubber(s) + } + return s + } +}