Newer
Older
"gitlab.com/golang-utils/spectest"
)
/*
// Writeable is an interface for a filesystem, that expands on the Minimal interface
// and adds the ability to write files and folders.
type Writeable interface {
Minimal
// Write writes either a file or a folder to the given path with default permissions.
// If p is a directory, data is ignored.
// If recursive is set to true, all intermediate directories will be created if necessary.
// Write always overwrites an existing file. If you need to make sure that a file at the given
// path should not be overwritten, check with Exists(p) first.
// If p is a directory that already exists, nothing will be done and no error will be returned.
Write(p path.Relative, data io.ReadCloser, recursive bool) error
}
*/
func ExtWriteable(c Config, fn func(path.Absolute) fs.ExtWriteable) *spectest.Spec {
spec := spectest.NewSpec("ExtWriteable", "test spec for the ExtWriteable interface")
write := spectest.NewSpec("Write", "Write writes either a file or a folder to the given path with default permissions.")
write.AddTest("write file", tests.testWriteFile)
write.AddTest("overwrite file", func(t *testing.T) {
})
write.AddTest("write file recursive", func(t *testing.T) {
})
write.AddTest("write dir", func(t *testing.T) {
})
write.AddTest("overwrite dir", func(t *testing.T) {
})
type writeable struct {
conf Config
fn func(path.Absolute) fs.ExtWriteable
}
func (fstest *writeable) testMkdirAll(t *testing.T) {
// fmt.Println(path.MustWD().Join("tests/"))
if fstest.conf.isLocal {
os.RemoveAll("tests")
os.Mkdir("tests", 0755)
}
tests := []struct {
forlocal bool
fsroot path.Absolute
dir string
}{
{false, path.MustLocal("C:"), "testdir_c/d/e/"},
{false, path.MustLocal("C:"), "testdir_a/b/c/"},
{true, path.MustWD().Join("tests/"), "testdir_b/g/f/"},
{true, path.MustWD().Join("tests/"), "testdir_b/g/f/"},
{true, path.MustWD().Join("tests/with space/"), "testdir_b/g/f/"},
{true, path.MustWD().Join("tests/with space/"), "testdir b/a b/f/"},
{false, path.MustLocal("C:/fh h/"), "testdir c/g a/f/"},
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
}
for i, test := range tests {
if fstest.conf.isLocal && !test.forlocal {
continue
}
fsys := fstest.fn(test.fsroot)
if fsys == nil {
continue
}
rel := path.Relative(test.dir)
if !path.IsDir(rel) {
t.Errorf("[%v] %T(%q): %q is not a dir", i, fsys, test.fsroot, test.dir)
}
err := fs.MkDirAll(fsys, rel)
if err != nil {
t.Fatalf("[%v] %T(%q): could not create dir %q: %v", i, fsys, test.fsroot, test.dir, err)
}
if !fsys.Exists(rel) {
t.Errorf("[%v] %T(%q): dir %q not created", i, fsys, test.fsroot, test.dir)
}
}
}
func (fstest *writeable) testWriteFile(t *testing.T) {
if fstest.conf.Unsupported.WritingFile {
t.Skip()
return
}
os.RemoveAll("tests")
os.Mkdir("tests", 0755)
tests := []struct {
forLocal bool
fsroot path.Absolute
fileWrite string
data string
fileRead string
}{
{false, path.MustLocal("/memory/"), "testdir_a/b/c", "content", "testdir_a/b/c"},
{false, path.MustLocal("/map/"), "testdir_c/d/e", "content", "testdir_c/d/e"},
{true, path.MustWD().Join("tests/"), "f", "content", "f"},
{true, path.MustWD().Join("tests/"), "testdir_b/g/f", "content", "testdir_b/g/f"},
{true, path.MustWD().Join("tests/with space/"), "testdir_b/g/f", "content", "testdir_b/g/f"},
{false, path.MustLocal("C:/fh h/"), "testdir c/g a/f", "content", "testdir c/g a/f"},
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//{"Dir-schon vorhanden", Dir("tests"), "testdir_b/g/f"},
}
for i, test := range tests {
if fstest.conf.isLocal && !test.forLocal {
continue
}
fsys := fstest.fn(test.fsroot)
if fsys == nil {
continue
}
file := path.Relative(test.fileWrite)
dir := path.Dir(file).Relative()
if dir.String() != "" && !fsys.Exists(dir) {
err := fs.MkDirAll(fsys, dir)
if err != nil {
t.Fatalf("[%v] %T(%q): could not create dir %q: %v", i, fsys, test.fsroot, dir, err)
}
}
rd := strings.NewReader(test.data)
err := fsys.Write(file, fs.ReadCloser(rd), false)
// fmt.Printf("writing file %q\n", file)
if err != nil {
t.Fatalf("[%v] %T(%q): could not write file %q: %T %v", i, fsys, test.fsroot, test.fileWrite, err, err)
}
if !fsys.Exists(file) {
t.Errorf("[%v] %T(%q): file %q not created", i, fsys, test.fsroot, test.fileWrite)
}
if path.IsDir(file) {
t.Errorf("[%v] %T(%q): %q is a dir and not a file", i, fsys, test.fsroot, test.fileWrite)
}
file = path.Relative(test.fileRead)
if !fsys.Exists(file) {
t.Errorf("[%v] %T(%q): file %q not there", i, fsys, test.fsroot, test.fileRead)
}
if path.IsDir(file) {
t.Errorf("[%v] %T(%q): %q is a dir and not a file", i, fsys, test.fsroot, test.fileRead)
}
rd2, err := fsys.Reader(file)
if err != nil {
t.Fatalf("[%v] %T(%q): could not read from file %q: %v", i, fsys, test.fsroot, test.fileRead, err)
}
bt, err := io.ReadAll(rd2)
if err != nil {
t.Fatalf("[%v] %T(%q): could not readall from file %q: %v", i, fsys, test.fsroot, test.fileRead, err)
}
if got, want := string(bt), test.data; got != want {
t.Errorf("[%v] %T(%q) content of %q = %q // expected %q", i, fsys, test.fsroot, test.fileRead, got, want)
}
}
}