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

View File

@ -93,22 +93,37 @@ const (
// SeekModes contains all valid SeekMode's
var SeekModes = []SeekMode{SeekModeNone, SeekModeRegular, SeekModeRange}
type contentMockObject struct {
// ContentMockObject mocks an fs.Object and has content
type ContentMockObject struct {
Object
content []byte
seekMode SeekMode
f fs.Fs
}
// WithContent returns a fs.Object with the given content.
func (o Object) WithContent(content []byte, mode SeekMode) fs.Object {
return &contentMockObject{
func (o Object) WithContent(content []byte, mode SeekMode) *ContentMockObject {
return &ContentMockObject{
Object: o,
content: content,
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
for _, option := range options {
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())
}
}
func (o *contentMockObject) Size() int64 {
// Size returns the size of the file
func (o *ContentMockObject) Size() int64 {
return int64(len(o.content))
}