Imports #
"io"
"io/fs"
"path"
"slices"
"strings"
"time"
"errors"
"fmt"
"io"
"io/fs"
"maps"
"path"
"slices"
"strings"
"testing/iotest"
"io"
"io/fs"
"path"
"slices"
"strings"
"time"
"errors"
"fmt"
"io"
"io/fs"
"maps"
"path"
"slices"
"strings"
"testing/iotest"
var _ fs.FS = *ast.CallExprvar _ fs.File = *ast.CallExprA MapFS is a simple in-memory file system for use in tests, represented as a map from path names (arguments to Open) to information about the files or directories they represent. The map need not include parent directories for files contained in the map; those will be synthesized if needed. But a directory can still be included by setting the [MapFile.Mode]'s [fs.ModeDir] bit; this may be necessary for detailed control over the directory's [fs.FileInfo] or to create an empty directory. File system operations read directly from the map, so that the file system can be changed by editing the map as needed. An implication is that file system operations must not run concurrently with changes to the map, which would be a race. Another implication is that opening or reading a directory requires iterating over the entire map, so a MapFS should typically be used with not more than a few hundred entries or directory reads.
type MapFS map[string]*MapFileA MapFile describes a single file in a [MapFS].
type MapFile struct {
Data []byte
Mode fs.FileMode
ModTime time.Time
Sys any
}fsOnly is a wrapper that hides all but the fs.FS methods, to avoid an infinite recursion when implementing special methods in terms of helpers that would use them. (In general, implementing these methods using the package fs helpers is redundant and unnecessary, but having the methods may make MapFS exercise more code paths when used in tests.)
type fsOnly struct {
fs.FS
}An fsTester holds state for running the test.
type fsTester struct {
fsys fs.FS
errors []error
dirs []string
files []string
}A mapDir is a directory fs.File (so also an fs.ReadDirFile) open for reading.
type mapDir struct {
path string
mapFileInfo
entry []mapFileInfo
offset int
}A mapFileInfo implements fs.FileInfo and fs.DirEntry for a given map file.
type mapFileInfo struct {
name string
f *MapFile
}type noSub struct {
MapFS
}An openMapFile is a regular (non-directory) fs.File open for reading.
type openMapFile struct {
path string
mapFileInfo
offset int64
}func (d *mapDir) Close() errorfunc (f *openMapFile) Close() errorfunc (fsys MapFS) Glob(pattern string) ([]string, error)func (i *mapFileInfo) Info() (fs.FileInfo, error)func (i *mapFileInfo) IsDir() boolfunc (i *mapFileInfo) ModTime() time.Timefunc (i *mapFileInfo) Mode() fs.FileModefunc (i *mapFileInfo) Name() stringOpen opens the named file.
func (fsys MapFS) Open(name string) (fs.File, error)func (f *openMapFile) Read(b []byte) (int, error)func (d *mapDir) Read(b []byte) (int, error)func (f *openMapFile) ReadAt(b []byte, offset int64) (int, error)func (d *mapDir) ReadDir(count int) ([]fs.DirEntry, error)func (fsys MapFS) ReadDir(name string) ([]fs.DirEntry, error)func (fsys MapFS) ReadFile(name string) ([]byte, error)func (f *openMapFile) Seek(offset int64, whence int) (int64, error)func (i *mapFileInfo) Size() int64func (d *mapDir) Stat() (fs.FileInfo, error)func (fsys MapFS) Stat(name string) (fs.FileInfo, error)func (f *openMapFile) Stat() (fs.FileInfo, error)func (i *mapFileInfo) String() stringfunc (fsys MapFS) Sub(dir string) (fs.FS, error)func (noSub) Sub()func (i *mapFileInfo) Sys() anyfunc (i *mapFileInfo) Type() fs.FileModecheckBadPath checks that various invalid forms of file's name cannot be opened using open.
func (t *fsTester) checkBadPath(file string, desc string, open func(string) error)checkDir checks the directory dir, which is expected to exist (it is either the root or was found in a directory listing with IsDir true).
func (t *fsTester) checkDir(dir string)checkDirList checks that two directory lists contain the same files and file info. The order of the lists need not match.
func (t *fsTester) checkDirList(dir string, desc string, list1 []fs.DirEntry, list2 []fs.DirEntry)checkFile checks that basic file reading works correctly.
func (t *fsTester) checkFile(file string)func (t *fsTester) checkFileRead(file string, desc string, data1 []byte, data2 []byte)checkGlob checks that various glob patterns work if the file system implements GlobFS.
func (t *fsTester) checkGlob(dir string, list []fs.DirEntry)checkOpen validates file opening behavior by attempting to open and then close the given file path.
func (t *fsTester) checkOpen(file string)checkStat checks that a direct stat of path matches entry, which was found in the parent's directory listing.
func (t *fsTester) checkStat(path string, entry fs.DirEntry)errorf adds an error to the list of errors.
func (t *fsTester) errorf(format string, args ...any)formatEntry formats an fs.DirEntry into a string for error messages and comparison.
func formatEntry(entry fs.DirEntry) stringformatInfo formats an fs.FileInfo into a string for error messages and comparison.
func formatInfo(info fs.FileInfo) stringformatInfoEntry formats an fs.FileInfo into a string like the result of formatEntry, for error messages and comparison.
func formatInfoEntry(info fs.FileInfo) stringfunc (t *fsTester) openDir(dir string) fs.ReadDirFilefunc testFS(fsys fs.FS, expected ...string) errorGenerated with Arrow