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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
package goldie
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGoldenFileName(t *testing.T) {
tests := map[string]struct {
name string
options []Option
expected string
}{
"using defaults": {
name: "example",
expected: fmt.Sprintf("%s/%s%s", defaultFixtureDir, "example", defaultFileNameSuffix),
},
"with custom suffix": {
name: "example",
options: []Option{
WithNameSuffix(".txt"),
},
expected: fmt.Sprintf("%s/%s%s", defaultFixtureDir, "example", ".txt"),
},
"with custom fixture dir": {
name: "example",
options: []Option{
WithFixtureDir("fixtures"),
},
expected: fmt.Sprintf("%s/%s%s", "fixtures", "example", defaultFileNameSuffix),
},
"using test name for dir": {
name: "example",
options: []Option{
WithTestNameForDir(true),
},
expected: fmt.Sprintf("%s/%s/%s%s", defaultFixtureDir, t.Name(), "example", defaultFileNameSuffix),
},
"using sub test name for dir": {
name: "example",
options: []Option{
WithSubTestNameForDir(true),
},
expected: fmt.Sprintf("%s/%s/%s%s", defaultFixtureDir, "using_sub_test_name_for_dir", "example", defaultFileNameSuffix),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
g := New(t, test.options...)
assert.Equal(t, test.expected, g.GoldenFileName(t, test.name))
})
}
}
func TestEnsureDir(t *testing.T) {
tests := map[string]struct {
dir string
shouldExist bool
fileExist bool
err interface{}
}{
"with existing directory": {
dir: "example1",
shouldExist: true,
err: nil,
},
"without existing directory": {
dir: "example2",
shouldExist: false,
fileExist: false,
err: nil,
},
"with existing deep directory structure": {
dir: "now/still/works",
shouldExist: true,
err: nil,
},
"error, fixture directory is a file": {
dir: "this/will/not",
shouldExist: false,
fileExist: true,
err: newErrFixtureDirectoryIsFile(filepath.Join(os.TempDir(), "this/will/not")),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
g := New(t)
target := filepath.Join(os.TempDir(), test.dir)
if test.shouldExist {
err := os.MkdirAll(target, g.dirPerms)
assert.Nil(t, err)
}
if test.fileExist {
err := os.MkdirAll(filepath.Dir(target), g.dirPerms)
assert.Nil(t, err)
f, err := os.Create(target)
require.NoError(t, err)
err = f.Close()
assert.Nil(t, err)
}
err := g.ensureDir(target)
assert.Equal(t, test.err, err)
if err != nil {
return
}
s, err := os.Stat(target)
assert.Nil(t, err)
assert.True(t, s.IsDir())
})
}
}
// TODO: This test could use a little <3. It should test some more negative
// cases.
func TestUpdate(t *testing.T) {
tests := map[string]struct {
name string
data []byte
err error
}{
"successful update": {
name: "abc",
data: []byte("some example data"),
err: nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
g := New(t)
err := g.Update(t, test.name, test.data)
assert.Equal(t, test.err, err)
data, err := ioutil.ReadFile(g.GoldenFileName(t, test.name))
assert.Nil(t, err)
assert.Equal(t, test.data, data)
err = os.RemoveAll(g.fixtureDir)
assert.Nil(t, err)
})
}
}
func TestDiffEngines(t *testing.T) {
type engine struct {
engine DiffEngine
diff string
}
tests := map[string]struct {
actual string
expected string
engine engine
}{
"simple": {
actual: "Lorem ipsum dolor.",
expected: "Lorem dolor sit amet.",
engine: engine{
engine: Simple,
diff: `Expected: Lorem dolor sit amet.
Got: Lorem ipsum dolor.`},
},
"classic": {
actual: "Lorem ipsum dolor.",
expected: "Lorem dolor sit amet.",
engine: engine{
engine: ClassicDiff,
diff: `--- Expected
+++ Actual
@@ -1 +1 @@
-Lorem dolor sit amet.
+Lorem ipsum dolor.
`},
},
"colored": {
actual: "Lorem ipsum dolor.",
expected: "Lorem dolor sit amet.",
engine: engine{
engine: ColoredDiff,
diff: "Lorem \x1b[31mipsum \x1b[0mdolor\x1b[32m sit amet\x1b[0m.",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(
t,
test.engine.diff,
Diff(test.engine.engine, test.actual, test.expected),
)
})
}
}
func TestCleanFunction(t *testing.T) {
savedCleanState := *clean
*clean = false
savedUpdateState := *update
*update = true
ts = time.Now()
sampleData := []byte("sample data")
fixtureDir := "test-fixtures"
fixtureSubDirA := fixtureDir + "/a"
fixtureSubDirB := fixtureDir + "/b"
suffix := ".golden"
// The first time running go test, with -update, without -clean
firstTests := []struct {
fixtureDirWithSub string
filePrefix string
}{
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a1"},
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a2"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b1"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b2"},
}
for i, tt := range firstTests {
g := New(t,
WithFixtureDir(tt.fixtureDirWithSub),
WithNameSuffix(suffix),
)
t.Run(fmt.Sprint(i), func(t *testing.T) {
g.Assert(t, tt.filePrefix, sampleData)
})
fullPath := fmt.Sprintf("%s%s",
filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
suffix,
)
_, err := os.Stat(fullPath)
assert.Nil(t, err)
}
*clean = true
ts = time.Now()
// The second time running go test, with -update and -clean
secondTests := []struct {
fixtureDirWithSub string
filePrefix string
}{
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a3"},
{fixtureDirWithSub: fixtureSubDirA, filePrefix: "example-a4"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b3"},
{fixtureDirWithSub: fixtureSubDirB, filePrefix: "example-b4"},
}
for i, tt := range secondTests {
g := New(t,
WithFixtureDir(tt.fixtureDirWithSub),
WithNameSuffix(suffix),
)
t.Run(fmt.Sprint(i), func(t *testing.T) {
g.Assert(t, tt.filePrefix, sampleData)
})
fullPath := fmt.Sprintf("%s%s",
filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
suffix,
)
_, err := os.Stat(fullPath)
assert.Nil(t, err)
}
// make sure output files of the first run doesnt exist
for _, tt := range firstTests {
fullPath := fmt.Sprintf("%s%s",
filepath.Join(tt.fixtureDirWithSub, tt.filePrefix),
suffix,
)
_, err := os.Stat(fullPath)
assert.Error(t, err)
assert.True(t, os.IsNotExist(err))
}
err := os.RemoveAll(fixtureDir)
assert.Nil(t, err)
*clean = savedCleanState
*update = savedUpdateState
}
|