fstest: add ability for mock objects and filesystems to have hashes

This commit is contained in:
Nick Craig-Wood 2020-06-19 11:02:25 +01:00
parent c820576329
commit 25662b9e05
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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 }