-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrebed_test.go
136 lines (122 loc) · 3.01 KB
/
rebed_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
package rebed
import (
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"testing"
)
// defer os.Chdir(strings.Repeat("../", len(filepath.SplitList(testDir))))
//go:embed testFS/*
var testFS embed.FS
// this is where filesystems are created
const testDir = "testdata/testdir"
func setup(path string, t *testing.T) {
os.RemoveAll(path)
os.MkdirAll(path, 0777)
}
func TestTree(t *testing.T) {
tDir := filepath.Join(testDir, t.Name())
setup(tDir, t)
defer os.RemoveAll(tDir)
err := Tree(testFS, tDir)
if err != nil {
t.Error(err)
}
// We search our embedded directories and check if our created filesystem has the entries
err = Walk(testFS, ".", func(path string, de fs.DirEntry) error {
pathToCreated := filepath.Join(path, de.Name())
if de.IsDir() {
info, err := os.Stat(pathToCreated)
if os.IsNotExist(err) {
t.Errorf("folder %q not found", pathToCreated)
}
if !info.IsDir() {
t.Errorf("expected a folder %q, got file", pathToCreated)
}
if err != nil {
t.Error(err)
}
}
return nil
})
if err != nil {
t.Error(err)
}
}
func TestTouch(t *testing.T) {
testFileCreation(Touch, t)
}
func TestWrite(t *testing.T) {
testFileCreation(Write, t)
}
func TestPatch(t *testing.T) {
testFileCreation(Patch, t)
}
func TestCreate(t *testing.T) {
testFileCreation(Create, t)
}
func TestCreateError(t *testing.T) {
tDir := filepath.Join(testDir, t.Name())
setup(tDir, t)
defer os.RemoveAll(tDir)
err := Create(testFS, tDir)
if err != nil {
t.Errorf("Create failed with new directory %s", err)
}
err = Create(testFS, tDir)
if err == nil || !os.IsExist(err) {
t.Errorf("Create should have failed during conflicting filesystem creation: %s", err)
}
}
func TestWalkError(t *testing.T) {
theError := fmt.Errorf("oops")
err := Walk(testFS, ".", func(path string, de fs.DirEntry) error {
return theError
})
if err != theError {
t.Errorf("Expected specific error, got %s", err)
}
}
func TestWalkDirError(t *testing.T) {
err := Walk(testFS, "nonexistent", func(path string, de fs.DirEntry) error {
return nil
})
if err == nil {
t.Errorf("expected error while walking non-existent directory")
}
}
func testFileCreation(rebedder func(embed.FS, string) error, t *testing.T) {
tDir := filepath.Join(testDir, t.Name())
setup(tDir, t)
defer os.RemoveAll(tDir)
err := rebedder(testFS, tDir)
if err != nil {
t.Error(err)
}
err = Walk(testFS, ".", func(path string, de fs.DirEntry) error {
pathToCreated := filepath.Join(path, de.Name())
info, err := os.Stat(pathToCreated)
if err != nil {
return err
}
if de.IsDir() != info.IsDir() {
t.Errorf("expected folder/file got file/folder %q", pathToCreated)
}
return nil
})
if err != nil {
t.Error(err)
}
}
func TestEmbedCopyErrors(t *testing.T) {
err := embedCopyToFile(testFS, "nonexistent/File/path", "outputPath")
if err == nil {
t.Error("file should not have existed!")
}
err = embedCopyToFile(testFS, "testFS/file", "nonexistentfolder/file")
if err == nil {
t.Error("copying to a nonexistent folder should error")
}
}