File: stub.go

package info (click to toggle)
golang-github-juju-testing 0.0~git20170608.2fe0e88-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 464 kB
  • sloc: makefile: 6
file content (264 lines) | stat: -rw-r--r-- 4,682 bytes parent folder | download
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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package filetesting

import (
	"bytes"
	"hash"
	"io"
	"os"
	"strings"
	"time"

	"github.com/juju/errors"
	"github.com/juju/testing"
)

type StubReader struct {
	Stub *testing.Stub

	ReturnRead io.Reader
}

func NewStubReader(stub *testing.Stub, content string) io.Reader {
	return &StubReader{
		Stub:       stub,
		ReturnRead: strings.NewReader(content),
	}
}

func (s *StubReader) Read(data []byte) (int, error) {
	s.Stub.AddCall("Read", data)
	if err := s.Stub.NextErr(); err != nil {
		return 0, errors.Trace(err)
	}

	if s.ReturnRead == nil {
		return 0, nil
	}
	return s.ReturnRead.Read(data)
}

type StubWriter struct {
	Stub *testing.Stub

	ReturnWrite io.Writer
}

func NewStubWriter(stub *testing.Stub) (io.Writer, *bytes.Buffer) {
	buf := new(bytes.Buffer)
	s := &StubWriter{
		Stub:        stub,
		ReturnWrite: buf,
	}
	return s, buf
}

func (s *StubWriter) Write(data []byte) (int, error) {
	s.Stub.AddCall("Write", data)
	if err := s.Stub.NextErr(); err != nil {
		return 0, errors.Trace(err)
	}

	if s.ReturnWrite == nil {
		return 0, nil
	}
	return s.ReturnWrite.Write(data)
}

type StubSeeker struct {
	Stub *testing.Stub

	ReturnSeek int64
}

func (s *StubSeeker) Seek(offset int64, whence int) (int64, error) {
	s.Stub.AddCall("Seek", offset, whence)
	if err := s.Stub.NextErr(); err != nil {
		return 0, errors.Trace(err)
	}

	return s.ReturnSeek, nil
}

type StubCloser struct {
	Stub *testing.Stub
}

func (s *StubCloser) Close() error {
	s.Stub.AddCall("Close")
	if err := s.Stub.NextErr(); err != nil {
		return errors.Trace(err)
	}

	return nil
}

type StubFile struct {
	io.Reader
	io.Writer
	io.Seeker
	io.Closer

	Stub *testing.Stub
	Info StubFileInfo
}

func NewStubFile(stub *testing.Stub, raw io.ReadWriter) *StubFile {
	return &StubFile{
		Reader: &StubReader{Stub: stub, ReturnRead: raw},
		Writer: &StubWriter{Stub: stub, ReturnWrite: raw},
		Seeker: &StubSeeker{Stub: stub},
		Closer: &StubCloser{Stub: stub},
		Stub:   stub,
	}
}

func (s *StubFile) Name() string {
	s.Stub.AddCall("Name")
	s.Stub.NextErr() // Pop one off.

	return s.Info.Info.Name
}

func (s *StubFile) Stat() (os.FileInfo, error) {
	s.Stub.AddCall("Stat")
	if err := s.Stub.NextErr(); err != nil {
		return nil, errors.Trace(err)
	}

	return &s.Info, nil
}

func (s *StubFile) Sync() error {
	s.Stub.AddCall("Sync")
	if err := s.Stub.NextErr(); err != nil {
		return errors.Trace(err)
	}

	return nil
}

func (s *StubFile) Truncate(size int64) error {
	s.Stub.AddCall("Truncate", size)
	if err := s.Stub.NextErr(); err != nil {
		return errors.Trace(err)
	}

	return nil
}

type FileInfo struct {
	Name    string
	Size    int64
	Mode    os.FileMode
	ModTime time.Time
}

var _ os.FileInfo = (*StubFileInfo)(nil)

type StubFileInfo struct {
	Stub *testing.Stub

	Info      FileInfo
	ReturnSys interface{}
}

func NewStubFileInfo(stub *testing.Stub, name, content string) *StubFileInfo {
	return &StubFileInfo{
		Stub: stub,
		Info: FileInfo{
			Name:    name,
			Size:    int64(len(content)),
			Mode:    0644,
			ModTime: time.Now(),
		},
	}
}

func (s StubFileInfo) Name() string {
	s.Stub.AddCall("Name")
	s.Stub.NextErr() // Pop one off.

	return s.Info.Name
}

func (s StubFileInfo) Size() int64 {
	s.Stub.AddCall("Size")
	s.Stub.NextErr() // Pop one off.

	return s.Info.Size
}

func (s StubFileInfo) Mode() os.FileMode {
	s.Stub.AddCall("Mode")
	s.Stub.NextErr() // Pop one off.

	return s.Info.Mode
}

func (s StubFileInfo) ModTime() time.Time {
	s.Stub.AddCall("ModTime")
	s.Stub.NextErr() // Pop one off.

	return s.Info.ModTime
}

func (s StubFileInfo) IsDir() bool {
	s.Stub.AddCall("IsDir")
	s.Stub.NextErr() // Pop one off.

	return s.Info.Mode.IsDir()
}

func (s StubFileInfo) Sys() interface{} {
	s.Stub.AddCall("Sys")
	s.Stub.NextErr() // Pop one off.

	return s.ReturnSys
}

var _ hash.Hash = (*StubHash)(nil)

type StubHash struct {
	io.Writer

	Stub            *testing.Stub
	ReturnSum       []byte
	ReturnSize      int
	ReturnBlockSize int
}

func NewStubHash(stub *testing.Stub, raw io.Writer) *StubHash {
	return &StubHash{
		Writer: &StubWriter{Stub: stub, ReturnWrite: raw},
		Stub:   stub,
	}
}

func (s *StubHash) Sum(b []byte) []byte {
	s.Stub.AddCall("Sum", b)
	s.Stub.NextErr() // Pop one off.

	return s.ReturnSum
}

func (s *StubHash) Reset() {
	s.Stub.AddCall("Reset")
	s.Stub.NextErr() // Pop one off.
}

func (s *StubHash) Size() int {
	s.Stub.AddCall("Size")
	s.Stub.NextErr() // Pop one off.

	return s.ReturnSize
}

func (s *StubHash) BlockSize() int {
	s.Stub.AddCall("BlockSize")
	s.Stub.NextErr() // Pop one off.

	return s.ReturnBlockSize
}