From ec8e0a6c584ac17e79b162e1208359e53c69b861 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 13 Aug 2019 13:20:37 +0100 Subject: [PATCH] fstest/mockobject: add SetFs method so it can have a valid Fs() #3419 --- fstest/mockobject/mockobject.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/fstest/mockobject/mockobject.go b/fstest/mockobject/mockobject.go index 095508723..3f55e4d7c 100644 --- a/fstest/mockobject/mockobject.go +++ b/fstest/mockobject/mockobject.go @@ -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)) }