Source file src/io/fs/stat_test.go

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

View as plain text