-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockfs_test.go
139 lines (123 loc) · 3.44 KB
/
mockfs_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
137
138
139
package gofs
import (
"fmt"
"io"
"os"
"testing"
)
func testFileExists(t *testing.T, fs FileSystem, file string, expected bool) {
t.Run(fmt.Sprintf("FileExists('%v')", file), func(t *testing.T) {
exists, err := FileExists(fs, file)
if err != nil {
t.Fatalf("%v", err)
}
if exists != expected {
t.Fatalf("Expected %v but got %v", expected, exists)
}
})
}
func testDirExists(t *testing.T, fs FileSystem, dir string, expected bool) {
t.Run(fmt.Sprintf("DirExists('%v')", dir), func(t *testing.T) {
exists, err := DirExists(fs, dir)
if err != nil {
t.Fatalf("%v", err)
}
if exists != expected {
t.Fatalf("expected %v but got %v", expected, exists)
}
})
}
func TestExists(t *testing.T) {
fs := MockFs()
fs.Mkdir("/foo", os.FileMode(0777))
fs.Mkdir("/foo/bar", os.FileMode(0700))
f, _ := fs.Create("/foo/bar/hello")
f.Write([]byte("Hello World"))
f.Chmod(os.FileMode(0400))
f.Close()
testFileExists(t, fs, "/", false)
testFileExists(t, fs, "/bogus", false)
testFileExists(t, fs, "/foo", false)
testFileExists(t, fs, "/foo/bogus", false)
testFileExists(t, fs, "/foo/bar", false)
testFileExists(t, fs, "/foo/bar/bogus", false)
testFileExists(t, fs, "/foo/bar/hello", true)
testFileExists(t, fs, "/foo/bar/hello/bogus", false)
testDirExists(t, fs, "/", true)
testDirExists(t, fs, "/bogus", false)
testDirExists(t, fs, "/foo", true)
testDirExists(t, fs, "/foo/bogus", false)
testDirExists(t, fs, "/foo/bar", true)
testDirExists(t, fs, "/foo/bar/bogus", false)
testDirExists(t, fs, "/foo/bar/hello", false)
testDirExists(t, fs, "/foo/bar/hello/bogus", false)
}
func testOpenFails(t *testing.T, fs FileSystem, path string) {
t.Run(fmt.Sprintf("Open('%v')", path), func(t *testing.T) {
_, err := fs.Open(path)
if err == nil {
t.Fatalf("Expected an error, got nil")
}
})
}
func TestOpen(t *testing.T) {
fs := MockFs()
fs.Mkdir("/foo", os.FileMode(0777))
fs.MkdirAll("/foo/bar/baz", os.FileMode(0700))
f, _ := fs.Create("/foo/bar/baz/hello")
f.Write([]byte("Hello World"))
f.Close()
testOpenFails(t, fs, "/bogus")
testOpenFails(t, fs, "/foo/bogus")
testOpenFails(t, fs, "/foo/bar/bogus")
testOpenFails(t, fs, "/foo/bar/baz/bogus")
t.Run("Open('/foo/bar/baz')", func(t *testing.T) {
f, err := fs.Open("/foo/bar/baz")
if err != nil {
t.Fatalf("Unexpected error from Open: %v", err)
}
defer f.Close()
infos, err := f.Readdir(-1)
if err != nil {
t.Fatalf("Unexpected error from Readdir: %v", err)
}
if len(infos) != 1 {
t.Fatalf("Unexpected number of files: %v", len(infos))
}
if infos[0].Name() != "hello" {
t.Fatalf("Unexpected name: %v", infos[0].Name())
}
})
t.Run("Open('/foo/bar/baz/hello')", func(t *testing.T) {
f, err := fs.Open("/foo/bar/baz/hello")
if err != nil {
t.Fatalf("Unexpected error from Open: %v", err)
}
defer f.Close()
buf := make([]byte, 8)
n, err := f.Read(buf)
if err != nil {
t.Fatalf("Unexpected error from Read: %v", err)
}
if n != 8 {
t.Fatalf("Unexpected length: expected 8 was %v", n)
}
if string(buf) != "Hello Wo" {
t.Fatalf("Unexpected read result: '%v'", string(buf))
}
n, err = f.Read(buf)
if err != nil {
t.Fatalf("Unexpected error from Read: %v", err)
}
if n != 3 {
t.Fatalf("Unexpected length: expected 3 was %v", n)
}
if string(buf[0:3]) != "rld" {
t.Fatalf("Unexpected read result: '%v'", string(buf[0:3]))
}
_, err = f.Read(buf)
if err != io.EOF {
t.Fatalf("Expected EOF, got '%v'", err)
}
})
}