From f098ccf04a33e2e4d2dffa2e90fe77ca8a0fcbb4 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 6 Jul 2020 11:26:26 -0400 Subject: io/fs: add ReadFile and ReadFileFS Add ReadFile helper function, ReadFileFS interface, and test. Add ReadFile method to fstest.MapFS. Add testing of ReadFile method to fstest.TestFS. For #41190. Change-Id: I5b6a41e2e582824e570463b698b635abaa436c32 Reviewed-on: https://go-review.googlesource.com/c/go/+/243912 Trust: Russ Cox Reviewed-by: Rob Pike --- src/testing/fstest/mapfs.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/testing/fstest/mapfs.go') diff --git a/src/testing/fstest/mapfs.go b/src/testing/fstest/mapfs.go index 84a943f409..e969ac2bd1 100644 --- a/src/testing/fstest/mapfs.go +++ b/src/testing/fstest/mapfs.go @@ -108,6 +108,18 @@ func (fsys MapFS) Open(name string) (fs.File, error) { return &mapDir{name, mapFileInfo{elem, file}, list, 0}, nil } +// fsOnly is a wrapper that hides all but the fs.FS methods, +// to avoid an infinite recursion when implementing special +// methods in terms of helpers that would use them. +// (In general, implementing these methods using the package fs helpers +// is redundant and unnecessary, but having the methods may make +// MapFS exercise more code paths when used in tests.) +type fsOnly struct{ fs.FS } + +func (fsys MapFS) ReadFile(name string) ([]byte, error) { + return fs.ReadFile(fsOnly{fsys}, name) +} + // A mapFileInfo implements fs.FileInfo and fs.DirEntry for a given map file. type mapFileInfo struct { name string -- cgit v1.3