diff --git a/cupaloy.go b/cupaloy.go index 476fae6..50964b7 100644 --- a/cupaloy.go +++ b/cupaloy.go @@ -31,6 +31,11 @@ func SnapshotT(t TestingT, i ...interface{}) { Global.SnapshotT(t, i...) } +// SnapshotWithName calls Snapshotter.SnapshotWithName with the global config. +func SnapshotWithName(snapshotName string, i ...interface{}) error { + return Global.SnapshotWithName(snapshotName, i...) +} + // Snapshot compares the given variable to its previous value stored on the filesystem. // An error containing a diff is returned if the snapshots do not match, or if a new // snapshot was created. @@ -54,6 +59,12 @@ func (c *Config) SnapshotMulti(snapshotID string, i ...interface{}) error { return c.snapshot(snapshotName, i...) } +// SnapshotWithName is similar to SnapshotMulti without appending the function name. +// It is useful when you need full control of the snapshot filename. +func (c *Config) SnapshotWithName(snapshotName string, i ...interface{}) error { + return c.snapshot(snapshotName, i...) +} + // SnapshotT compares the given variable to the its previous value stored on the filesystem. // The current test is failed (with error containing a diff) if the values do not match, or // if a new snapshot was created. diff --git a/examples/.snapshots/chosen-by-user b/examples/.snapshots/chosen-by-user new file mode 100644 index 0000000..4d513f1 --- /dev/null +++ b/examples/.snapshots/chosen-by-user @@ -0,0 +1,2 @@ +Hello +Universe! diff --git a/examples/advanced_test.go b/examples/advanced_test.go index 8d76434..96912c0 100644 --- a/examples/advanced_test.go +++ b/examples/advanced_test.go @@ -47,6 +47,14 @@ func TestConfig(t *testing.T) { snapshotter.WithOptions(cupaloy.SnapshotSubdirectory("testdata")).SnapshotT(t, "Hello world!") } +// SnapshotMulti allows a user to choose the full snapshot name +func TestSnapshotWithName(t *testing.T) { + err := cupaloy.SnapshotWithName("chosen-by-user", "Hello", "Universe!") + if err != nil { + t.Fatalf("The config struct has all the same methods as the default %s", err) + } +} + // If a snapshot is updated then this returns an error // This is to prevent you accidentally updating your snapshots in CI func TestUpdate(t *testing.T) {