Skip to content

Commit df20b00

Browse files
committed
Restructured code and now requires >= Go v1.13 for error handling
In order allow customizations of the golden library per unit test, the goldie library was restructured to not use global variables and adopt the function options pattern: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis This restructuring also made it easier to implement the following features: * Different fixture directories * A separate directory per unit test * A separate directory per subtest in the unit test * Erroring out if a template didn't get data that it needed * Diff output (classic or in color) * Using your own diff function for output Although a little non standard, the methods that a consumer will be using are now all in the `interface.go` file. In addition, error handling was cleaned up (now uses pointer receivers) and leverages the new Go v1.13 `errors` package. More unit testing is desired.
1 parent d313ffb commit df20b00

File tree

8 files changed

+516
-108
lines changed

8 files changed

+516
-108
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ what is present in the golden test file.
2424

2525
```
2626
func TestExample(t *testing.T) {
27+
g := goldie.New(t)
2728
recorder := httptest.NewRecorder()
2829
2930
req, err := http.NewRequest("GET", "/example", nil)
@@ -32,14 +33,14 @@ func TestExample(t *testing.T) {
3233
handler := http.HandlerFunc(ExampleHandler)
3334
handler.ServeHTTP()
3435
35-
goldie.Assert(t, "example", recorder.Body.Bytes())
36+
g.Assert(t, "example", recorder.Body.Bytes())
3637
}
3738
```
3839

3940
## Using template golden file
4041

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

4445
### example.golden
4546
```
@@ -49,6 +50,8 @@ This is a {{ .Type }} file.
4950
### Test
5051
```
5152
func TestTemplateExample(t *testing.T) {
53+
g := goldie.New(t)
54+
5255
recorder := httptest.NewRecorder()
5356
5457
req, err := http.NewRequest("POST", "/example/Golden", nil)
@@ -63,7 +66,7 @@ func TestTemplateExample(t *testing.T) {
6366
Type: "Golden",
6467
}
6568
66-
goldie.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
69+
g.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
6770
}
6871
```
6972

@@ -76,14 +79,32 @@ drop the `-update` flag.
7679

7780
`go test ./...`
7881

82+
## Options
83+
`goldie` supports a number of configuration options that will alter the behavior
84+
of the library. These options should be passed into the `goldie.New()` method.
85+
86+
```
87+
func TestNewExample(t *testing.T) {
88+
g := goldie.New(
89+
t,
90+
goldie.WithFixtureDir("test-fixtures"),
91+
goldie.WithNameSuffix(".golden.json"),
92+
goldie.WithDiffEngine(goldie.ColoredDiff),
93+
goldie.WithTestNameForDir(true),
94+
)
95+
96+
g.Assert(t, "example", "my example data")
97+
}
98+
99+
```
100+
79101
## FAQ
80102

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

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

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

88109
### Why the name `goldie`?
89110

errors.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ type errFixtureNotFound struct {
88
}
99

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

1718
// Error returns the error message.
18-
func (e errFixtureNotFound) Error() string {
19+
func (e *errFixtureNotFound) Error() string {
1920
return e.message
2021
}
2122

@@ -26,13 +27,13 @@ type errFixtureMismatch struct {
2627
}
2728

2829
// newErrFixtureMismatch returns a new instance of the error.
29-
func newErrFixtureMismatch(message string) errFixtureMismatch {
30-
return errFixtureMismatch{
30+
func newErrFixtureMismatch(message string) *errFixtureMismatch {
31+
return &errFixtureMismatch{
3132
message: message,
3233
}
3334
}
3435

35-
func (e errFixtureMismatch) Error() string {
36+
func (e *errFixtureMismatch) Error() string {
3637
return e.message
3738
}
3839

@@ -42,12 +43,32 @@ type errFixtureDirectoryIsFile struct {
4243
}
4344

4445
// newFixtureDirectoryIsFile returns a new instance of the error.
45-
func newErrFixtureDirectoryIsFile(file string) errFixtureDirectoryIsFile {
46-
return errFixtureDirectoryIsFile{
46+
func newErrFixtureDirectoryIsFile(file string) *errFixtureDirectoryIsFile {
47+
return &errFixtureDirectoryIsFile{
4748
file: file,
4849
}
4950
}
5051

51-
func (e errFixtureDirectoryIsFile) Error() string {
52+
func (e *errFixtureDirectoryIsFile) Error() string {
5253
return fmt.Sprintf("fixture folder is a file: %s", e.file)
5354
}
55+
56+
func (e *errFixtureDirectoryIsFile) File() string {
57+
return e.file
58+
}
59+
60+
// errMissingKey is thrown when a value for a template is missing
61+
type errMissingKey struct {
62+
message string
63+
}
64+
65+
// newErrMissingKey returns a new instance of the error.
66+
func newErrMissingKey(message string) *errMissingKey {
67+
return &errMissingKey{
68+
message: message,
69+
}
70+
}
71+
72+
func (e *errMissingKey) Error() string {
73+
return e.message
74+
}

errors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ func TestErrFixtureNotFound(t *testing.T) {
1111
err := newErrFixtureNotFound()
1212

1313
assert.Equal(t, expected, err.Error())
14-
assert.IsType(t, errFixtureNotFound{}, err)
14+
assert.IsType(t, &errFixtureNotFound{}, err)
1515
}
1616

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

2121
assert.Equal(t, message, err.Error())
22-
assert.IsType(t, errFixtureMismatch{}, err)
22+
assert.IsType(t, &errFixtureMismatch{}, err)
2323
}
2424

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

3030
assert.Equal(t, message, err.Error())
31-
assert.IsType(t, errFixtureDirectoryIsFile{}, err)
31+
assert.IsType(t, &errFixtureDirectoryIsFile{}, err)
3232
}

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ module github.com/sebdah/goldie
22

33
go 1.12
44

5-
require github.com/stretchr/testify v1.3.0
5+
require (
6+
github.com/pkg/errors v0.8.1
7+
github.com/pmezard/go-difflib v1.0.0
8+
github.com/sergi/go-diff v1.0.0
9+
github.com/stretchr/testify v1.3.0
10+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
4+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
8+
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
59
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
610
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
711
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 commit comments

Comments
 (0)