Skip to content

Commit

Permalink
Add option to specify custom snapshot differ (#64)
Browse files Browse the repository at this point in the history
* Add option to specify custom snapshot differ

* Update updateShapshot to use the differ

* Update config.go

Co-authored-by: Bradley Kemp <[email protected]>

Co-authored-by: Bradley Kemp <[email protected]>
  • Loading branch information
smitt04 and bradleyjkemp authored Sep 14, 2022
1 parent 28a118f commit bae0788
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func SnapshotFileExtension(snapshotFileExtension string) Configurator {
}
}

// DiffSnapshots allows you to change the diffing function used to display the
// difference between the previous snapshot and the current.
// Default: Internal differ using difflib
func DiffSnapshots(differ func(previous, current string) string) Configurator {
return func(c *Config) {
c.diffSnapshots = differ
}
}

// 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
Expand All @@ -93,6 +102,7 @@ type Config struct {
createNewAutomatically bool
fatalOnMismatch bool
snapshotFileExtension string
diffSnapshots func(previous, current string) string
useStringerMethods bool
}

Expand All @@ -106,6 +116,7 @@ func NewDefaultConfig() *Config {
CreateNewAutomatically(true),
FatalOnMismatch(false),
SnapshotFileExtension(""),
DiffSnapshots(diffSnapshots),
UseStringerMethods(true),
)
}
Expand All @@ -121,6 +132,7 @@ func (c *Config) clone() *Config {
createNewAutomatically: c.createNewAutomatically,
fatalOnMismatch: c.fatalOnMismatch,
snapshotFileExtension: c.snapshotFileExtension,
diffSnapshots: c.diffSnapshots,
useStringerMethods: c.useStringerMethods,
}
}
2 changes: 1 addition & 1 deletion cupaloy.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
}

return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
Diff: c.diffSnapshots(prevSnapshot, snapshot),
}
}
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *Config) updateSnapshot(snapshotName string, prevSnapshot string, snapsh
return nil
}

snapshotDiff := diffSnapshots(prevSnapshot, snapshot)
snapshotDiff := c.diffSnapshots(prevSnapshot, snapshot)

if isNewSnapshot {
return internal.ErrSnapshotCreated{
Expand Down

0 comments on commit bae0788

Please sign in to comment.