-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
50 lines (44 loc) · 1.09 KB
/
dir.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
package temptest
import (
"errors"
"fmt"
"io"
"github.com/medtune/go-utils/temp"
)
// FakeDir implements a Directory that is not backed on the
// filesystem. This is useful for testing since the created "files" are
// simple bytes.Buffer that can be inspected.
type FakeDir struct {
Files map[string]*FakeFile
Deleted bool
}
var _ temp.Directory = &FakeDir{}
// NewFile returns a new FakeFile if the filename doesn't exist already.
// This function will fail if the directory has already been deleted.
func (d *FakeDir) NewFile(name string) (io.WriteCloser, error) {
if d.Deleted {
return nil, errors.New("can't create file in deleted FakeDir")
}
if d.Files == nil {
d.Files = map[string]*FakeFile{}
}
f := d.Files[name]
if f != nil {
return nil, fmt.Errorf(
"FakeDir already has file named %q",
name,
)
}
f = &FakeFile{}
d.Files[name] = f
return f, nil
}
// Delete doesn't remove anything, but records that the directory has
// been deleted.
func (d *FakeDir) Delete() error {
if d.Deleted {
return errors.New("failed to re-delete FakeDir")
}
d.Deleted = true
return nil
}