From 29f58f1e4393a0062f60596ee97bfec34a25cd5f Mon Sep 17 00:00:00 2001 From: Bradley Kemp Date: Sun, 21 Oct 2018 09:58:05 +0100 Subject: [PATCH] Allow setting custom file extension for snapshots (#47) --- config.go | 13 +++++++++++++ .../TestSnapshotFileExtension.myextension | 1 + examples/advanced_test.go | 5 +++++ util.go | 4 ++-- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 examples/.snapshots/TestSnapshotFileExtension.myextension diff --git a/config.go b/config.go index ac4a080..7e74142 100644 --- a/config.go +++ b/config.go @@ -66,6 +66,16 @@ 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 @@ -73,6 +83,7 @@ type Config struct { failOnUpdate bool createNewAutomatically bool fatalOnMismatch bool + snapshotFileExtension string } // NewDefaultConfig returns a new Config instance initialised with the same options as @@ -84,6 +95,7 @@ func NewDefaultConfig() *Config { FailOnUpdate(true), CreateNewAutomatically(true), FatalOnMismatch(false), + SnapshotFileExtension(""), ) } @@ -97,5 +109,6 @@ func (c *Config) clone() *Config { failOnUpdate: c.failOnUpdate, createNewAutomatically: c.createNewAutomatically, fatalOnMismatch: c.fatalOnMismatch, + snapshotFileExtension: c.snapshotFileExtension, } } diff --git a/examples/.snapshots/TestSnapshotFileExtension.myextension b/examples/.snapshots/TestSnapshotFileExtension.myextension new file mode 100644 index 0000000..af7b243 --- /dev/null +++ b/examples/.snapshots/TestSnapshotFileExtension.myextension @@ -0,0 +1 @@ +This should end up in a file with extension .myextension diff --git a/examples/advanced_test.go b/examples/advanced_test.go index f57621b..5cc14b3 100644 --- a/examples/advanced_test.go +++ b/examples/advanced_test.go @@ -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") +} diff --git a/util.go b/util.go index 50b211c..1e192e7 100644 --- a/util.go +++ b/util.go @@ -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 @@ -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) }