From 1853411d8376570295711f9084d494d458822578 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 25 Feb 2021 09:44:35 -0800 Subject: [PATCH] testing/fstest: test that ReadDirFile on a non-dir fails ReadDirFile implementations should return an error for non-directories. Change-Id: I99888562cb6cf829017904ae8c1e8887a416c4cd Reviewed-on: https://go-review.googlesource.com/c/go/+/296391 Trust: Josh Bleecher Snyder Run-TryBot: Josh Bleecher Snyder TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/testing/fstest/testfs.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index 736bbf0590..89c5fa19af 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -119,6 +119,9 @@ func (t *fsTester) openDir(dir string) fs.ReadDirFile { t.errorf("%s: Open: %v", dir, err) return nil } + // It'd be nice to test here that f.Read fails, because f is a directory. + // However, FreeBSD supports calling read on a directory. + // See https://groups.google.com/g/golang-dev/c/rh8jwxyG1PQ. d, ok := f.(fs.ReadDirFile) if !ok { f.Close() @@ -514,6 +517,12 @@ func (t *fsTester) checkFile(file string) { return } + if dir, ok := f.(fs.ReadDirFile); ok { + if _, err := dir.ReadDir(-1); err == nil { + t.errorf("%s: ReadDir of non-dir file should return an error", file) + } + } + data, err := ioutil.ReadAll(f) if err != nil { f.Close() -- 2.50.0