Source file src/testing/fstest/mapfs_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 fstest
     6  
     7  import (
     8  	"fmt"
     9  	"io/fs"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestMapFS(t *testing.T) {
    15  	m := MapFS{
    16  		"hello":             {Data: []byte("hello, world\n")},
    17  		"fortune/k/ken.txt": {Data: []byte("If a program is too slow, it must have a loop.\n")},
    18  	}
    19  	if err := TestFS(m, "hello", "fortune", "fortune/k", "fortune/k/ken.txt"); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  }
    23  
    24  func TestMapFSChmodDot(t *testing.T) {
    25  	m := MapFS{
    26  		"a/b.txt": &MapFile{Mode: 0666},
    27  		".":       &MapFile{Mode: 0777 | fs.ModeDir},
    28  	}
    29  	buf := new(strings.Builder)
    30  	fs.WalkDir(m, ".", func(path string, d fs.DirEntry, err error) error {
    31  		fi, err := d.Info()
    32  		if err != nil {
    33  			return err
    34  		}
    35  		fmt.Fprintf(buf, "%s: %v\n", path, fi.Mode())
    36  		return nil
    37  	})
    38  	want := `
    39  .: drwxrwxrwx
    40  a: dr-xr-xr-x
    41  a/b.txt: -rw-rw-rw-
    42  `[1:]
    43  	got := buf.String()
    44  	if want != got {
    45  		t.Errorf("MapFS modes want:\n%s\ngot:\n%s\n", want, got)
    46  	}
    47  }
    48  
    49  func TestMapFSFileInfoName(t *testing.T) {
    50  	m := MapFS{
    51  		"path/to/b.txt": &MapFile{},
    52  	}
    53  	info, _ := m.Stat("path/to/b.txt")
    54  	want := "b.txt"
    55  	got := info.Name()
    56  	if want != got {
    57  		t.Errorf("MapFS FileInfo.Name want:\n%s\ngot:\n%s\n", want, got)
    58  	}
    59  }
    60  

View as plain text