mock

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package mock provides mock implementations for testing file system, hash, and I/O operations. It enables isolated testing by simulating various scenarios including error conditions.

Index

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 (c *CloseErrorWriteCloser) Close() error

Close always returns the configured error, simulating a close failure while allowing writes to succeed.

func (*CloseErrorWriteCloser) Write

func (c *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

func NewErrorFile(err error) *ErrorFile

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

func (e *ErrorFile) Close() error

Close always returns the configured error, simulating a file close failure.

func (*ErrorFile) Read

func (e *ErrorFile) Read(p []byte) (int, error)

Read always returns the configured error, simulating a file read failure.

func (*ErrorFile) ReadDir

func (e *ErrorFile) ReadDir(count int) ([]fs.DirEntry, error)

ReadDir always returns the configured error, simulating a directory read failure.

func (*ErrorFile) Seek

func (e *ErrorFile) Seek(offset int64, whence int) (int64, error)

Seek always returns the configured error, simulating a file seek failure.

func (*ErrorFile) Stat

func (e *ErrorFile) Stat() (os.FileInfo, error)

Stat always returns the configured error, simulating a file stat failure.

func (*ErrorFile) Write

func (e *ErrorFile) Write(p []byte) (n int, err error)

Write always returns the configured error, simulating a file write failure.

type ErrorHasher added in v1.1.1

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

ErrorHasher is a mock implementation of hash.Hash that can return errors on Write operations. This is useful for testing error handling in code that uses hash.Hash interfaces.

func NewErrorHasher added in v1.1.1

func NewErrorHasher(writeErr error) *ErrorHasher

NewErrorHasher creates a new ErrorHasher that will return the specified error when Write() is called. This is useful for testing hash write error scenarios.

func (*ErrorHasher) BlockSize added in v1.1.1

func (h *ErrorHasher) BlockSize() int

BlockSize implements the hash.Hash interface and returns a mock block size.

func (*ErrorHasher) Reset added in v1.1.1

func (h *ErrorHasher) Reset()

Reset implements the hash.Hash interface but does nothing in this mock.

func (*ErrorHasher) Size added in v1.1.1

func (h *ErrorHasher) Size() int

Size implements the hash.Hash interface and returns a mock hash size.

func (*ErrorHasher) Sum added in v1.1.1

func (h *ErrorHasher) Sum(b []byte) []byte

Sum implements the hash.Hash interface and returns a mock hash value. This always succeeds and returns a unique mock hash for testing.

func (*ErrorHasher) Write added in v1.1.1

func (h *ErrorHasher) Write(p []byte) (n int, err error)

Write implements the hash.Hash interface and returns the configured error. This simulates a hash write failure for testing purposes.

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 (e *ErrorWriteCloser) Close() error

Close always returns the configured error, simulating a close failure.

func (*ErrorWriteCloser) Write

func (e *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

func NewFile(data []byte, name string) *File

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

func (f *File) Bytes() []byte

Bytes returns the current file content (for testing)

func (*File) Close

func (f *File) Close() error

Close implements the io.Closer interface for mock file operations. Marks the file as closed, preventing further read operations.

func (*File) Read

func (f *File) Read(p []byte) (int, error)

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

func (f *File) ReadDir(count int) ([]fs.DirEntry, error)

ReadDir implements the fs.ReadDirFile interface. Since this mock represents a regular file, not a directory, it always returns an error.

func (*File) Reset

func (f *File) Reset()

Reset resets the file position to the beginning

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

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

func (f *File) Stat() (os.FileInfo, error)

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

func (f *File) Truncate(n int)

Truncate truncates the file to the specified size

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

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 (w *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 (w *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

  • doc.go
  • file.go
  • hash.go

Jump to

Keyboard shortcuts

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