From 25662b9e051fc000431415531a341a8370419d61 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 19 Jun 2020 11:02:25 +0100 Subject: [PATCH] fstest: add ability for mock objects and filesystems to have hashes --- fstest/mockfs/mockfs.go | 8 +++++++- fstest/mockobject/mockobject.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/fstest/mockfs/mockfs.go b/fstest/mockfs/mockfs.go index ed2d1b5e6..b036c57ce 100644 --- a/fstest/mockfs/mockfs.go +++ b/fstest/mockfs/mockfs.go @@ -18,6 +18,7 @@ type Fs struct { root string // The root directory (OS path) features *fs.Features // optional features rootDir fs.DirEntries // directory listing of root + hashes hash.Set // which hashes we support } // ErrNotImplemented is returned by unimplemented methods @@ -66,7 +67,12 @@ func (f *Fs) Precision() time.Duration { // Hashes returns the supported hash types of the filesystem func (f *Fs) Hashes() hash.Set { - return hash.NewHashSet() + return f.hashes +} + +// SetHashes sets the hashes that this supports +func (f *Fs) SetHashes(hashes hash.Set) { + f.hashes = hashes } // Features returns the optional features of this Fs diff --git a/fstest/mockobject/mockobject.go b/fstest/mockobject/mockobject.go index dcf04792d..0d0c4516c 100644 --- a/fstest/mockobject/mockobject.go +++ b/fstest/mockobject/mockobject.go @@ -178,6 +178,20 @@ func (o *ContentMockObject) Size() int64 { return int64(len(o.content)) } +// Hash returns the selected checksum of the file +// If no checksum is available it returns "" +func (o *ContentMockObject) Hash(ctx context.Context, t hash.Type) (string, error) { + hasher, err := hash.NewMultiHasherTypes(hash.NewHashSet(t)) + if err != nil { + return "", err + } + _, err = hasher.Write(o.content) + if err != nil { + return "", err + } + return hasher.Sums()[t], nil +} + type readCloser struct{ io.Reader } func (r *readCloser) Close() error { return nil }