-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdebme_test.go
118 lines (102 loc) · 2.38 KB
/
debme_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
package debme
import (
"embed"
"github.com/leaanthony/slicer"
"github.com/matryer/is"
"io"
"io/fs"
"os"
"testing"
)
//go:embed fixtures
var testfixtures embed.FS
func TestReadFile(t *testing.T) {
is2 := is.New(t)
d, err := FS(testfixtures, "fixtures")
is2.NoErr(err)
file, err := d.ReadFile("test1/onefile.txt")
is2.NoErr(err)
is2.Equal(string(file), "test")
test2, err := d.FS("test2")
is2.NoErr(err)
file, err = test2.ReadFile("inner/deeper/three.txt")
is2.NoErr(err)
is2.Equal(string(file), "3")
_, err = FS(testfixtures, "badfixture")
is2.True(err != nil)
_, err = d.ReadFile("badfile")
is2.True(err != nil)
}
func TestReadDir(t *testing.T) {
is2 := is.New(t)
d, err := FS(testfixtures, "fixtures/test2")
is2.NoErr(err)
files, err := d.ReadDir("inner")
is2.NoErr(err)
is2.Equal(len(files), 3)
expectedFiles := slicer.String([]string{
"deeper",
"one.txt",
"two.txt",
})
for _, file := range files {
is2.True(expectedFiles.Contains(file.Name()))
}
_, err = d.ReadDir("baddir")
is2.True(err != nil)
}
func TestOpen(t *testing.T) {
is2 := is.New(t)
d, err := FS(testfixtures, "fixtures/test1")
is2.NoErr(err)
file, err := d.Open("onefile.txt")
is2.NoErr(err)
data, err := io.ReadAll(file)
is2.NoErr(err)
is2.Equal(string(data), "test")
_, err = d.Open("badfile")
is2.True(err != nil)
}
func TestCompatibility(t *testing.T) {
is2 := is.New(t)
inner, err := FS(testfixtures, "fixtures/test2/inner")
is2.NoErr(err)
expectedFiles := slicer.String([]string{
".",
"deeper",
"deeper/three.txt",
"one.txt",
"two.txt",
})
err = fs.WalkDir(inner, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
is2.True(expectedFiles.Contains(path))
return nil
})
is2.NoErr(err)
}
func TestFS(t *testing.T) {
is2 := is.New(t)
_, err := FS(testfixtures, "baddir")
is2.True(err != nil)
}
func TestCopy(t *testing.T) {
is2 := is.New(t)
inner, err := FS(testfixtures, "fixtures/test2/inner")
is2.NoErr(err)
err = inner.CopyFile("one.txt", "one.txt", 0644)
is2.NoErr(err)
sourceData, err := inner.ReadFile("one.txt")
is2.NoErr(err)
targetData, err := os.ReadFile("one.txt")
is2.NoErr(err)
is2.Equal(sourceData, targetData)
// Bad source file
err = inner.CopyFile("one.txtsd", "one.txt", 0644)
is2.True(err != nil)
// Bad target file
err = inner.CopyFile("one.txt", "/:09/one.txt", 0644)
is2.True(err != nil)
}