Skip to content

Commit

Permalink
Allow setting custom file extension for snapshots (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyjkemp authored Oct 21, 2018
1 parent 2969b70 commit 29f58f1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
13 changes: 13 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,24 @@ func FatalOnMismatch(fatalOnMismatch bool) Configurator {
}
}

// SnapshotFileExtension allows you to change the extension of the snapshot files
// that are written. E.g. if you're snapshotting HTML then adding SnapshotFileExtension(".html")
// will allow for more easier viewing of snapshots.
// Default: "", no extension is added.
func SnapshotFileExtension(snapshotFileExtension string) Configurator {
return func(c *Config) {
c.snapshotFileExtension = snapshotFileExtension
}
}

// 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
}

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

Expand All @@ -97,5 +109,6 @@ func (c *Config) clone() *Config {
failOnUpdate: c.failOnUpdate,
createNewAutomatically: c.createNewAutomatically,
fatalOnMismatch: c.fatalOnMismatch,
snapshotFileExtension: c.snapshotFileExtension,
}
}
1 change: 1 addition & 0 deletions examples/.snapshots/TestSnapshotFileExtension.myextension
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This should end up in a file with extension .myextension
5 changes: 5 additions & 0 deletions examples/advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,8 @@ func TestGlobalFatalOnMismatch(t *testing.T) {
mockT.AssertNotCalled(t, "Error", mock.Anything)
mockT.AssertCalled(t, "Fatal", mock.Anything)
}

func TestSnapshotFileExtension(t *testing.T) {
snapshotter := cupaloy.New(cupaloy.SnapshotFileExtension(".myextension"))
snapshotter.SnapshotT(t, "This should end up in a file with extension .myextension")
}
4 changes: 2 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func envVariableSet(envVariable string) bool {
}

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

// Legacy snapshot format where all items were spewed
Expand Down Expand Up @@ -109,7 +109,7 @@ func (c *Config) updateSnapshot(snapshotName string, snapshot string) error {
//TODO: should a warning still be printed here?
return nil
}

if isNewSnapshot {
return fmt.Errorf("snapshot created for test %s", snapshotName)
}
Expand Down

0 comments on commit 29f58f1

Please sign in to comment.