~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/testing/filetesting/stub.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package filetesting
 
5
 
 
6
import (
 
7
        "bytes"
 
8
        "hash"
 
9
        "io"
 
10
        "os"
 
11
        "strings"
 
12
        "time"
 
13
 
 
14
        "github.com/juju/errors"
 
15
        "github.com/juju/testing"
 
16
)
 
17
 
 
18
type StubReader struct {
 
19
        Stub *testing.Stub
 
20
 
 
21
        ReturnRead io.Reader
 
22
}
 
23
 
 
24
func NewStubReader(stub *testing.Stub, content string) io.Reader {
 
25
        return &StubReader{
 
26
                Stub:       stub,
 
27
                ReturnRead: strings.NewReader(content),
 
28
        }
 
29
}
 
30
 
 
31
func (s *StubReader) Read(data []byte) (int, error) {
 
32
        s.Stub.AddCall("Read", data)
 
33
        if err := s.Stub.NextErr(); err != nil {
 
34
                return 0, errors.Trace(err)
 
35
        }
 
36
 
 
37
        if s.ReturnRead == nil {
 
38
                return 0, nil
 
39
        }
 
40
        return s.ReturnRead.Read(data)
 
41
}
 
42
 
 
43
type StubWriter struct {
 
44
        Stub *testing.Stub
 
45
 
 
46
        ReturnWrite io.Writer
 
47
}
 
48
 
 
49
func NewStubWriter(stub *testing.Stub) (io.Writer, *bytes.Buffer) {
 
50
        buf := new(bytes.Buffer)
 
51
        s := &StubWriter{
 
52
                Stub:        stub,
 
53
                ReturnWrite: buf,
 
54
        }
 
55
        return s, buf
 
56
}
 
57
 
 
58
func (s *StubWriter) Write(data []byte) (int, error) {
 
59
        s.Stub.AddCall("Write", data)
 
60
        if err := s.Stub.NextErr(); err != nil {
 
61
                return 0, errors.Trace(err)
 
62
        }
 
63
 
 
64
        if s.ReturnWrite == nil {
 
65
                return 0, nil
 
66
        }
 
67
        return s.ReturnWrite.Write(data)
 
68
}
 
69
 
 
70
type StubSeeker struct {
 
71
        Stub *testing.Stub
 
72
 
 
73
        ReturnSeek int64
 
74
}
 
75
 
 
76
func (s *StubSeeker) Seek(offset int64, whence int) (int64, error) {
 
77
        s.Stub.AddCall("Seek", offset, whence)
 
78
        if err := s.Stub.NextErr(); err != nil {
 
79
                return 0, errors.Trace(err)
 
80
        }
 
81
 
 
82
        return s.ReturnSeek, nil
 
83
}
 
84
 
 
85
type StubCloser struct {
 
86
        Stub *testing.Stub
 
87
}
 
88
 
 
89
func (s *StubCloser) Close() error {
 
90
        s.Stub.AddCall("Close")
 
91
        if err := s.Stub.NextErr(); err != nil {
 
92
                return errors.Trace(err)
 
93
        }
 
94
 
 
95
        return nil
 
96
}
 
97
 
 
98
type StubFile struct {
 
99
        io.Reader
 
100
        io.Writer
 
101
        io.Seeker
 
102
        io.Closer
 
103
 
 
104
        Stub *testing.Stub
 
105
        Info StubFileInfo
 
106
}
 
107
 
 
108
func NewStubFile(stub *testing.Stub, raw io.ReadWriter) *StubFile {
 
109
        return &StubFile{
 
110
                Reader: &StubReader{Stub: stub, ReturnRead: raw},
 
111
                Writer: &StubWriter{Stub: stub, ReturnWrite: raw},
 
112
                Seeker: &StubSeeker{Stub: stub},
 
113
                Closer: &StubCloser{Stub: stub},
 
114
                Stub:   stub,
 
115
        }
 
116
}
 
117
 
 
118
func (s *StubFile) Name() string {
 
119
        s.Stub.AddCall("Name")
 
120
        s.Stub.NextErr() // Pop one off.
 
121
 
 
122
        return s.Info.Info.Name
 
123
}
 
124
 
 
125
func (s *StubFile) Stat() (os.FileInfo, error) {
 
126
        s.Stub.AddCall("Stat")
 
127
        if err := s.Stub.NextErr(); err != nil {
 
128
                return nil, errors.Trace(err)
 
129
        }
 
130
 
 
131
        return &s.Info, nil
 
132
}
 
