Skip to content

Commit

Permalink
Add an option to disabled usage of stringer methods.
Browse files Browse the repository at this point in the history
This is especially annoying for snapshotting custom errors that contain
some payload. The payload would arbitrary be dismissed which is
confusing.

fixes #45
  • Loading branch information
gilbsgilbs committed Aug 31, 2020
1 parent 132dad5 commit 4f373e7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
12 changes: 12 additions & 0 deletions config.go
Expand Up @@ -76,6 +76,15 @@ func SnapshotFileExtension(snapshotFileExtension string) Configurator {
}
}

// UseStringerMethods invoke String() or Error() methods when available rather than dumping the object.
// This should probably be disabled by default but is not for backwards compatibility reasons.
// Default: true
func UseStringerMethods(useStringerMethods bool) Configurator {
return func(c *Config) {
c.useStringerMethods = useStringerMethods
}
}

// Config provides the same snapshotting functions with additional configuration capabilities.
type Config struct {
shouldUpdate func() bool
Expand All @@ -84,6 +93,7 @@ type Config struct {
createNewAutomatically bool
fatalOnMismatch bool
snapshotFileExtension string
useStringerMethods bool
}

// NewDefaultConfig returns a new Config instance initialised with the same options as
Expand All @@ -96,6 +106,7 @@ func NewDefaultConfig() *Config {
CreateNewAutomatically(true),
FatalOnMismatch(false),
SnapshotFileExtension(""),
UseStringerMethods(true),
)
}

Expand All @@ -110,5 +121,6 @@ func (c *Config) clone() *Config {
createNewAutomatically: c.createNewAutomatically,
fatalOnMismatch: c.fatalOnMismatch,
snapshotFileExtension: c.snapshotFileExtension,
useStringerMethods: c.useStringerMethods,
}
}
4 changes: 2 additions & 2 deletions cupaloy.go
Expand Up @@ -98,7 +98,7 @@ func (c *Config) WithOptions(configurators ...Configurator) *Config {
}

func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
snapshot := takeSnapshot(i...)
snapshot := c.takeSnapshot(i...)

prevSnapshot, err := c.readSnapshot(snapshotName)
if os.IsNotExist(err) {
Expand All @@ -111,7 +111,7 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
return err
}

if snapshot == prevSnapshot || takeV1Snapshot(i...) == prevSnapshot {
if snapshot == prevSnapshot || c.takeV1Snapshot(i...) == prevSnapshot {
// previous snapshot matches current value
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions examples/.snapshots/TestUseStringerMethods-disabled
@@ -0,0 +1,2 @@
(examples_test.stringer) {
}
1 change: 1 addition & 0 deletions examples/.snapshots/TestUseStringerMethods-enabled
@@ -0,0 +1 @@
(examples_test.stringer) with stringer
23 changes: 21 additions & 2 deletions examples/advanced_test.go
Expand Up @@ -2,11 +2,12 @@ package examples_test

import (
"bytes"
"github.com/bradleyjkemp/cupaloy/v2/internal"
"io/ioutil"
"strings"
"testing"

"github.com/bradleyjkemp/cupaloy/v2/internal"

"github.com/bradleyjkemp/cupaloy/v2"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -65,7 +66,6 @@ func TestUpdate(t *testing.T) {
t.Fatalf("Error returned will be of type ErrSnapshotUpdated")
}


}

// If a snapshot doesn't exist then it is created and an error returned
Expand Down Expand Up @@ -209,3 +209,22 @@ func TestSnapshotFileExtension(t *testing.T) {
snapshotter := cupaloy.New(cupaloy.SnapshotFileExtension(".myextension"))
snapshotter.SnapshotT(t, "This should end up in a file with extension .myextension")
}

type stringer struct {
}

func (s stringer) String() string {
return "with stringer"
}

func TestUseStringerMethods(t *testing.T) {
s := stringer{}

t.Run("enabled", func(t *testing.T) {
cupaloy.New(cupaloy.UseStringerMethods(true)).SnapshotT(t, s)
})

t.Run("disabled", func(t *testing.T) {
cupaloy.New(cupaloy.UseStringerMethods(false)).SnapshotT(t, s)
})
}
27 changes: 15 additions & 12 deletions util.go
Expand Up @@ -15,14 +15,6 @@ import (
"github.com/pmezard/go-difflib/difflib"
)

var spewConfig = spew.ConfigState{
Indent: " ",
SortKeys: true, // maps should be spewed in a deterministic order
DisablePointerAddresses: true, // don't spew the addresses of pointers
DisableCapacities: true, // don't spew capacities of collections
SpewKeys: true, // if unable to sort map keys then spew keys to strings and sort those
}

//go:generate $GOPATH/bin/mockery -output=examples -outpkg=examples_test -testonly -name=TestingT

// TestingT is a subset of the interface testing.TB allowing it to be mocked in tests.
Expand All @@ -47,17 +39,28 @@ func envVariableSet(envVariable string) bool {
return varSet
}

func (c *Config) getSpewConfig() *spew.ConfigState {
return &spew.ConfigState{
Indent: " ",
SortKeys: true, // maps should be spewed in a deterministic order
DisablePointerAddresses: true, // don't spew the addresses of pointers
DisableCapacities: true, // don't spew capacities of collections
SpewKeys: true, // if unable to sort map keys then spew keys to strings and sort those
DisableMethods: !c.useStringerMethods,
}
}

func (c *Config) snapshotFilePath(testName string) string {
return filepath.Join(c.subDirName, testName+c.snapshotFileExtension)
}

// Legacy snapshot format where all items were spewed
func takeV1Snapshot(i ...interface{}) string {
return spewConfig.Sdump(i...)
func (c *Config) takeV1Snapshot(i ...interface{}) string {
return c.getSpewConfig().Sdump(i...)
}

// New snapshot format where some types are written out raw to the file
func takeSnapshot(i ...interface{}) string {
func (c *Config) takeSnapshot(i ...interface{}) string {
snapshot := &bytes.Buffer{}
for _, v := range i {
switch vt := v.(type) {
Expand All @@ -68,7 +71,7 @@ func takeSnapshot(i ...interface{}) string {
snapshot.Write(vt)
snapshot.WriteString("\n")
default:
spewConfig.Fdump(snapshot, v)
c.getSpewConfig().Fdump(snapshot, v)
}
}

Expand Down

0 comments on commit 4f373e7

Please sign in to comment.