fstest

Imports

Imports #

"io"
"io/fs"
"path"
"slices"
"strings"
"time"
"errors"
"fmt"
"io"
"io/fs"
"maps"
"path"
"slices"
"strings"
"testing/iotest"

Constants & Variables

_ var #

var _ fs.FS = *ast.CallExpr

_ var #

var _ fs.File = *ast.CallExpr

Type Aliases

MapFS type #

A 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]*MapFile

Structs

MapFile struct #

A MapFile describes a single file in a [MapFS].

type MapFile struct {
Data []byte
Mode fs.FileMode
ModTime time.Time
Sys any
}

fsOnly struct #

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
}

fsTester struct #

An fsTester holds state for running the test.

type fsTester struct {
fsys fs.FS
errors []error
dirs []string
files []string
}

mapDir struct #

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
}

mapFileInfo struct #

A mapFileInfo implements fs.FileInfo and fs.DirEntry for a given map file.

type mapFileInfo struct {
name string
f *MapFile
}

noSub struct #

type noSub struct {
MapFS
}

openMapFile struct #

An openMapFile is a regular (non-directory) fs.File open for reading.

type openMapFile struct {
path string
mapFileInfo
offset int64
}

Functions

Close method #

func (d *mapDir) Close() error

Close method #

func (f *openMapFile) Close() error

Glob method #

func (fsys MapFS) Glob(pattern string) ([]string, error)

Info method #

func (i *mapFileInfo) Info() (fs.FileInfo, error)

IsDir method #

func (i *mapFileInfo) IsDir() bool

ModTime method #

func (i *mapFileInfo) ModTime() time.Time

Mode method #

func (i *mapFileInfo) Mode() fs.FileMode

Name method #

func (i *mapFileInfo) Name() string

Open method #

Open opens the named file.

func (fsys MapFS) Open(name string) (fs.File, error)

Read method #

func (f *openMapFile) Read(b []byte) (int, error)

Read method #

func (d *mapDir) Read(b []byte) (int, error)

ReadAt method #

func (f *openMapFile) ReadAt(b []byte, offset int64) (int, error)

ReadDir method #

func (d *mapDir) ReadDir(count int) ([]fs.DirEntry, error)

ReadDir method #

func (fsys MapFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadFile method #

func (fsys MapFS) ReadFile(name string) ([]byte, error)

Seek method #

func (f *openMapFile) Seek(offset int64, whence int) (int64, error)

Size method #

func (i *mapFileInfo) Size() int64

Stat method #

func (d *mapDir) Stat() (fs.FileInfo, error)

Stat method #

func (fsys MapFS) Stat(name string) (fs.FileInfo, error)

Stat method #

func (f *openMapFile) Stat() (fs.FileInfo, error)

String method #

func (i *mapFileInfo) String() string

Sub method #

func (fsys MapFS) Sub(dir string) (fs.FS, error)

Sub method #

func (noSub) Sub()

Sys method #

func (i *mapFileInfo) Sys() any

Type method #

func (i *mapFileInfo) Type() fs.FileMode

checkBadPath method #

checkBadPath 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 method #

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 method #

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 method #

checkFile checks that basic file reading works correctly.

func (t *fsTester) checkFile(file string)

checkFileRead method #

func (t *fsTester) checkFileRead(file string, desc string, data1 []byte, data2 []byte)

checkGlob method #

checkGlob checks that various glob patterns work if the file system implements GlobFS.

func (t *fsTester) checkGlob(dir string, list []fs.DirEntry)

checkOpen method #

checkOpen validates file opening behavior by attempting to open and then close the given file path.

func (t *fsTester) checkOpen(file string)

checkStat method #

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 method #

errorf adds an error to the list of errors.

func (t *fsTester) errorf(format string, args ...any)

formatEntry function #

formatEntry formats an fs.DirEntry into a string for error messages and comparison.

func formatEntry(entry fs.DirEntry) string

formatInfo function #

formatInfo formats an fs.FileInfo into a string for error messages and comparison.

func formatInfo(info fs.FileInfo) string

formatInfoEntry function #

formatInfoEntry formats an fs.FileInfo into a string like the result of formatEntry, for error messages and comparison.

func formatInfoEntry(info fs.FileInfo) string

openDir method #

func (t *fsTester) openDir(dir string) fs.ReadDirFile

testFS function #

func testFS(fsys fs.FS, expected ...string) error

Generated with Arrow