Documentation
¶
Overview ¶
Package filesystem provides a file system abstraction for testability.
Package filesystem provides a file system abstraction for testability.
Index ¶
- func AppDataDir(appDirName, subdir string) (string, error)
- func CopyFile(src, dst string) error
- func EnsureDir(path string) error
- func Exists(path string) bool
- func IsDir(path string) bool
- func IsFile(path string) bool
- type FileSystem
- type MockFileSystem
- func (m *MockFileSystem) FileCount() int
- func (m *MockFileSystem) HasFile(path string) bool
- func (m *MockFileSystem) MkdirAll(path string, perm fs.FileMode) error
- func (m *MockFileSystem) Open(name string) (*os.File, error)
- func (m *MockFileSystem) ReadDir(name string) ([]fs.DirEntry, error)
- func (m *MockFileSystem) ReadFile(name string) ([]byte, error)
- func (m *MockFileSystem) Remove(name string) error
- func (m *MockFileSystem) RemoveAll(path string) error
- func (m *MockFileSystem) Reset()
- func (m *MockFileSystem) Stat(name string) (fs.FileInfo, error)
- func (m *MockFileSystem) UserHomeDir() (string, error)
- func (m *MockFileSystem) WithDir(path string) *MockFileSystem
- func (m *MockFileSystem) WithError(err error) *MockFileSystem
- func (m *MockFileSystem) WithFile(path string, content []byte) *MockFileSystem
- func (m *MockFileSystem) WithFileString(path, content string) *MockFileSystem
- func (m *MockFileSystem) WithHomeDir(path string) *MockFileSystem
- func (m *MockFileSystem) WithPathError(path string, err error) *MockFileSystem
- func (m *MockFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error
- type OSFileSystem
- func (OSFileSystem) MkdirAll(path string, perm fs.FileMode) error
- func (OSFileSystem) Open(name string) (*os.File, error)
- func (OSFileSystem) ReadDir(name string) ([]fs.DirEntry, error)
- func (OSFileSystem) ReadFile(name string) ([]byte, error)
- func (OSFileSystem) Remove(name string) error
- func (OSFileSystem) RemoveAll(path string) error
- func (OSFileSystem) Stat(name string) (fs.FileInfo, error)
- func (OSFileSystem) UserHomeDir() (string, error)
- func (OSFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppDataDir ¶
AppDataDir returns the path to the application's data directory. If subdir is provided, it returns the path to that subdirectory.
Types ¶
type FileSystem ¶
type FileSystem interface {
// Read operations
ReadFile(name string) ([]byte, error)
Stat(name string) (fs.FileInfo, error)
Open(name string) (*os.File, error)
// Write operations
WriteFile(name string, data []byte, perm fs.FileMode) error
MkdirAll(path string, perm fs.FileMode) error
Remove(name string) error
RemoveAll(path string) error
// Directory operations
ReadDir(name string) ([]fs.DirEntry, error)
// Path operations
UserHomeDir() (string, error)
}
FileSystem defines the interface for file system operations. This abstraction allows for easy mocking in tests.
var Default FileSystem = &OSFileSystem{}
Default is the default file system implementation using OS calls.
type MockFileSystem ¶
type MockFileSystem struct {
// Files stores file contents by path.
Files map[string][]byte
// Dirs stores directory paths (value is always true).
Dirs map[string]bool
// Err is a default error to return (if set, overrides normal behavior).
Err error
// ErrByPath allows setting specific errors for specific paths.
ErrByPath map[string]error
// HomeDir is the home directory to return from UserHomeDir.
HomeDir string
// contains filtered or unexported fields
}
MockFileSystem is a mock implementation of FileSystem for testing. It uses an in-memory map to simulate files and directories.
func NewMockFileSystem ¶
func NewMockFileSystem() *MockFileSystem
NewMockFileSystem creates a new MockFileSystem.
func (*MockFileSystem) FileCount ¶
func (m *MockFileSystem) FileCount() int
FileCount returns the number of files in the mock file system.
func (*MockFileSystem) HasFile ¶
func (m *MockFileSystem) HasFile(path string) bool
HasFile returns true if the file exists.
func (*MockFileSystem) MkdirAll ¶
func (m *MockFileSystem) MkdirAll(path string, perm fs.FileMode) error
MkdirAll creates a directory path in the mock file system.
func (*MockFileSystem) Open ¶
func (m *MockFileSystem) Open(name string) (*os.File, error)
Open opens the named file for reading. Note: Returns an os.File pointer for interface compatibility, but for mocks this will return nil with an error since we can't create real file handles. Use ReadFile for mock testing instead.
func (*MockFileSystem) ReadDir ¶
func (m *MockFileSystem) ReadDir(name string) ([]fs.DirEntry, error)
ReadDir reads the named directory and returns all its directory entries.
func (*MockFileSystem) ReadFile ¶
func (m *MockFileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the named file from the mock file system.
func (*MockFileSystem) Remove ¶
func (m *MockFileSystem) Remove(name string) error
Remove removes the named file or empty directory.
func (*MockFileSystem) RemoveAll ¶
func (m *MockFileSystem) RemoveAll(path string) error
RemoveAll removes path and any children it contains.
func (*MockFileSystem) Reset ¶
func (m *MockFileSystem) Reset()
Reset clears all files, directories, and errors.
func (*MockFileSystem) Stat ¶
func (m *MockFileSystem) Stat(name string) (fs.FileInfo, error)
Stat returns a mock FileInfo for the named file or directory.
func (*MockFileSystem) UserHomeDir ¶
func (m *MockFileSystem) UserHomeDir() (string, error)
UserHomeDir returns the mock home directory.
func (*MockFileSystem) WithDir ¶
func (m *MockFileSystem) WithDir(path string) *MockFileSystem
WithDir adds a directory to the mock file system.
func (*MockFileSystem) WithError ¶
func (m *MockFileSystem) WithError(err error) *MockFileSystem
WithError sets a default error to return for all operations.
func (*MockFileSystem) WithFile ¶
func (m *MockFileSystem) WithFile(path string, content []byte) *MockFileSystem
WithFile adds a file to the mock file system and returns the mock for chaining.
func (*MockFileSystem) WithFileString ¶
func (m *MockFileSystem) WithFileString(path, content string) *MockFileSystem
WithFileString adds a file with string content.
func (*MockFileSystem) WithHomeDir ¶
func (m *MockFileSystem) WithHomeDir(path string) *MockFileSystem
WithHomeDir sets the home directory.
func (*MockFileSystem) WithPathError ¶
func (m *MockFileSystem) WithPathError(path string, err error) *MockFileSystem
WithPathError sets an error for a specific path.
type OSFileSystem ¶
type OSFileSystem struct{}
OSFileSystem implements FileSystem using the real OS file system.
func (OSFileSystem) MkdirAll ¶
func (OSFileSystem) MkdirAll(path string, perm fs.FileMode) error
MkdirAll creates a directory named path, along with any necessary parents.
func (OSFileSystem) Open ¶
func (OSFileSystem) Open(name string) (*os.File, error)
Open opens the named file for reading.
func (OSFileSystem) ReadDir ¶
func (OSFileSystem) ReadDir(name string) ([]fs.DirEntry, error)
ReadDir reads the named directory and returns all its directory entries.
func (OSFileSystem) ReadFile ¶
func (OSFileSystem) ReadFile(name string) ([]byte, error)
ReadFile reads the named file and returns the contents.
func (OSFileSystem) Remove ¶
func (OSFileSystem) Remove(name string) error
Remove removes the named file or empty directory.
func (OSFileSystem) RemoveAll ¶
func (OSFileSystem) RemoveAll(path string) error
RemoveAll removes path and any children it contains.
func (OSFileSystem) Stat ¶
func (OSFileSystem) Stat(name string) (fs.FileInfo, error)
Stat returns a FileInfo describing the named file.
func (OSFileSystem) UserHomeDir ¶
func (OSFileSystem) UserHomeDir() (string, error)
UserHomeDir returns the current user's home directory.