Skip to content

Commit

Permalink
! F added Noop and MultiScrubber, refactored scrubbers
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Jan 15, 2025
1 parent 9caa58a commit 6b76b38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
39 changes: 19 additions & 20 deletions approvals.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"os"
"reflect"
"regexp"
"strings"

"github.com/approvals/go-approval-tests/reporters"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand Down
35 changes: 35 additions & 0 deletions scrubber.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 6b76b38

Please sign in to comment.