Skip to content

Commit

Permalink
Add SnapshotWithName (#72)
Browse files Browse the repository at this point in the history
* Add SnapshotWithName

SnapshotWithName is similar to SnapshotMulti without appending the function name.
It is useful when you need full control of the snapshot filename.

* Fix typo in test description

Co-authored-by: Aaran McGuire <[email protected]>

* Update examples/advanced_test.go

Co-authored-by: Aaran McGuire <[email protected]>
Co-authored-by: Bradley Kemp <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2021
1 parent 23b79b2 commit 28a118f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cupaloy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions examples/.snapshots/chosen-by-user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello
Universe!
8 changes: 8 additions & 0 deletions examples/advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 28a118f

Please sign in to comment.