-
Notifications
You must be signed in to change notification settings - Fork 2
/
dest_test.go
178 lines (162 loc) · 3.44 KB
/
dest_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package zipx
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestDir_CreateDir_Mode(t *testing.T) {
dir, err := ioutil.TempDir("", "CreateDir_Mode*")
if err != nil {
t.Fatalf("failed to TempDir: %s", err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})
d := Dir(dir)
for _, tc := range []struct {
name string
mode os.FileMode
}{
{"foo", os.FileMode(0755)},
{"bar", os.FileMode(0777)},
} {
err := d.CreateDir(tc.name, DirInfo{Mode: tc.mode})
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, tc.name)
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !fi.Mode().IsDir() {
t.Fatalf("%s is not directory", path)
}
if runtime.GOOS != "windows" {
if m := fi.Mode(); m.Perm() != tc.mode {
t.Fatalf("dir mode mismatch: want=0%o got=0%o", tc.mode, m.Perm())
}
}
}
}
func TestDir_CreateFile_Mode(t *testing.T) {
dir, err := ioutil.TempDir("", "CreateFile_Mode*")
if err != nil {
t.Fatalf("failed to TempDir: %s", err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})
d := Dir(dir)
for _, tc := range []struct {
name string
mode os.FileMode
}{
{"foo", os.FileMode(0755)},
{"bar", os.FileMode(0666)},
{"baz", os.FileMode(0644)},
} {
w, err := d.CreateFile(tc.name, FileInfo{Mode: tc.mode})
if err != nil {
t.Fatal(err)
}
if wc, ok := w.(io.WriteCloser); ok {
wc.Close()
}
path := filepath.Join(dir, tc.name)
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !fi.Mode().IsRegular() {
t.Fatalf("%s is not regular file", path)
}
if runtime.GOOS != "windows" {
if m := fi.Mode(); m.Perm() != tc.mode.Perm() {
t.Fatalf("file mode mismatch: want=0%o got=0%o", tc.mode, m)
}
}
}
}
func TestDir_NoUTF8(t *testing.T) {
dir, err := ioutil.TempDir("", "Dir_NoUTF8*")
if err != nil {
t.Fatalf("failed to TempDir: %s", err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})
d := Dir(dir)
err = d.CreateDir("foo", DirInfo{NonUTF8: true, Mode: 0777})
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "foo")
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !fi.Mode().IsDir() {
t.Fatalf("%s is not directory", path)
}
w, err := d.CreateFile("bar", FileInfo{Mode: 0666, NonUTF8: true})
if err != nil {
t.Fatal(err)
}
if wc, ok := w.(io.WriteCloser); ok {
wc.Close()
}
path = filepath.Join(dir, "bar")
fi, err = os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !fi.Mode().IsRegular() {
t.Fatalf("%s is not regular file", path)
}
}
func TestDiscard(t *testing.T) {
err := Discard.CreateDir("foo", DirInfo{})
if err != nil {
t.Fatal(err)
}
w, err := Discard.CreateFile("bar", FileInfo{})
if err != nil {
t.Fatal(err)
}
if w != ioutil.Discard {
t.Fatal("Discard.CreateFile returns not ioutil.Discard")
}
}
func TestDir_FileWrite(t *testing.T) {
dir, err := ioutil.TempDir("", "Dir_FileWrite*")
if err != nil {
t.Fatalf("failed to TempDir: %s", err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})
d := Dir(dir)
const exp = "The quick brown fox jumps over the lazy dog"
w, err := d.CreateFile("foo.txt", FileInfo{Mode: 0666})
if err != nil {
t.Fatal(err)
}
fmt.Fprint(w, exp)
if wc, ok := w.(io.WriteCloser); ok {
wc.Close()
}
b, err := ioutil.ReadFile(filepath.Join(dir, "foo.txt"))
if err != nil {
t.Fatal(err)
}
act := string(b)
if d := cmp.Diff(exp, act); d != "" {
t.Fatal("content mismatch: -want +got", exp, act)
}
}