-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
80 lines (72 loc) · 1.73 KB
/
store.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
package golden
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"sync"
)
// Store defines a location and index of golden files.
type Store struct {
RootDir string
IndexFile string
skip int
resetOnce sync.Once
}
// NewStore returns a Store initialized with testdata as RootDir and
// golden.files as IndexFile
func NewStore() *Store {
return &Store{
RootDir: "testdata",
IndexFile: filepath.Join("testdata", "golden.files"),
skip: 3,
}
}
// Load returns body of a golden file based on caller func name.
// Empty if no golden file exists.
func (s *Store) Load() []byte {
_, file := s.filenameFromCaller(s.skip)
body, _ := ioutil.ReadFile(file)
return body
}
func (s *Store) filenameFromCaller(skip int) (filename, file string) {
pc, _, _, _ := runtime.Caller(skip)
fullName := runtime.FuncForPC(pc).Name()
fullName = cleanFilename(fullName)
filename = filepath.Base(fullName)
file = path.Join(s.RootDir, filename)
return
}
// Save writes the given data to RootDir with name from caller func
func (s *Store) Save(t T, data []byte) {
if !*updateGolden {
return
}
t.Helper()
s.resetOnce.Do(s.resetGoldenFiles(t))
filename, file := s.filenameFromCaller(s.skip)
err := ioutil.WriteFile(file, data, 0644)
if err != nil {
t.Fatal(err)
}
// Append the output to list of golden files so it's easy to spot
// when a file should be removed.
flag := os.O_APPEND | os.O_CREATE | os.O_WRONLY
f, err := os.OpenFile(s.IndexFile, flag, 0644)
if err != nil {
t.Fatal(err)
return
}
f.Write([]byte(filename + "\n"))
f.Close()
}
func (s *Store) resetGoldenFiles(t T) func() {
t.Helper()
return func() {
t.Helper()
// No error checking here
os.MkdirAll(s.RootDir, 0755)
os.RemoveAll(s.IndexFile)
}
}