forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_data_folder.go
52 lines (42 loc) · 1.19 KB
/
mock_data_folder.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
package testtools
import (
"bytes"
"github.com/wal-g/wal-g/internal"
"io"
"io/ioutil"
)
type MockDataFolder map[string]*bytes.Buffer
func (folder *MockDataFolder) FileExists(filename string) bool {
_, ok := (*folder)[filename]
return ok
}
func (folder *MockDataFolder) DeleteFile(filename string) error {
delete(*folder, filename)
return nil
}
func (folder *MockDataFolder) CreateFile(filename string) error {
(*folder)[filename] = bytes.NewBuffer(nil)
return nil
}
func (folder *MockDataFolder) CleanFolder() error {
return nil
}
func NewMockDataFolder() *MockDataFolder {
dataFolder := MockDataFolder(make(map[string]*bytes.Buffer))
return &dataFolder
}
func (folder *MockDataFolder) IsEmpty() bool {
return len(*folder) == 0
}
func (folder *MockDataFolder) OpenReadonlyFile(filename string) (io.ReadCloser, error) {
if _, ok := (*folder)[filename]; ok {
return ioutil.NopCloser(bytes.NewReader((*folder)[filename].Bytes())), nil
} else {
return nil, internal.NewNoSuchFileError(filename)
}
}
func (folder *MockDataFolder) OpenWriteOnlyFile(filename string) (io.WriteCloser, error) {
file := bytes.NewBuffer(nil)
(*folder)[filename] = file
return &ReadWriteNopCloser{file}, nil
}