Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructuring of package plus diff generation and new errors #15

Merged
merged 12 commits into from
Oct 17, 2019
Merged
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ func TestExample(t *testing.T) {
handler := http.HandlerFunc(ExampleHandler)
handler.ServeHTTP()

goldie.Assert(t, "example", recorder.Body.Bytes())
g := goldie.New(t)
g.Assert(t, "example", recorder.Body.Bytes())
}
```

## Using template golden file

If some values in the golden file can change depending on the test, you can use golang
template in the golden file and pass the data to `goldie.AssertWithTemplate`.
template in the golden file and pass the data to `AssertWithTemplate`.

### example.golden
```
Expand All @@ -63,7 +64,8 @@ func TestTemplateExample(t *testing.T) {
Type: "Golden",
}

goldie.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
g := goldie.New(t)
g.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
}
```

Expand All @@ -76,14 +78,32 @@ drop the `-update` flag.

`go test ./...`

## Options
`goldie` supports a number of configuration options that will alter the behavior
of the library. These options should be passed into the `goldie.New()` method.

```
func TestNewExample(t *testing.T) {
g := goldie.New(
t,
goldie.WithFixtureDir("test-fixtures"),
goldie.WithNameSuffix(".golden.json"),
goldie.WithDiffEngine(goldie.ColoredDiff),
goldie.WithTestNameForDir(true),
)

g.Assert(t, "example", []byte("my example data"))
}

```

## FAQ

### Do you need any help in the project?

Yes, please! Pull requests are most welcome. On the wish list:

- Unit tests.
- Better output for failed tests. A diff of some sort would be great.

### Why the name `goldie`?

Expand Down
39 changes: 30 additions & 9 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ type errFixtureNotFound struct {
}

// newErrFixtureNotFound returns a new instance of the error.
func newErrFixtureNotFound() errFixtureNotFound {
return errFixtureNotFound{
func newErrFixtureNotFound() *errFixtureNotFound {
return &errFixtureNotFound{
// TODO: flag name should be based on the variable value
message: "Golden fixture not found. Try running with -update flag.",
}
}

// Error returns the error message.
func (e errFixtureNotFound) Error() string {
func (e *errFixtureNotFound) Error() string {
return e.message
}

Expand All @@ -26,13 +27,13 @@ type errFixtureMismatch struct {
}

// newErrFixtureMismatch returns a new instance of the error.
func newErrFixtureMismatch(message string) errFixtureMismatch {
return errFixtureMismatch{
func newErrFixtureMismatch(message string) *errFixtureMismatch {
return &errFixtureMismatch{
message: message,
}
}

func (e errFixtureMismatch) Error() string {
func (e *errFixtureMismatch) Error() string {
return e.message
}

Expand All @@ -42,12 +43,32 @@ type errFixtureDirectoryIsFile struct {
}

// newFixtureDirectoryIsFile returns a new instance of the error.
func newErrFixtureDirectoryIsFile(file string) errFixtureDirectoryIsFile {
return errFixtureDirectoryIsFile{
func newErrFixtureDirectoryIsFile(file string) *errFixtureDirectoryIsFile {
return &errFixtureDirectoryIsFile{
file: file,
}
}

func (e errFixtureDirectoryIsFile) Error() string {
func (e *errFixtureDirectoryIsFile) Error() string {
return fmt.Sprintf("fixture folder is a file: %s", e.file)
}

func (e *errFixtureDirectoryIsFile) File() string {
return e.file
}

// errMissingKey is thrown when a value for a template is missing
type errMissingKey struct {
message string
}

// newErrMissingKey returns a new instance of the error.
func newErrMissingKey(message string) *errMissingKey {
return &errMissingKey{
message: message,
}
}

func (e *errMissingKey) Error() string {
return e.message
}
6 changes: 3 additions & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func TestErrFixtureNotFound(t *testing.T) {
err := newErrFixtureNotFound()

assert.Equal(t, expected, err.Error())
assert.IsType(t, errFixtureNotFound{}, err)
assert.IsType(t, &errFixtureNotFound{}, err)
}

func TestErrFixtureMismatch(t *testing.T) {
message := "example message"
err := newErrFixtureMismatch(message)

assert.Equal(t, message, err.Error())
assert.IsType(t, errFixtureMismatch{}, err)
assert.IsType(t, &errFixtureMismatch{}, err)
}

func TestErrFixtureDirectoryIsFile(t *testing.T) {
Expand All @@ -28,5 +28,5 @@ func TestErrFixtureDirectoryIsFile(t *testing.T) {
err := newErrFixtureDirectoryIsFile(location)

assert.Equal(t, message, err.Error())
assert.IsType(t, errFixtureDirectoryIsFile{}, err)
assert.IsType(t, &errFixtureDirectoryIsFile{}, err)
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/sebdah/goldie

go 1.12

require github.com/stretchr/testify v1.3.0
require (
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/sergi/go-diff v1.0.0
github.com/stretchr/testify v1.3.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Loading