github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/io/fs/readdir_test.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fs_test
     6  
     7  import (
     8  	. "io/fs"
     9  	"testing"
    10  )
    11  
    12  type readDirOnly struct{ ReadDirFS }
    13  
    14  func (readDirOnly) Open(name string) (File, error) { return nil, ErrNotExist }
    15  
    16  func TestReadDir(t *testing.T) {
    17  	check := func(desc string, dirs []DirEntry, err error) {
    18  		t.Helper()
    19  		if err != nil || len(dirs) != 2 || dirs[0].Name() != "hello.txt" || dirs[1].Name() != "sub" {
    20  			var names []string
    21  			for _, d := range dirs {
    22  				names = append(names, d.Name())
    23  			}
    24  			t.Errorf("ReadDir(%s) = %v, %v, want %v, nil", desc, names, err, []string{"hello.txt", "sub"})
    25  		}
    26  	}
    27  
    28  	// Test that ReadDir uses the method when present.
    29  	dirs, err := ReadDir(readDirOnly{testFsys}, ".")
    30  	check("readDirOnly", dirs, err)
    31  
    32  	// Test that ReadDir uses Open when the method is not present.
    33  	dirs, err = ReadDir(openOnly{testFsys}, ".")
    34  	check("openOnly", dirs, err)
    35  
    36  	// Test that ReadDir on Sub of . works (sub_test checks non-trivial subs).
    37  	sub, err := Sub(testFsys, ".")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	dirs, err = ReadDir(sub, ".")
    42  	check("sub(.)", dirs, err)
    43  }