memfs

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package memfs implements an in-memory tool.FileSystem and tool.Workspace (map-backed) for fast, offline FS-tool tests. It enforces the same path-escape rejection and the same Edit read-ledger semantics as the osfs adapter, and performs Grep over the in-memory contents.

memfs has no shell, so its Workspace deliberately does NOT execute commands. For deterministic Bash-tool stubbing it exposes a separate, programmable tool.CommandRunner (see CommandRunner / NewCommandRunner): by default Run returns ErrNoShell so tests cannot accidentally depend on shell behavior, and a canned result may be programmed via SetResult.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoShell = fmt.Errorf("memfs: %w; program a result with SetResult", tool.ErrNoShell)

ErrNoShell is returned by CommandRunner.Run when no canned result has been programmed: memfs has no shell to run commands against. It wraps tool.ErrNoShell so callers can match either sentinel.

View Source
var ErrNotExist = fs.ErrNotExist

ErrNotExist is returned when a file is not present in the in-memory store.

View Source
var ErrPathEscape = errors.New("memfs: path escapes workspace root")

ErrPathEscape is returned when a session-relative path resolves outside the Workspace root.

Functions

This section is empty.

Types

type CommandRunner

type CommandRunner struct {
	// contains filtered or unexported fields
}

CommandRunner is a programmable, in-memory tool.CommandRunner for deterministic Bash-tool stubbing. memfs has no shell, so by default Run returns ErrNoShell; program a canned result (or error) with SetResult.

func NewCommandRunner

func NewCommandRunner() *CommandRunner

NewCommandRunner returns a programmable in-memory CommandRunner. Until SetResult is called, Run returns ErrNoShell.

func (*CommandRunner) Run

Run returns the programmed canned result. memfs has no shell, so absent a programmed result it returns ErrNoShell. It honors ctx cancellation. The command string is ignored beyond being a marker; this method exists for deterministic Bash-tool stubbing, not real execution.

func (*CommandRunner) SetResult

func (r *CommandRunner) SetResult(res *tool.CommandResult, err error)

SetResult programs the deterministic result (and/or error) that the next and subsequent Run calls return. Passing a nil result with a nil error makes Run return an empty successful result; this is the only way to get a non-error Run from memfs, since it has no shell.

type FileSystem

type FileSystem struct {
	// contains filtered or unexported fields
}

FileSystem is an in-memory, map-backed tool.FileSystem. Paths are normalized to clean, slash-separated, root-relative keys. It is safe for concurrent use.

func NewFileSystem

func NewFileSystem(root string) *FileSystem

NewFileSystem returns an empty in-memory FileSystem with the given (logical) root. The root is used only for Root() reporting and escape checks; no real directory is created.

func (*FileSystem) Glob

func (f *FileSystem) Glob(_ context.Context, pattern string) ([]string, error)

Glob returns session-relative paths matching the glob pattern, in deterministic (sorted) order. The pattern supports the "**" globstar (matching across "/" recursively) in addition to "*", "?", "[…]" and "{…}". Matching uses doublestar.Match against each stored key. This mirrors the osfs adapter's globstar support so the in-memory test fabric and the real filesystem agree; the shared fsconformance suite pins both.

func (*FileSystem) Read

func (f *FileSystem) Read(_ context.Context, p string) ([]byte, error)

Read returns the entire contents of the file at the session-relative path.

func (*FileSystem) Root

func (f *FileSystem) Root() string

Root returns the logical workspace root.

func (*FileSystem) Stat

func (f *FileSystem) Stat(_ context.Context, p string) (tool.FileInfo, error)

Stat returns metadata for the file at the session-relative path.

func (*FileSystem) Write

func (f *FileSystem) Write(_ context.Context, p string, data []byte) error

Write replaces the contents of the file at the session-relative path, creating it if needed.

type Workspace

type Workspace struct {
	// contains filtered or unexported fields
}

Workspace is the in-memory session-scoped content seam. Command execution and read-before-write evidence are carried separately by tool.Environment.

func NewWorkspace

func NewWorkspace(root string) *Workspace

NewWorkspace returns an empty in-memory Workspace with the given logical root.

func NewWorkspaceOverFileSystem added in v0.14.0

func NewWorkspaceOverFileSystem(backend *FileSystem) *Workspace

NewWorkspaceOverFileSystem returns a Workspace over the given content backend.

func (*Workspace) AuthorityResourcePath

func (w *Workspace) AuthorityResourcePath(p string) (target, workspace string, err error)

AuthorityResourcePath derives memfs's confined logical path identity. memfs has no symlinks, so its cleaned key is already the physical identity it serves.

func (*Workspace) CreateFile

func (w *Workspace) CreateFile(_ context.Context, p string, data []byte) (tool.FileVersion, error)

CreateFile creates a NEW file at path with the given content, atomically. It fails (wrapping fs.ErrExist) if a file already exists. Parent directories are created as needed (memfs is flat — directories are implicit in the key). It returns the new file's FileVersion.

func (*Workspace) Glob

func (w *Workspace) Glob(ctx context.Context, pattern string) ([]string, error)

Glob returns session-relative paths matching the shell-style pattern.

func (*Workspace) Grep

func (w *Workspace) Grep(ctx context.Context, pattern, pathGlob string) ([]tool.GrepMatch, error)

Grep returns the matches of a regular expression across in-memory files selected by an optional path glob. When pathGlob is empty, all files are searched. Results are returned in deterministic order (by path, then line). The search honors ctx cancellation.

func (*Workspace) Read

func (w *Workspace) Read(ctx context.Context, p string) ([]byte, error)

Read returns the contents of the file at the session-relative path.

func (*Workspace) ReadVersion

func (w *Workspace) ReadVersion(_ context.Context, p string) ([]byte, tool.FileVersion, error)

ReadVersion returns the contents of the file at the session-relative path AND the authoritative FileVersion (sha256 of the content). It reads under the FileSystem's lock so the content and the version are a consistent snapshot.

func (*Workspace) ReplaceFile

func (w *Workspace) ReplaceFile(_ context.Context, p string, old tool.FileVersion, data []byte) (tool.FileVersion, error)

ReplaceFile conditionally replaces the contents of the file at path with data, only if the file's current authoritative version equals old. On a version mismatch it returns a *tool.VersionMismatchError; on a missing file it returns an error wrapping fs.ErrNotExist. It is atomic under the FileSystem's lock.

func (*Workspace) Root

func (w *Workspace) Root() string

Root returns the absolute session root all paths are scoped to.

func (*Workspace) Stat

func (w *Workspace) Stat(ctx context.Context, p string) (tool.FileInfo, error)

Stat returns metadata for the file at the session-relative path.

func (*Workspace) Write

func (w *Workspace) Write(ctx context.Context, p string, data []byte) error

Write is an adapter-public bootstrap operation, deliberately outside tool.Workspace; tools use CreateFile/ReplaceFile instead.

Jump to

Keyboard shortcuts

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