stat_test.go 949 B

123456789101112131415161718192021222324252627282930313233343536
  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. package fs_test
  5. import (
  6. "fmt"
  7. . "io/fs"
  8. "testing"
  9. )
  10. type statOnly struct{ StatFS }
  11. func (statOnly) Open(name string) (File, error) { return nil, ErrNotExist }
  12. func TestStat(t *testing.T) {
  13. check := func(desc string, info FileInfo, err error) {
  14. t.Helper()
  15. if err != nil || info == nil || info.Mode() != 0456 {
  16. infoStr := "<nil>"
  17. if info != nil {
  18. infoStr = fmt.Sprintf("FileInfo(Mode: %#o)", info.Mode())
  19. }
  20. t.Fatalf("Stat(%s) = %v, %v, want Mode:0456, nil", desc, infoStr, err)
  21. }
  22. }
  23. // Test that Stat uses the method when present.
  24. info, err := Stat(statOnly{testFsys}, "hello.txt")
  25. check("statOnly", info, err)
  26. // Test that Stat uses Open when the method is not present.
  27. info, err = Stat(openOnly{testFsys}, "hello.txt")
  28. check("openOnly", info, err)
  29. }