Documentation
¶
Overview ¶
Package mockfs provides a mock filesystem implementation based on testing/fstest.MapFS, allowing for controlled error injection and latency simulation for testing purposes.
Index ¶
- Variables
- func AllOperations() map[Operation]struct{}
- type ErrorConfig
- type ErrorMode
- type MapFile
- type MockFS
- func (m *MockFS) AddCloseError(path string, err error)
- func (m *MockFS) AddDirectory(path string, mode fs.FileMode)
- func (m *MockFS) AddError(op Operation, config *ErrorConfig)
- func (m *MockFS) AddErrorExactMatch(op Operation, path string, err error, mode ErrorMode, successes int)
- func (m *MockFS) AddErrorPattern(op Operation, pattern string, err error, mode ErrorMode, successes int) error
- func (m *MockFS) AddFileBytes(path string, content []byte, mode fs.FileMode)
- func (m *MockFS) AddFileString(path string, content string, mode fs.FileMode)
- func (m *MockFS) AddOpenError(path string, err error)
- func (m *MockFS) AddOpenErrorOnce(path string, err error)
- func (m *MockFS) AddPathError(path string, err error, mode ErrorMode, successes int)
- func (m *MockFS) AddPathErrorPattern(pattern string, err error, mode ErrorMode, successes int) error
- func (m *MockFS) AddReadDirError(path string, err error)
- func (m *MockFS) AddReadError(path string, err error)
- func (m *MockFS) AddReadErrorAfterN(path string, err error, successes int)
- func (m *MockFS) AddStatError(path string, err error)
- func (m *MockFS) AddWriteError(path string, err error)
- func (m *MockFS) ClearErrors()
- func (m *MockFS) GetStats() Stats
- func (m *MockFS) MarkDirectoryNonExistent(dirPath string)
- func (m *MockFS) MarkNonExistent(paths ...string)
- func (m *MockFS) Open(name string) (fs.File, error)
- func (m *MockFS) ReadDir(name string) ([]fs.DirEntry, error)
- func (m *MockFS) ReadFile(name string) ([]byte, error)
- func (m *MockFS) RemovePath(path string)
- func (m *MockFS) ResetStats()
- func (m *MockFS) Stat(name string) (fs.FileInfo, error)
- func (m *MockFS) Sub(dir string) (fs.FS, error)
- type MockFile
- type Operation
- type Option
- type Stats
Constants ¶
This section is empty.
Variables ¶
var ( // Standard fs errors ErrInvalid = fs.ErrInvalid // "invalid argument" ErrPermission = fs.ErrPermission // "permission denied" ErrExist = fs.ErrExist // "file already exists" ErrNotExist = fs.ErrNotExist // "file does not exist" ErrClosed = fs.ErrClosed // "file already closed" // Additional custom errors ErrDiskFull = errors.New("disk full") ErrTimeout = errors.New("operation timeout") ErrCorrupted = errors.New("corrupted data") ErrTooManyHandles = errors.New("too many open files") ErrNotDir = errors.New("not a directory") )
Common errors that can be used with the mock
Functions ¶
func AllOperations ¶
func AllOperations() map[Operation]struct{}
AllOperations returns a map representing all known Operation types. Used for iteration.
Types ¶
type ErrorConfig ¶
type ErrorConfig struct { Error error // The error to return Mode ErrorMode // How the error is applied Counter atomic.Int64 // Counter for behaviors like ErrorModeOnce and ErrorModeAfterSuccesses (using atomic for safety) Matches []string // Exact path matches Patterns []*regexp.Regexp // Pattern matches // contains filtered or unexported fields }
ErrorConfig captures the settings for an error to be injected.
func NewErrorConfig ¶
func NewErrorConfig(err error, mode ErrorMode, counter int, matches []string, patterns []*regexp.Regexp) *ErrorConfig
NewErrorConfig creates a new error configuration pointer. For ErrorModeAfterSuccesses, 'counter' is the number of successes *before* the error. For ErrorModeOnce, 'counter' is ignored.
func (*ErrorConfig) Clone ¶
func (c *ErrorConfig) Clone() *ErrorConfig
Clone creates a deep copy of the ErrorConfig. It ensures atomic values are copied correctly and slices are handled appropriately.
type MapFile ¶
A MapFile describes a single file in a [MapFS]. See testing/fstest.MapFile.
type MockFS ¶
type MockFS struct {
// contains filtered or unexported fields
}
MockFS wraps fstest.MapFS to inject errors for specific paths and operations.
func (*MockFS) AddCloseError ¶
AddCloseError configures a path to return the specified error on Close calls. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) AddDirectory ¶
AddDirectory adds a directory to the mock filesystem. Overwrites if exists.
func (*MockFS) AddError ¶
func (m *MockFS) AddError(op Operation, config *ErrorConfig)
AddError adds an error configuration pointer for a specific operation.
func (*MockFS) AddErrorExactMatch ¶
func (m *MockFS) AddErrorExactMatch(op Operation, path string, err error, mode ErrorMode, successes int)
AddErrorExactMatch adds an error for exact path matches on a specific operation.
func (*MockFS) AddErrorPattern ¶
func (m *MockFS) AddErrorPattern(op Operation, pattern string, err error, mode ErrorMode, successes int) error
AddErrorPattern adds an error for pattern matches on a specific operation.
func (*MockFS) AddFileBytes ¶
AddFileBytes adds a binary file to the mock filesystem. Overwrites if exists.
func (*MockFS) AddFileString ¶
AddFileString adds a text file to the mock filesystem. Overwrites if exists.
func (*MockFS) AddOpenError ¶
AddOpenError configures a path to return the specified error on Open calls. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) AddOpenErrorOnce ¶
AddOpenErrorOnce configures a path to return the specified error on Open once. It is a convenience method for AddErrorExactMatch with ErrorModeOnce mode.
func (*MockFS) AddPathError ¶
AddPathError adds the same error config for *all* operations on an exact path.
func (*MockFS) AddPathErrorPattern ¶
func (m *MockFS) AddPathErrorPattern(pattern string, err error, mode ErrorMode, successes int) error
AddPathErrorPattern adds the same error config for *all* operations matching a pattern.
func (*MockFS) AddReadDirError ¶
AddReadDirError configures a path to return the specified error on ReadDir calls. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) AddReadError ¶
AddReadError configures a path to return the specified error on Read calls. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) AddReadErrorAfterN ¶
AddReadErrorAfterN configures a read error after N successful reads. It is a convenience method for AddErrorExactMatch with ErrorModeAfterSuccesses mode.
func (*MockFS) AddStatError ¶
AddStatError configures a path to return the specified error on Stat. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) AddWriteError ¶
AddWriteError configures a path to return the specified error on Write calls. It is a convenience method for AddErrorExactMatch with ErrorModeAlways mode.
func (*MockFS) ClearErrors ¶
func (m *MockFS) ClearErrors()
ClearErrors removes all configured errors for all operations.
func (*MockFS) MarkDirectoryNonExistent ¶
MarkDirectoryNonExistent removes a directory and all its contents from the map and injects ErrNotExist errors for the directory path and a pattern matching its contents.
func (*MockFS) MarkNonExistent ¶
MarkNonExistent injects ErrNotExist for all operations on the given paths.
func (*MockFS) Open ¶
Open intercepts the Open call and returns configured errors for paths. It implements the fs.FS interface.
func (*MockFS) ReadFile ¶
ReadFile implements the fs.ReadFileFS interface. This implementation tries to use the mock's own methods for consistency.
func (*MockFS) RemovePath ¶
RemovePath removes a file or directory from the mock filesystem. Note: This does not recursively remove directory contents.
func (*MockFS) ResetStats ¶
func (m *MockFS) ResetStats()
ResetStats resets all operation counters to zero.
type MockFile ¶
type MockFile struct {
// contains filtered or unexported fields
}
MockFile wraps fs.File to inject errors and track operations.
type Operation ¶
type Operation int
Operation defines the type of filesystem operation for error injection context.
type Option ¶
type Option func(*MockFS)
Option is a function type for configuring MockFS.
func WithLatency ¶
WithLatency sets the simulated latency for operations.
func WithWriteCallback ¶
WithWriteCallback sets a callback function to handle writes. This allows simulating writes by updating external state or the internal map.
func WithWritesEnabled ¶
func WithWritesEnabled() Option
WithWritesEnabled allows write operations, simulating them by modifying the internal MapFS. Note: This modifies the underlying map, use with caution if the original map data is important.