Involved Source Filesmapfs.go Package fstest implements support for testing implementations and users of file systems.
Package-Level Type Names (total 8, in which 2 are exported)
/* sort exporteds by: | */
A MapFile describes a single file in a [MapFS]. // file content or symlink destination // fs.FileInfo.ModTime // fs.FileInfo.Mode // fs.FileInfo.Sys
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, directories, or symbolic links 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.( MapFS) Glob(pattern string) ([]string, error) Lstat returns a FileInfo describing the named file.
If the file is a symbolic link, the returned FileInfo describes the symbolic link.
Lstat makes no attempt to follow the link. Open opens the named file after following any symbolic links.( MapFS) ReadDir(name string) ([]fs.DirEntry, error)( MapFS) ReadFile(name string) ([]byte, error) ReadLink returns the destination of the named symbolic link.( MapFS) Stat(name string) (fs.FileInfo, error)( MapFS) Sub(dir string) (fs.FS, error)( MapFS) lstat(name string) (*mapFileInfo, error)( MapFS) resolveSymlinks(name string) (_ string, ok bool)
MapFS : io/fs.FS
MapFS : io/fs.GlobFS
MapFS : io/fs.ReadDirFS
MapFS : io/fs.ReadFileFS
MapFS : io/fs.ReadLinkFS
MapFS : io/fs.StatFS
MapFS : io/fs.SubFS
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.)FSfs.FS Open opens the named file.
[File.Close] must be called to release any associated resources.
When Open returns an error, it should be of type *PathError
with the Op field set to "open", the Path field set to name,
and the Err field describing the problem.
Open should reject attempts to open names that do not satisfy
ValidPath(name), returning a *PathError with Err set to
ErrInvalid or ErrNotExist.
fsOnly : io/fs.FS
An fsTester holds state for running the test.dirs[]stringerrors[]errorfiles[]stringfsysfs.FS checkBadPath checks that various invalid forms of file's name cannot be opened using open. 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). checkDirList checks that two directory lists contain the same files and file info.
The order of the lists need not match. checkFile checks that basic file reading works correctly.(*fsTester) checkFileRead(file, desc string, data1, data2 []byte) checkGlob checks that various glob patterns work if the file system implements GlobFS. checkOpen validates file opening behavior by attempting to open and then close the given file path. checkStat checks that a direct stat of path matches entry,
which was found in the parent's directory listing. errorf adds an error to the list of errors.(*fsTester) openDir(dir string) fs.ReadDirFile
Package-Level Functions (total 5, in which 1 is exported)
TestFS tests a file system implementation.
It walks the entire tree of files in fsys,
opening and checking that each file behaves correctly.
Symbolic links are not followed,
but their Lstat values are checked
if the file system implements [fs.ReadLinkFS].
It also checks that the file system contains at least the expected files.
As a special case, if no expected files are listed, fsys must be empty.
Otherwise, fsys must contain at least the listed files; it can also contain others.
The contents of fsys must not change concurrently with TestFS.
If TestFS finds any misbehaviors, it returns either the first error or a
list of errors. Use [errors.Is] or [errors.AsType] to inspect.
Typical usage inside a test is:
if err := fstest.TestFS(myFS, "file/that/should/be/present"); err != nil {
t.Fatal(err)
}
formatEntry formats an fs.DirEntry into a string for error messages and comparison.
formatInfo formats an fs.FileInfo into a string for error messages and comparison.
formatInfoEntry formats an fs.FileInfo into a string like the result of formatEntry, for error messages and comparison.