diff options
| author | Russ Cox <rsc@golang.org> | 2020-07-06 11:26:26 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2020-10-20 17:52:55 +0000 |
| commit | f098ccf04a33e2e4d2dffa2e90fe77ca8a0fcbb4 (patch) | |
| tree | a478de6a5075d156a273bda1614bd506e13664d3 /src/testing/fstest/testfs.go | |
| parent | b1f76f7a220a806d74bf55da374ea89467753e1f (diff) | |
| download | go-f098ccf04a33e2e4d2dffa2e90fe77ca8a0fcbb4.tar.xz | |
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 <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/testing/fstest/testfs.go')
| -rw-r--r-- | src/testing/fstest/testfs.go | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index 2bb2120c19..66725ca2a4 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -310,6 +310,27 @@ func (t *fsTester) checkFile(file string) { // The return value doesn't matter. f.Close() + // Check that ReadFile works if present. + if fsys, ok := t.fsys.(fs.ReadFileFS); ok { + data2, err := fsys.ReadFile(file) + if err != nil { + t.errorf("%s: fsys.ReadFile: %v", file, err) + return + } + t.checkFileRead(file, "ReadAll vs fsys.ReadFile", data, data2) + + t.checkBadPath(file, "ReadFile", + func(name string) error { _, err := fsys.ReadFile(name); return err }) + } + + // Check that fs.ReadFile works with t.fsys. + data2, err := fs.ReadFile(t.fsys, file) + if err != nil { + t.errorf("%s: fs.ReadFile: %v", file, err) + return + } + t.checkFileRead(file, "ReadAll vs fs.ReadFile", data, data2) + // Use iotest.TestReader to check small reads, Seek, ReadAt. f, err = t.fsys.Open(file) if err != nil { @@ -329,8 +350,19 @@ func (t *fsTester) checkFileRead(file, desc string, data1, data2 []byte) { } } -// checkOpen checks that various invalid forms of file's name cannot be opened. +// checkBadPath checks that various invalid forms of file's name cannot be opened using t.fsys.Open. func (t *fsTester) checkOpen(file string) { + t.checkBadPath(file, "Open", func(file string) error { + f, err := t.fsys.Open(file) + if err == nil { + f.Close() + } + return err + }) +} + +// checkBadPath checks that various invalid forms of file's name cannot be opened using open. +func (t *fsTester) checkBadPath(file string, desc string, open func(string) error) { bad := []string{ "/" + file, file + "/.", @@ -356,9 +388,8 @@ func (t *fsTester) checkOpen(file string) { } for _, b := range bad { - if f, err := t.fsys.Open(b); err == nil { - f.Close() - t.errorf("%s: Open(%s) succeeded, want error", file, b) + if err := open(b); err == nil { + t.errorf("%s: %s(%s) succeeded, want error", file, desc, b) } } } |
