fstest/mockobject: add SetFs method so it can have a valid Fs() #3419

This commit is contained in:
Nick Craig-Wood 2019-08-13 13:20:37 +01:00
parent f0e0d6cc3c
commit ec8e0a6c58
1 changed files with 22 additions and 5 deletions

View File

@ -93,22 +93,37 @@ const (
// SeekModes contains all valid SeekMode's // SeekModes contains all valid SeekMode's
var SeekModes = []SeekMode{SeekModeNone, SeekModeRegular, SeekModeRange} var SeekModes = []SeekMode{SeekModeNone, SeekModeRegular, SeekModeRange}
type contentMockObject struct { // ContentMockObject mocks an fs.Object and has content
type ContentMockObject struct {
Object Object
content []byte content []byte
seekMode SeekMode seekMode SeekMode
f fs.Fs
} }
// WithContent returns a fs.Object with the given content. // WithContent returns a fs.Object with the given content.
func (o Object) WithContent(content []byte, mode SeekMode) fs.Object { func (o Object) WithContent(content []byte, mode SeekMode) *ContentMockObject {
return &contentMockObject{ return &ContentMockObject{
Object: o, Object: o,
content: content, content: content,
seekMode: mode, seekMode: mode,
} }
} }
func (o *contentMockObject) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) { // SetFs sets the return value of the Fs() call
func (o *ContentMockObject) SetFs(f fs.Fs) {
o.f = f
}
// Fs returns read only access to the Fs that this object is part of
//
// This is nil unless SetFs has been called
func (o *ContentMockObject) Fs() fs.Info {
return o.f
}
// Open opens the file for read. Call Close() on the returned io.ReadCloser
func (o *ContentMockObject) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
var offset, limit int64 = 0, -1 var offset, limit int64 = 0, -1
for _, option := range options { for _, option := range options {
switch x := option.(type) { switch x := option.(type) {
@ -147,7 +162,9 @@ func (o *contentMockObject) Open(ctx context.Context, options ...fs.OpenOption)
return nil, errors.New(o.seekMode.String()) return nil, errors.New(o.seekMode.String())
} }
} }
func (o *contentMockObject) Size() int64 {
// Size returns the size of the file
func (o *ContentMockObject) Size() int64 {
return int64(len(o.content)) return int64(len(o.content))
} }