Skip to content

Commit

Permalink
Write failed snapshots to disk (bradleyjkemp#70)
Browse files Browse the repository at this point in the history
* added option to write failed snapshots to disk. default is false

* added test
  • Loading branch information
ninogresenz committed Aug 4, 2022
1 parent 28a118f commit 137d03e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
39 changes: 25 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,24 @@ func UseStringerMethods(useStringerMethods bool) Configurator {
}
}

// WriteFailedSnapshot writes the failed snapshot to disk with an extra suffix .failed
// Default: false
func WriteFailedSnapshot(shouldWriteFailedSnapshot bool) Configurator {
return func(c *Config) {
c.shouldWriteFailedSnapshot = shouldWriteFailedSnapshot
}
}

// Config provides the same snapshotting functions with additional configuration capabilities.
type Config struct {
shouldUpdate func() bool
subDirName string
failOnUpdate bool
createNewAutomatically bool
fatalOnMismatch bool
snapshotFileExtension string
useStringerMethods bool
shouldUpdate func() bool
subDirName string
failOnUpdate bool
createNewAutomatically bool
fatalOnMismatch bool
snapshotFileExtension string
useStringerMethods bool
shouldWriteFailedSnapshot bool
}

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

Expand All @@ -115,12 +125,13 @@ var Global = NewDefaultConfig()

func (c *Config) clone() *Config {
return &Config{
shouldUpdate: c.shouldUpdate,
subDirName: c.subDirName,
failOnUpdate: c.failOnUpdate,
createNewAutomatically: c.createNewAutomatically,
fatalOnMismatch: c.fatalOnMismatch,
snapshotFileExtension: c.snapshotFileExtension,
useStringerMethods: c.useStringerMethods,
shouldUpdate: c.shouldUpdate,
subDirName: c.subDirName,
failOnUpdate: c.failOnUpdate,
createNewAutomatically: c.createNewAutomatically,
fatalOnMismatch: c.fatalOnMismatch,
snapshotFileExtension: c.snapshotFileExtension,
useStringerMethods: c.useStringerMethods,
shouldWriteFailedSnapshot: c.shouldWriteFailedSnapshot,
}
}
5 changes: 5 additions & 0 deletions cupaloy.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}

if c.shouldWriteFailedSnapshot {
// writes the current (not matching) to a file with an extra suffix: .failed
return c.writeFailedSnapshot(snapshotName, prevSnapshot, snapshot)
}

return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
}
Expand Down
1 change: 1 addition & 0 deletions examples/.snapshots/TestWriteFailedSnapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
28 changes: 28 additions & 0 deletions examples/advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package examples_test

import (
"bytes"
"io/fs"
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -236,3 +238,29 @@ func TestUseStringerMethods(t *testing.T) {
cupaloy.New(cupaloy.UseStringerMethods(false)).SnapshotT(t, s)
})
}

func TestWriteFailedSnapshot(t *testing.T) {
dir := "./.snapshots"
snapShotName := t.Name()
failedSnapshotName := snapShotName + ".failed"
fileStat := func() error {
_, err := fs.Stat(os.DirFS(dir), failedSnapshotName)
return err
}
err := fileStat()
if err == nil {
t.Fatalf("File %s should not be there yet.", failedSnapshotName)
}
defer func() {
_ = os.Remove(dir + "/" + failedSnapshotName)
}()
snapshotter := cupaloy.New(cupaloy.WriteFailedSnapshot(true))
err = snapshotter.SnapshotWithName(snapShotName, "Hello World! <changed>")
if err == nil {
t.Fatal("It should return an error as the snapshots are not equal.")
}
err = fileStat()
if err != nil {
t.Fatalf("Could not find %s", failedSnapshotName)
}
}
15 changes: 15 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ func (c *Config) updateSnapshot(snapshotName string, prevSnapshot string, snapsh
}
}

func (c *Config) writeFailedSnapshot(snapshotName string, prevSnapshot string, snapshot string) error {
snapshotFile := c.snapshotFilePath(snapshotName)
_, err := os.Stat(snapshotFile)
if err != nil {
return errors.New("could not find snapshot file")
}
err = ioutil.WriteFile(snapshotFile+".failed", []byte(snapshot), os.FileMode(0644))
if err != nil {
return err
}
return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
}
}

func diffSnapshots(previous, current string) string {
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(previous),
Expand Down

0 comments on commit 137d03e

Please sign in to comment.