Documentation
¶
Overview ¶
Package mock provides mock implementations for testing file operations and I/O interfaces. It includes mock files, readers, writers, and error scenarios to facilitate unit testing of file-based operations without requiring actual file system access.
Index ¶
- type CloseErrorWriteCloser
- type ErrorFile
- func (f *ErrorFile) Close() error
- func (f *ErrorFile) Read(p []byte) (int, error)
- func (f *ErrorFile) ReadDir(count int) ([]fs.DirEntry, error)
- func (f *ErrorFile) Seek(offset int64, whence int) (int64, error)
- func (f *ErrorFile) Stat() (os.FileInfo, error)
- func (f *ErrorFile) Write(p []byte) (n int, err error)
- type ErrorReadWriteCloser
- type ErrorWriteCloser
- type File
- func (f *File) Bytes() []byte
- func (f *File) Close() error
- func (f *File) Read(p []byte) (int, error)
- func (f *File) ReadDir(count int) ([]fs.DirEntry, error)
- func (f *File) Reset()
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Truncate(n int)
- func (f *File) Write(p []byte) (n int, err error)
- type WriteCloser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CloseErrorWriteCloser ¶
type CloseErrorWriteCloser struct {
// contains filtered or unexported fields
}
CloseErrorWriteCloser is a mock io.WriteCloser where only the Close() method returns an error. This is useful for testing scenarios where writes succeed but closing fails.
func NewCloseErrorWriteCloser ¶
func NewCloseErrorWriteCloser(w io.Writer, err error) *CloseErrorWriteCloser
NewCloseErrorWriteCloser creates a new WriteCloser that writes successfully but returns an error when Close() is called. This simulates partial failure scenarios.
func (*CloseErrorWriteCloser) Close ¶
func (wc *CloseErrorWriteCloser) Close() error
Close always returns the configured error, simulating a close failure while allowing writes to succeed.
func (*CloseErrorWriteCloser) Write ¶
func (wc *CloseErrorWriteCloser) Write(p []byte) (n int, err error)
Write implements the io.Writer interface by delegating to the underlying writer. This method always succeeds, allowing testing of close error scenarios.
type ErrorFile ¶
type ErrorFile struct {
// contains filtered or unexported fields
}
ErrorFile is a mock file implementation that always returns errors. This is useful for testing error handling paths in code that operates on files.
func NewErrorFile ¶
NewErrorFile creates a new error file that will return the specified error for all file operations. This is commonly used to test error scenarios.
func (*ErrorFile) Close ¶
Close always returns the configured error, simulating a file close failure.
func (*ErrorFile) Read ¶
Read always returns the configured error, simulating a file read failure.
func (*ErrorFile) ReadDir ¶
ReadDir always returns the configured error, simulating a directory read failure.
func (*ErrorFile) Seek ¶
Seek always returns the configured error, simulating a file seek failure.
func (*ErrorFile) Stat ¶
Stat always returns the configured error, simulating a file stat failure.
type ErrorReadWriteCloser ¶
type ErrorReadWriteCloser struct {
Err error // The error to return for all read, write, and close operations
}
ErrorReadWriteCloser is a mock that implements io.Reader, io.Writer, and io.Closer interfaces, always returning the specified error for all operations. This is useful for testing scenarios where all I/O operations fail, such as network failures or corrupted streams.
func NewErrorReadWriteCloser ¶
func NewErrorReadWriteCloser(err error) *ErrorReadWriteCloser
NewErrorReadWriteCloser creates a new ErrorReadWriteCloser that will return the specified error for all read, write, and close operations. This mock is particularly useful for testing error handling in streaming operations where all I/O methods need to fail consistently.
func (*ErrorReadWriteCloser) Close ¶
func (e *ErrorReadWriteCloser) Close() error
Close always returns the configured error, simulating a close failure. This method implements the io.Closer interface for consistent error testing.
func (*ErrorReadWriteCloser) Read ¶
func (e *ErrorReadWriteCloser) Read(p []byte) (int, error)
Read always returns the configured error, simulating a read failure. This method implements the io.Reader interface for consistent error testing.
func (*ErrorReadWriteCloser) Write ¶
func (e *ErrorReadWriteCloser) Write(p []byte) (int, error)
Write always returns the configured error, simulating a write failure. This method implements the io.Writer interface for consistent error testing.
type ErrorWriteCloser ¶
type ErrorWriteCloser struct {
// contains filtered or unexported fields
}
ErrorWriteCloser is a mock io.WriteCloser that always returns errors. Useful for testing error handling in code that writes to files or other writers.
func NewErrorWriteCloser ¶
func NewErrorWriteCloser(err error) *ErrorWriteCloser
NewErrorWriteCloser creates a new error WriteCloser that will return the specified error for all write and close operations.
func (*ErrorWriteCloser) Close ¶
func (wc *ErrorWriteCloser) Close() error
Close always returns the configured error, simulating a close failure.
func (*ErrorWriteCloser) Write ¶
func (wc *ErrorWriteCloser) Write(p []byte) (n int, err error)
Write always returns the configured error, simulating a write failure.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is a mock implementation of the fs.File interface for testing purposes. It provides an in-memory file representation that can be used to simulate file operations without touching the actual file system.
func NewFile ¶
NewFile creates a new mock file with the specified data and name. This function is commonly used in tests to create file-like objects that can be passed to functions expecting file interfaces.
func (*File) Bytes ¶
Bytes returns the current file content (for testing)
func (*File) Close ¶
Close implements the io.Closer interface for mock file operations. Marks the file as closed, preventing further read operations.
func (*File) Read ¶
Read implements the io.Reader interface for mock file operations. It reads data from the current position and advances the position accordingly. Returns os.ErrClosed if the file has been closed, or io.EOF when reaching the end.
func (*File) ReadDir ¶
ReadDir implements the fs.ReadDirFile interface. Since this mock represents a regular file, not a directory, it always returns an error.
func (*File) Seek ¶
Seek implements the io.Seeker interface for mock file operations. Allows positioning the file pointer at different locations within the file. Supports seeking from start, current position, or end of file.
func (*File) Stat ¶
Stat returns file information for the mock file. Creates a fileInfo object with the file's name and size based on data length.
func (*File) Truncate ¶
Truncate truncates the file to the specified size
func (*File) Write ¶
Write implements the io.Writer interface for mock file operations. It writes data to the current position and advances the position accordingly. If writing beyond the current data length, the file is extended. Returns os.ErrClosed if the file has been closed.
type WriteCloser ¶
type WriteCloser struct {
// contains filtered or unexported fields
}
WriteCloser is a mock implementation of io.WriteCloser for testing purposes. It wraps an io.Writer and adds close functionality with state tracking.
func NewWriteCloser ¶
func NewWriteCloser(w io.Writer) *WriteCloser
NewWriteCloser creates a new mock WriteCloser that wraps the provided io.Writer. This is useful for testing code that requires both write and close operations.
func (*WriteCloser) Close ¶
func (wc *WriteCloser) Close() error
Close implements the io.Closer interface for the mock WriteCloser. Marks the WriteCloser as closed and prevents further write operations.
func (*WriteCloser) Write ¶
func (wc *WriteCloser) Write(p []byte) (n int, err error)
Write implements the io.Writer interface by delegating to the underlying writer. Returns os.ErrClosed if the WriteCloser has been closed.
Source Files
¶
- file.go