Skip to content

Commit

Permalink
Add custom ShouldUpdate function (+minor configuration refactor) (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyjkemp authored Jan 17, 2018
1 parent 6fb5357 commit 1526e6d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
35 changes: 21 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ package cupaloy
// Configurator is a functional option that can be passed to cupaloy.New() to change snapshotting behaviour.
type Configurator func(*config)

// EnvVariableName can be used to customize the environment variable that determines whether snapshots should be updated.
// EnvVariableName can be used to customize the environment variable that determines whether snapshots should be updated
// e.g.
// cupaloy.New(EnvVariableName("UPDATE"))
// Will create an instance where snapshots will be updated if the UPDATE environment variable is set,
// instead of the default of UPDATE_SNAPSHOTS.
func EnvVariableName(name string) Configurator {
return func(c *config) {
c.envVariable = name
c.shouldUpdate = func() bool {
return envVariableSet(name)
}
}
}

// ShouldUpdate can be used to provide custom logic to decide whether or not to update a snapshot
// e.g.
// var update = flag.Bool("update", false, "update snapshots")
// cupaloy.New(ShouldUpdate(func () bool { return *update })
// Will create an instance where snapshots are updated if the --update flag is passed to go test.
func ShouldUpdate(f func() bool) Configurator {
return func(c *config) {
c.shouldUpdate = f
}
}

Expand All @@ -25,19 +38,13 @@ func SnapshotSubdirectory(name string) Configurator {
}

type config struct {
envVariable string
subDirName string
snapshotExtension string
shouldUpdate func() bool
subDirName string
}

func defaultConfig() *config {
return &config{
envVariable: "UPDATE_SNAPSHOTS",
subDirName: ".snapshots",
snapshotExtension: "",
}
}

func (c *config) shouldUpdate() bool {
return envVariableSet(c.envVariable)
c := &config{}
SnapshotSubdirectory(".snapshots")(c)
EnvVariableName("UPDATE_SNAPSHOTS")(c)
return c
}
1 change: 1 addition & 0 deletions examples/.snapshots/examples_test-TestShouldUpdate-func1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(string) (len=5) "Hello"
1 change: 1 addition & 0 deletions examples/.snapshots/examples_test-TestShouldUpdate-func2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(string) (len=5) "Hello"
26 changes: 26 additions & 0 deletions examples/advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package examples_test

import (
"io/ioutil"
"strings"
"testing"

"github.com/bradleyjkemp/cupaloy"
Expand Down Expand Up @@ -84,3 +85,28 @@ func TestMultipleSnapshots(t *testing.T) {
cupaloy.New().SnapshotT(t, result2)
})
}

// Test the ShouldUpdate configurator
func TestShouldUpdate(t *testing.T) {
t.Run("false", func(t *testing.T) {
result := "Hello!"
err := cupaloy.New(cupaloy.ShouldUpdate(func() bool { return false })).Snapshot(result)
if err == nil || !strings.Contains(err.Error(), "not equal") {
// not updating snapshot so error should contain a diff
t.Fatal(err)
}
})

t.Run("true", func(t *testing.T) {
result := "Hello!"
c := cupaloy.New(cupaloy.ShouldUpdate(func() bool { return true }))
err := c.Snapshot(result)
if err == nil || !strings.Contains(err.Error(), "updated") {
// snapshot should have been updated with error signalling this
t.Fatal(err)
}

// snapshot again with old value to revert the update
c.Snapshot("Hello")
})
}
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func envVariableSet(envVariable string) bool {
}

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

func takeSnapshot(i ...interface{}) string {
Expand Down

0 comments on commit 1526e6d

Please sign in to comment.