Source file src/testing/fstest/testfs_test.go

     1  // Copyright 2021 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 fstest
     6  
     7  import (
     8  	"errors"
     9  	"internal/testenv"
    10  	"io/fs"
    11  	"os"
    12  	"path/filepath"
    13  	"sort"
    14  	"testing"
    15  )
    16  
    17  func TestSymlink(t *testing.T) {
    18  	testenv.MustHaveSymlink(t)
    19  
    20  	tmp := t.TempDir()
    21  	tmpfs := os.DirFS(tmp)
    22  
    23  	if err := os.WriteFile(filepath.Join(tmp, "hello"), []byte("hello, world\n"), 0644); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	if err := os.Symlink(filepath.Join(tmp, "hello"), filepath.Join(tmp, "hello.link")); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	if err := TestFS(tmpfs, "hello", "hello.link"); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  }
    35  
    36  func TestDash(t *testing.T) {
    37  	m := MapFS{
    38  		"a-b/a": {Data: []byte("a-b/a")},
    39  	}
    40  	if err := TestFS(m, "a-b/a"); err != nil {
    41  		t.Error(err)
    42  	}
    43  }
    44  
    45  type shuffledFS MapFS
    46  
    47  func (fsys shuffledFS) Open(name string) (fs.File, error) {
    48  	f, err := MapFS(fsys).Open(name)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return &shuffledFile{File: f}, nil
    53  }
    54  
    55  type shuffledFile struct{ fs.File }
    56  
    57  func (f *shuffledFile) ReadDir(n int) ([]fs.DirEntry, error) {
    58  	dirents, err := f.File.(fs.ReadDirFile).ReadDir(n)
    59  	// Shuffle in a deterministic way, all we care about is making sure that the
    60  	// list of directory entries is not is the lexicographic order.
    61  	//
    62  	// We do this to make sure that the TestFS test suite is not affected by the
    63  	// order of directory entries.
    64  	sort.Slice(dirents, func(i, j int) bool {
    65  		return dirents[i].Name() > dirents[j].Name()
    66  	})
    67  	return dirents, err
    68  }
    69  
    70  func TestShuffledFS(t *testing.T) {
    71  	fsys := shuffledFS{
    72  		"tmp/one":   {Data: []byte("1")},
    73  		"tmp/two":   {Data: []byte("2")},
    74  		"tmp/three": {Data: []byte("3")},
    75  	}
    76  	if err := TestFS(fsys, "tmp/one", "tmp/two", "tmp/three"); err != nil {
    77  		t.Error(err)
    78  	}
    79  }
    80  
    81  // failPermFS is a filesystem that always fails with fs.ErrPermission.
    82  type failPermFS struct{}
    83  
    84  func (f failPermFS) Open(name string) (fs.File, error) {
    85  	if !fs.ValidPath(name) {
    86  		return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
    87  	}
    88  	return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrPermission}
    89  }
    90  
    91  func TestTestFSWrappedErrors(t *testing.T) {
    92  	err := TestFS(failPermFS{})
    93  	if err == nil {
    94  		t.Fatal("error expected")
    95  	}
    96  	t.Logf("Error (expecting wrapped fs.ErrPermission):\n%v", err)
    97  
    98  	if !errors.Is(err, fs.ErrPermission) {
    99  		t.Errorf("error should be a wrapped ErrPermission: %#v", err)
   100  	}
   101  
   102  	// TestFS is expected to return a list of errors.
   103  	// Enforce that the list can be extracted for browsing.
   104  	var errs interface{ Unwrap() []error }
   105  	if !errors.As(err, &errs) {
   106  		t.Errorf("caller should be able to extract the errors as a list: %#v", err)
   107  	} else {
   108  		for _, err := range errs.Unwrap() {
   109  			// ErrPermission is expected
   110  			// but any other error must be reported.
   111  			if !errors.Is(err, fs.ErrPermission) {
   112  				t.Errorf("unexpected error: %v", err)
   113  			}
   114  		}
   115  	}
   116  }
   117  

View as plain text