filesystem

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package filesystem provides a file system abstraction for testability.

Package filesystem provides a file system abstraction for testability.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppDataDir

func AppDataDir(appDirName, subdir string) (string, error)

AppDataDir returns the path to the application's data directory. If subdir is provided, it returns the path to that subdirectory.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst.

func EnsureDir

func EnsureDir(path string) error

EnsureDir creates a directory if it doesn't exist.

func Exists

func Exists(path string) bool

Exists returns true if the path exists (file or directory).

func IsDir

func IsDir(path string) bool

IsDir returns true if the path is a directory.

func IsFile

func IsFile(path string) bool

IsFile returns true if the path is a file.

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.

func (*MockFileSystem) WriteFile

func (m *MockFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile writes data to the named file in the mock file system.

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.

func (OSFileSystem) WriteFile

func (OSFileSystem) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile writes data to the named file, creating it if necessary.

Jump to

Keyboard shortcuts

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