]> Cypherpunks repositories - gostls13.git/commitdiff
testing/fstest: test that ReadDirFile on a non-dir fails
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 25 Feb 2021 17:44:35 +0000 (09:44 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 11 Mar 2021 18:25:41 +0000 (18:25 +0000)
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 <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/testing/fstest/testfs.go

index 736bbf0590e981828382eba6deff2dc1e2c755ac..89c5fa19af80959d972a86b80090250e22c2ba17 100644 (file)
@@ -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()