-
Notifications
You must be signed in to change notification settings - Fork 0
/
failing_mock_archive.go
82 lines (66 loc) · 1.63 KB
/
failing_mock_archive.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
package historyarchive
import (
"io"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/storage"
)
// FailingMockArchiveBackend is a mocking backend that will fail only when you
// try to read but otherwise behave like MockArchiveBackend.
type FailingMockArchiveBackend struct {
files map[string][]byte
}
func (b *FailingMockArchiveBackend) Exists(pth string) (bool, error) {
_, ok := b.files[pth]
return ok, nil
}
func (b *FailingMockArchiveBackend) Size(pth string) (int64, error) {
f, ok := b.files[pth]
sz := int64(0)
if ok {
sz = int64(len(f))
}
return sz, nil
}
func (b *FailingMockArchiveBackend) GetFile(pth string) (io.ReadCloser, error) {
data, ok := b.files[pth]
if !ok {
return nil, errors.New("file does not exist")
}
fr := FakeReader{}
fr.data = make([]byte, len(data))
copy(fr.data[:], data[:])
return &fr, nil
}
func (b *FailingMockArchiveBackend) PutFile(pth string, in io.ReadCloser) error {
buf, e := io.ReadAll(in)
if e != nil {
return e
}
b.files[pth] = buf
return nil
}
func (b *FailingMockArchiveBackend) ListFiles(pth string) (chan string, chan error) {
return nil, nil
}
func (b *FailingMockArchiveBackend) CanListFiles() bool {
return false
}
func (b *FailingMockArchiveBackend) Close() error {
b.files = make(map[string][]byte)
return nil
}
func makeFailingMockBackend() storage.Storage {
b := new(FailingMockArchiveBackend)
b.Close()
return b
}
type FakeReader struct {
data []byte
}
func (fr *FakeReader) Read(b []byte) (int, error) {
return 0, io.ErrClosedPipe
}
func (fr *FakeReader) Close() error {
return nil
}
var _ io.ReadCloser = &FakeReader{}