133
 
 
134
func (s *StubFile) Sync() error {
 
135
        s.Stub.AddCall("Sync")
 
136
        if err := s.Stub.NextErr(); err != nil {
 
137
                return errors.Trace(err)
 
138
        }
 
139
 
 
140
        return nil
 
141
}
 
142
 
 
143
func (s *StubFile) Truncate(size int64) error {
 
144
        s.Stub.AddCall("Truncate", size)
 
145
        if err := s.Stub.NextErr(); err != nil {
 
146
                return errors.Trace(err)
 
147
        }
 
148
 
 
149
        return nil
 
150
}
 
151
 
 
152
type FileInfo struct {
 
153
        Name    string
 
154
        Size    int64
 
155
        Mode    os.FileMode
 
156
        ModTime time.Time
 
157
}
 
158
 
 
159
var _ os.FileInfo = (*StubFileInfo)(nil)
 
160
 
 
161
type StubFileInfo struct {
 
162
        Stub *testing.Stub
 
163
 
 
164
        Info      FileInfo
 
165
        ReturnSys interface{}
 
166
}
 
167
 
 
168
func NewStubFileInfo(stub *testing.Stub, name, content string) *StubFileInfo {
 
169
        return &StubFileInfo{
 
170
                Stub: stub,
 
171
                Info: FileInfo{
 
172
                        Name:    name,
 
173
                        Size:    int64(len(content)),
 
174
                        Mode:    0644,
 
175
                        ModTime: time.Now(),
 
176
                },
 
177
        }
 
178
}
 
179
 
 
180
func (s StubFileInfo) Name() string {
 
181
        s.Stub.AddCall("Name")
 
182
        s.Stub.NextErr() // Pop one off.
 
183
 
 
184
        return s.Info.Name
 
185
}
 
186
 
 
187
func (s StubFileInfo) Size() int64 {
 
188
        s.Stub.AddCall("Size")
 
189
        s.Stub.NextErr() // Pop one off.
 
190
 
 
191
        return s.Info.Size
 
192
}
 
193
 
 
194
func (s StubFileInfo) Mode() os.FileMode {
 
195
        s.Stub.AddCall("Mode")
 
196
        s.Stub.NextErr() // Pop one off.
 
197
 
 
198
        return s.Info.Mode
 
199
}
 
200
 
 
201
func (s StubFileInfo) ModTime() time.Time {
 
202
        s.Stub.AddCall("ModTime")
 
203
        s.Stub.NextErr() // Pop one off.
 
204
 
 
205
        return s.Info.ModTime
 
206
}
 
207
 
 
208
func (s StubFileInfo) IsDir() bool {
 
209
        s.Stub.AddCall("IsDir")
 
210
        s.Stub.NextErr() // Pop one off.
 
211
 
 
212
        return s.Info.Mode.IsDir()
 
213
}
 
214
 
 
215
func (s StubFileInfo) Sys() interface{} {
 
216
        s.Stub.AddCall("Sys")
 
217
        s.Stub.NextErr() // Pop one off.
 
218
 
 
219
        return s.ReturnSys
 
220
}
 
221
 
 
222
var _ hash.Hash = (*StubHash)(nil)
 
223
 
 
224
type StubHash struct {
 
225
        io.Writer
 
226
 
 
227
        Stub            *testing.Stub
 
228
        ReturnSum       []byte
 
229
        ReturnSize      int
 
230
        ReturnBlockSize int
 
231
}
 
232
 
 
233
func NewStubHash(stub *testing.Stub, raw io.Writer) *StubHash {
 
234
        return &StubHash{
 
235
                Writer: &StubWriter{Stub: stub, ReturnWrite: raw},
 
236
                Stub:   stub,
 
237
        }
 
238
}
 
239
 
 
240
func (s *StubHash) Sum(b []byte) []byte {
 
241
        s.Stub.AddCall("Sum", b)
 
242
        s.Stub.NextErr() // Pop one off.
 
243
 
 
244
        return s.ReturnSum
 
245
}
 
246
 
 
247
func (s *StubHash) Reset() {
 
248
        s.Stub.AddCall("Reset")
 
249
        s.Stub.NextErr() // Pop one off.
 
250
}
 
251
 
 
252
func (s *StubHash) Size() int {
 
253
        s.Stub.AddCall("Size")
 
254
        s.Stub.NextErr() // Pop one off.
 
255
 
 
256
        return s.ReturnSize
 
257
}
 
258
 
 
259
func (s *StubHash) BlockSize() int {
 
260
        s.Stub.AddCall("BlockSize")
 
261
        s.Stub.NextErr() // Pop one off.
 
262
 
 
263
        return s.ReturnBlockSize
 
264
}