Skip to content

Commit

Permalink
Use error structs instead of fmt.Errorf (#55)
Browse files Browse the repository at this point in the history
* Refactor errors

* Fix go vet
  • Loading branch information
bradleyjkemp authored Mar 24, 2019
1 parent 4ebc4e8 commit 2f27407
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
13 changes: 8 additions & 5 deletions cupaloy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"strings"

"github.com/bradleyjkemp/cupaloy/v2/internal"
)

// New constructs a new, configured instance of cupaloy using the given
Expand Down Expand Up @@ -101,9 +103,9 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error {
prevSnapshot, err := c.readSnapshot(snapshotName)
if os.IsNotExist(err) {
if c.createNewAutomatically {
return c.updateSnapshot(snapshotName, snapshot)
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}
return fmt.Errorf("snapshot does not exist for test %s", snapshotName)
return internal.ErrNoSnapshot{Name: snapshotName}
}
if err != nil {
return err
Expand All @@ -116,9 +118,10 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error {

if c.shouldUpdate() {
// updates snapshot to current value and upgrades snapshot format
return c.updateSnapshot(snapshotName, snapshot)
return c.updateSnapshot(snapshotName, prevSnapshot, snapshot)
}

diff := diffSnapshots(prevSnapshot, snapshot)
return fmt.Errorf("snapshot not equal:\n%s", diff)
return internal.ErrSnapshotMismatch{
Diff: diffSnapshots(prevSnapshot, snapshot),
}
}
12 changes: 7 additions & 5 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 (
"bytes"
"github.com/bradleyjkemp/cupaloy/v2/internal"
"io/ioutil"
"strings"
"testing"
Expand Down Expand Up @@ -49,6 +50,7 @@ func TestConfig(t *testing.T) {
// This is to prevent you accidentally updating your snapshots in CI
func TestUpdate(t *testing.T) {
snapshotter := cupaloy.New(cupaloy.EnvVariableName("HOME"))
defer snapshotter.Snapshot("Hello world") // reset snapshot to known state

err := snapshotter.Snapshot("Hello world")
if err != nil {
Expand All @@ -59,11 +61,11 @@ func TestUpdate(t *testing.T) {
if err == nil {
t.Fatalf("Updating a snapshot with a new value is always an error %s", err)
}
if err.Error() != "snapshot updated for test examples_test-TestUpdate" {
t.Fatalf("Error returned will say that snapshot was updated")
if _, ok := err.(internal.ErrSnapshotUpdated); !ok {
t.Fatalf("Error returned will be of type ErrSnapshotUpdated")
}

snapshotter.Snapshot("Hello world") // reset snapshot to known state

}

// If a snapshot doesn't exist then it is created and an error returned
Expand All @@ -81,8 +83,8 @@ func TestMissingSnapshot(t *testing.T) {
if err == nil {
t.Fatalf("This will always return an error %s", err)
}
if err.Error() != "snapshot created for test examples_test-TestMissingSnapshot" {
t.Fatalf("Error returned will say that snapshot was created %s", err)
if _, ok := err.(internal.ErrSnapshotCreated); !ok {
t.Fatalf("Error returned will be of type ErrSnapshotCreated")
}
}

Expand Down
37 changes: 37 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package internal

import "fmt"

type ErrSnapshotCreated struct {
Name string
Contents string
}

func (e ErrSnapshotCreated) Error() string {
return fmt.Sprintf("snapshot created for test %s, with contents:\n%s", e.Name, e.Contents)
}

type ErrSnapshotUpdated struct {
Name string
Diff string
}

func (e ErrSnapshotUpdated) Error() string {
return fmt.Sprintf("snapshot %s updated:\n%s", e.Name, e.Diff)
}

type ErrSnapshotMismatch struct {
Diff string
}

func (e ErrSnapshotMismatch) Error() string {
return fmt.Sprintf("snapshot not equal:\n%s", e.Diff)
}

type ErrNoSnapshot struct {
Name string
}

func (e ErrNoSnapshot) Error() string {
return fmt.Sprintf("snapshot %s does not exist", e.Name)
}
17 changes: 13 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cupaloy
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/bradleyjkemp/cupaloy/v2/internal"

"github.com/davecgh/go-spew/spew"
"github.com/pmezard/go-difflib/difflib"
)
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c *Config) readSnapshot(snapshotName string) (string, error) {
return string(buf), nil
}

func (c *Config) updateSnapshot(snapshotName string, snapshot string) error {
func (c *Config) updateSnapshot(snapshotName string, prevSnapshot string, snapshot string) error {
// check that subdirectory exists before writing snapshot
err := os.MkdirAll(c.subDirName, os.ModePerm)
if err != nil {
Expand All @@ -110,11 +111,19 @@ func (c *Config) updateSnapshot(snapshotName string, snapshot string) error {
return nil
}

snapshotDiff := diffSnapshots(prevSnapshot, snapshot)

if isNewSnapshot {
return fmt.Errorf("snapshot created for test %s", snapshotName)
return internal.ErrSnapshotCreated{
Name: snapshotName,
Contents: snapshot,
}
}

return fmt.Errorf("snapshot updated for test %s", snapshotName)
return internal.ErrSnapshotUpdated{
Name: snapshotName,
Diff: snapshotDiff,
}
}

func diffSnapshots(previous, current string) string {
Expand Down

0 comments on commit 2f27407

Please sign in to comment.