-
Notifications
You must be signed in to change notification settings - Fork 0
/
gold_test.go
147 lines (108 loc) · 3.14 KB
/
gold_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gold_test
import (
"io/ioutil"
"testing"
. "github.com/mfcochauxlaberge/gold"
"github.com/stretchr/testify/assert"
)
func TestNewRunner(t *testing.T) {
assert := assert.New(t)
runner := NewRunner("some/path")
assert.Equal(false, runner.Update)
assert.Equal("some/path", runner.Directory)
assert.Len(runner.Filters, 0)
}
func TestRunnerPrepare(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir("", "")
assert.NoError(err)
// Put a file in the directory.
_, err = ioutil.TempFile(dir, "")
assert.NoError(err)
// Make sure it exists.
files, err := ioutil.ReadDir(dir)
assert.NoError(err)
assert.Equal(1, len(files))
// Create runner and prepare it.
runner := NewRunner(dir)
// Nothing happens here because
// update mode is disabled.
err = runner.Prepare()
assert.NoError(err)
files, err = ioutil.ReadDir(dir)
assert.NoError(err)
assert.Equal(1, len(files))
// The directory gets deleted and
// recreated in Prepare, so it
// should be empty after the call.
runner.Update = true
err = runner.Prepare()
assert.NoError(err)
files, err = ioutil.ReadDir(dir)
assert.NoError(err)
assert.Equal(0, len(files))
}
func TestRunnerTest(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir("", "")
assert.NoError(err)
// Create runner and prepare it.
runner := NewRunner(dir)
runner.Update = true
err = runner.Prepare()
assert.NoError(err)
// With update mode on, the following calls will
// create files.
err = runner.Test("test1", []byte("This is a test."))
assert.NoError(err)
err = runner.Test("test2", []byte("This is another test."))
assert.NoError(err)
// The update mode is turned off. The following should
// not return an error since the files have already
// been created.
runner.Update = false
err = runner.Test("test1", []byte("This is a test."))
assert.NoError(err)
err = runner.Test("test2", []byte("This is another test."))
assert.NoError(err)
}
func TestRunnerWithFilter(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir("", "")
assert.NoError(err)
// Create runner and prepare it.
runner := NewRunner(dir)
runner.Update = true
err = runner.Prepare()
assert.NoError(err)
runner.Filters = append(runner.Filters, func(src []byte) []byte {
return []byte("The output was altered.")
})
// Create file.
err = runner.Test("test1", []byte("The output was not altered."))
assert.NoError(err)
// Turn off update mode and check file.
runner.Update = false
err = runner.Test("test1", []byte("The output was altered."))
assert.NoError(err)
}
func TestRunnerTestComparisonError(t *testing.T) {
assert := assert.New(t)
dir, err := ioutil.TempDir("", "")
assert.NoError(err)
// Create runner and prepare it.
runner := NewRunner(dir)
runner.Update = true
err = runner.Prepare()
assert.NoError(err)
// Create file.
err = runner.Test("test1", []byte("This is a test."))
assert.NoError(err)
// Turn off update mode and check file.
runner.Update = false
err = runner.Test("test1", []byte("The content is different."))
assert.Error(err)
cErr, ok := err.(ComparisonError)
assert.True(ok)
assert.Equal("output and file are different", cErr.Error())
}