-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_system.go
75 lines (63 loc) · 1.79 KB
/
file_system.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
package spectest
import (
"errors"
"os"
"path/filepath"
)
// fileSystem interface to abstract file system operations
type fileSystem interface {
// create creates a file at the given path
create(name string) (*os.File, error)
// mkdirAll creates a directory at the given path
mkdirAll(path string, perm os.FileMode) error
}
// defaultFileSystem is the default implementation of the fileSystem interface
type defaultFileSystem struct{}
// create creates a file at the given path
func (r *defaultFileSystem) create(name string) (*os.File, error) {
return os.Create(filepath.Clean(name))
}
// mkdirAll creates a directory at the given path
func (r *defaultFileSystem) mkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
// goldenFile is a file that is used to store the golden data.
type goldenFile struct {
// path is the path to the golden file
path string
// update is a flag that indicates if the golden file should be updated
update bool
// fs is the file system used to create the golden file
fs fileSystem
}
// newGoldenFile creates a new golden file
func newGoldenFile(path string, update bool, fs fileSystem) *goldenFile {
return &goldenFile{
path: path,
update: update,
fs: fs,
}
}
// read reads the golden file.
func (g *goldenFile) read() ([]byte, error) {
return os.ReadFile(filepath.Clean(g.path))
}
// write writes the given data to the golden file.
func (g *goldenFile) write(data []byte) error {
if !g.update {
return errors.New("golden file update is disabled")
}
dir := filepath.Dir(g.path)
if err := g.fs.mkdirAll(filepath.Clean(dir), os.ModePerm); err != nil {
return err
}
f, err := g.fs.create(g.path)
if err != nil {
return err
}
defer f.Close() // nolint
if _, err := f.Write(data); err != nil {
return err
}
return nil
}