-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_system_test.go
63 lines (51 loc) · 1.71 KB
/
file_system_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
package spectest
import (
"os"
"path/filepath"
"testing"
)
func Test_defaultFileSystem_create(t *testing.T) {
t.Run("should create a file", func(t *testing.T) {
tempDir := os.TempDir()
fs := &defaultFileSystem{}
file, err := fs.create(filepath.Join(tempDir, "test.txt"))
if err != nil {
t.Fatalf("create() error = %v, wantErr %v", err, false)
}
defer file.Close() // nolint
if _, err := os.Stat(filepath.Join(tempDir, "test.txt")); os.IsNotExist(err) {
t.Errorf("create() file does not exist")
}
})
}
func Test_defaultFileSystem_mkdirAll(t *testing.T) {
t.Run("should create a directory", func(t *testing.T) {
tempDir := os.TempDir()
fs := &defaultFileSystem{}
if err := fs.mkdirAll(filepath.Join(tempDir, "test"), 0755); err != nil {
t.Fatalf("mkdirAll() error = %v, wantErr %v", err, false)
}
if _, err := os.Stat(filepath.Join(tempDir, "test")); os.IsNotExist(err) {
t.Errorf("mkdirAll() directory does not exist")
}
})
}
func Test_goldenFile_write(t *testing.T) {
t.Run("should write data to the golden file", func(t *testing.T) {
tempDir := os.TempDir()
g := newGoldenFile(filepath.Join(tempDir, "test.txt"), true, &defaultFileSystem{})
if err := g.write([]byte("test")); err != nil {
t.Fatalf("write() error = %v, wantErr %v", err, false)
}
if _, err := os.Stat(filepath.Join(tempDir, "test.txt")); os.IsNotExist(err) {
t.Errorf("write() file does not exist")
}
})
t.Run("should not write data to the golden file", func(t *testing.T) {
tempDir := os.TempDir()
g := newGoldenFile(filepath.Join(tempDir, "test.txt"), false, &defaultFileSystem{})
if err := g.write([]byte("test")); err == nil {
t.Fatalf("write() error = %v, wantErr %v", err, true)
}
})
}