fstest

package
v0.0.0-...-5b5ffaf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	D = Dir()
	F = File()

	// EmptyFS is a shared empty MapFS for tests that need an fs.FS with no files.
	EmptyFS = MapFS{}
)
View Source
var ErrDirNotEmpty = errors.New("directory not empty")

ErrDirNotEmpty is returned by RwMemFS.Remove when the target directory has children.

Functions

This section is empty.

Types

type MapFS

type MapFS = fstest.MapFS

MapFS is an alias for fstest.MapFS for use in tests.

type MapFile

type MapFile = fstest.MapFile

MapFile is an alias for fstest.MapFile for use in tests.

func Dir

func Dir() *MapFile

Dir creates a mock directory for fstest.MapFS.

func File

func File(sL ...string) *MapFile

File creates a mock file for fstest.MapFS.

type MemFS

type MemFS struct {
	fstest.MapFS
	// contains filtered or unexported fields
}

MemFS implements fs.FS and fmt.Stringer — the read-only view of an in-memory filesystem. It wraps testing/fstest.MapFS, whose underlying map is a reference type. Call NewRwMemFS on a MemFS for the read/write view (RwMemFS) of the same underlying map.

func NewMemFS

func NewMemFS(root string, mfs MapFS) MemFS

func (MemFS) GetFullPath

func (mfs MemFS) GetFullPath(relpath string) string

GetFullPath returns relpath joined onto the root identifier, matching osfs.FS's GetFullPath semantics closely enough for test code that treats the two interchangeably — an absolute-looking relpath is returned as-is.

func (MemFS) GetRoot

func (mfs MemFS) GetRoot() string

GetRoot returns the root identifier this MemFS was constructed with — there's no real filesystem root to report, so this is just the string passed to NewMemFS (a testcase name or similar identifier).

func (MemFS) NewRwMemFS

func (mfs MemFS) NewRwMemFS() RwMemFS

NewRwMemFS converts an already-constructed MemFS into its read/write view, sharing the same underlying map — no extra setup, since the map is already a reference type.

func (MemFS) String

func (mfs MemFS) String() string

type RwMemFS

type RwMemFS struct {
	MemFS
}

RwMemFS is MemFS's read/write view: write operations (WriteFile, MkdirAll, Remove, RemoveAll, ...) on top of MemFS's read-only surface. It wraps the same underlying map as the MemFS it was built from — MapFS's underlying map is a reference type, so value-receiver write methods mutate the shared map safely.

func (RwMemFS) Chmod

func (mfs RwMemFS) Chmod(name string, mode fs.FileMode) error

Chmod changes name's permission bits. name must already have an entry in the map.

func (RwMemFS) Create

func (mfs RwMemFS) Create(name string) (filesys.File, error)

Create creates (or truncates) name and returns a writable handle to it. Writes are buffered and committed into the map only on Close.

func (RwMemFS) Mkdir

func (mfs RwMemFS) Mkdir(name string, perm fs.FileMode) error

Mkdir creates the named directory. The parent directory must already exist (explicitly or implicitly), and name must not already exist — mirrors os.Mkdir semantics.

func (RwMemFS) MkdirAll

func (mfs RwMemFS) MkdirAll(name string, perm fs.FileMode) error

MkdirAll inserts explicit directory entries for name and each ancestor that is not already present. MapFS synthesises implicit directories, but explicit entries make Remove/RemoveAll semantics predictable and Stat consistent with real FS behaviour.

func (RwMemFS) MkdirTemp

func (mfs RwMemFS) MkdirTemp(dir, pattern string) (string, error)

MkdirTemp creates a new directory under dir with a name built from pattern (a trailing "*" in pattern is replaced with a random string; with no "*" the random string is appended) and returns its path. There's no real OS temp semantics here — no auto cleanup — it's just a uniquely named directory entry.

func (RwMemFS) Remove

func (mfs RwMemFS) Remove(name string) error

Remove deletes a single file or empty directory. Returns fs.ErrNotExist if name is absent (no explicit entry and no nested entries). Returns ErrDirNotEmpty if nested entries exist under name.

func (RwMemFS) RemoveAll

func (mfs RwMemFS) RemoveAll(name string) error

RemoveAll deletes name and all entries nested under it. A missing name is not an error (matches zxos.RemoveAll semantics).

func (RwMemFS) Rename

func (mfs RwMemFS) Rename(oldName, newName string) error

Rename moves oldName — and, if it's a directory, every entry nested under it — to newName within the map. A real key move, not a copy-and-delete, so it's cheap regardless of file size.

func (RwMemFS) TempDir

func (mfs RwMemFS) TempDir() string

TempDir creates a new, uniquely-named directory in mfs and returns its path — an in-memory analog of testing.T.TempDir(), for test code that needs a MemFS-backed "temp dir" fixture instead of a real one on disk. No error return: unlike a real MkdirTemp, this can't practically fail (no disk-full, no permissions).

func (RwMemFS) WriteFile

func (mfs RwMemFS) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile creates or replaces a file entry in the map. Parent dirs need not exist as explicit entries; MapFS synthesises implicit directories on Open/Stat.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL