mocks

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mocks provides mock implementations for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateCall

type CreateCall struct {
	DestPath  string
	SourceDir string
	Exclude   []string
}

CreateCall records parameters of a Create call.

type ExtractCall

type ExtractCall struct {
	ZipPath string
	DestDir string
}

ExtractCall records parameters of an Extract call.

type InstallCall

type InstallCall struct {
	ExecPath   string
	ConfigPath string
	Hour       int
	Minute     int
}

InstallCall records parameters of an Install call.

type MockArchiver

type MockArchiver struct {
	// CreateCalls records calls to Create
	CreateCalls []CreateCall
	// ExtractCalls records calls to Extract
	ExtractCalls []ExtractCall
	// ListResults maps zip paths to file listings
	ListResults map[string]map[string]ports.FileInfo
	// ReadResults maps "zipPath:filePath" to content
	ReadResults map[string]string
	// Errors maps method calls to errors
	Errors map[string]error
	// CreateResult is the default file count to return
	CreateResult int
}

MockArchiver implements ports.Archiver for testing.

func NewMockArchiver

func NewMockArchiver() *MockArchiver

NewMockArchiver creates a new mock archiver.

func (*MockArchiver) Create

func (m *MockArchiver) Create(destPath, sourceDir string, exclude []string) (int, error)

Create creates a zip archive of sourceDir at destPath. Returns the number of files archived.

func (*MockArchiver) Extract

func (m *MockArchiver) Extract(zipPath, destDir string) error

Extract extracts a zip archive to destDir.

func (*MockArchiver) List

func (m *MockArchiver) List(zipPath string) (map[string]ports.FileInfo, error)

List returns a map of file paths to their info from the archive.

func (*MockArchiver) ReadFile

func (m *MockArchiver) ReadFile(zipPath, filePath, projectName string) (string, error)

ReadFile reads the contents of a file from inside a zip archive.

type MockFileSystem

type MockFileSystem struct {
	// Files maps paths to file contents for ReadFile/WriteFile
	Files map[string][]byte
	// Dirs maps paths to directory entries for ReadDir
	Dirs map[string][]os.DirEntry
	// Stats maps paths to FileInfo for Stat
	Stats map[string]os.FileInfo
	// Errors maps paths to errors (for simulating failures)
	Errors map[string]error
	// WalkEntries contains entries to return during Walk
	WalkEntries []WalkEntry
}

MockFileSystem implements ports.FileSystem for testing.

func NewMockFileSystem

func NewMockFileSystem() *MockFileSystem

NewMockFileSystem creates a new mock filesystem.

func (*MockFileSystem) Create

func (m *MockFileSystem) Create(name string) (*os.File, error)

Create creates or truncates the named file.

func (*MockFileSystem) MkdirAll

func (m *MockFileSystem) MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory along with any necessary parents.

func (*MockFileSystem) Open

func (m *MockFileSystem) Open(name string) (fs.File, error)

Open opens the named file for reading.

func (*MockFileSystem) ReadDir

func (m *MockFileSystem) ReadDir(name string) ([]os.DirEntry, error)

ReadDir reads the named directory and returns directory entries.

func (*MockFileSystem) ReadFile

func (m *MockFileSystem) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns the contents.

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) Rename

func (m *MockFileSystem) Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath.

func (*MockFileSystem) Stat

func (m *MockFileSystem) Stat(name string) (os.FileInfo, error)

Stat returns file info for the named file.

func (*MockFileSystem) Walk

func (m *MockFileSystem) Walk(root string, fn ports.WalkFunc) error

Walk walks the file tree rooted at root, calling fn for each file or directory.

func (*MockFileSystem) WriteFile

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

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

type MockGitClient

type MockGitClient struct {
	// Heads maps repository paths to HEAD commit hashes
	Heads map[string]string
	// Repos maps paths to whether they are git repos
	Repos map[string]bool
}

MockGitClient implements ports.GitClient for testing.

func NewMockGitClient

func NewMockGitClient() *MockGitClient

NewMockGitClient creates a new mock git client.

func (*MockGitClient) GetHead

func (m *MockGitClient) GetHead(repoPath string) string

GetHead returns the current HEAD commit hash for the repository. Returns empty string if not a git repo or on error.

func (*MockGitClient) IsRepo

func (m *MockGitClient) IsRepo(path string) bool

IsRepo checks if the given path is a git repository.

type MockLaunchdService

type MockLaunchdService struct {
	// Installed tracks whether the service is "installed"
	Installed bool
	// StatusResult is the status to return
	StatusResult string
	// PlistPathResult is the plist path to return
	PlistPathResult string
	// LogPathResult is the log path to return
	LogPathResult string
	// InstallCalls records calls to Install
	InstallCalls []InstallCall
	// Errors maps method names to errors
	Errors map[string]error
}

MockLaunchdService implements ports.LaunchdService for testing.

func NewMockLaunchdService

func NewMockLaunchdService() *MockLaunchdService

NewMockLaunchdService creates a new mock launchd service.

func (*MockLaunchdService) Install

func (m *MockLaunchdService) Install(execPath, configPath string, hour, minute int) error

Install creates the plist file and loads the service.

func (*MockLaunchdService) IsInstalled

func (m *MockLaunchdService) IsInstalled() bool

IsInstalled checks if the service is currently installed.

func (*MockLaunchdService) LogPath

func (m *MockLaunchdService) LogPath() string

LogPath returns the path where logs should be written.

func (*MockLaunchdService) PlistPath

func (m *MockLaunchdService) PlistPath() string

PlistPath returns the path where the plist file should be stored.

func (*MockLaunchdService) Status

func (m *MockLaunchdService) Status() string

Status returns the current status of the service.

func (*MockLaunchdService) Uninstall

func (m *MockLaunchdService) Uninstall() error

Uninstall unloads the service and removes the plist file.

type MockResticClient added in v1.0.0

type MockResticClient struct {
	// InitializedRepos tracks which repos have been initialized
	InitializedRepos map[string]bool
	// Snapshots stores snapshots by repo path
	SnapshotsByRepo map[string][]ports.Snapshot
	// RestoredSnapshots tracks restore calls: repoPath -> snapshotID -> targetDir
	RestoredSnapshots map[string]map[string]string
	// ForgetCalls tracks forget calls: repoPath -> keepLast
	ForgetCalls map[string]int
	// NextSnapshotID is returned by the next Backup call
	NextSnapshotID string
	// Errors allows simulating errors for specific operations
	Errors struct {
		Init      error
		Backup    error
		Snapshots error
		Restore   error
		Forget    error
	}
}

MockResticClient implements ports.ResticClient for testing.

func NewMockResticClient added in v1.0.0

func NewMockResticClient() *MockResticClient

NewMockResticClient creates a new mock restic client.

func (*MockResticClient) Backup added in v1.0.0

func (m *MockResticClient) Backup(repoPath, password string, paths []string, tags []string) (string, error)

Backup creates a new backup of the given paths to the repository.

func (*MockResticClient) Forget added in v1.0.0

func (m *MockResticClient) Forget(repoPath, password string, keepLast int, prune bool) error

Forget removes old snapshots according to the retention policy.

func (*MockResticClient) Init added in v1.0.0

func (m *MockResticClient) Init(repoPath, password string) error

Init initializes a new restic repository at the given path.

func (*MockResticClient) IsInitialized added in v1.0.0

func (m *MockResticClient) IsInitialized(repoPath string) bool

IsInitialized checks if a restic repository exists at the given path.

func (*MockResticClient) Restore added in v1.0.0

func (m *MockResticClient) Restore(repoPath, password, snapshotID, targetDir string) error

Restore restores a snapshot to the given target directory.

func (*MockResticClient) Snapshots added in v1.0.0

func (m *MockResticClient) Snapshots(repoPath, password string, tags []string) ([]ports.Snapshot, error)

Snapshots returns all snapshots in the repository.

type MockTUIService

type MockTUIService struct {
	// ConfigResult is the config to return from LoadConfig
	ConfigResult *config.Config
	// ConfigError is the error to return from LoadConfig
	ConfigError error

	// Projects is the list of projects to return
	Projects []ports.TUIProjectInfo
	// ProjectsError is the error to return from ListProjects
	ProjectsError error

	// Versions maps project names to their versions
	Versions map[string][]ports.TUIVersionInfo
	// VersionsError is the error to return from ListVersions
	VersionsError error

	// Snapshots is the list of restic snapshots to return
	Snapshots []ports.TUISnapshotInfo
	// SnapshotsError is the error to return from ListSnapshots
	SnapshotsError error

	// BackupResults maps project names to backup results
	BackupResults map[string]ports.TUIBackupResult

	// VerifyErrors maps project names to verify errors
	VerifyErrors map[string]error

	// Call tracking
	LoadConfigCalls    int
	ListProjectsCalls  int
	ListVersionsCalls  []string
	ListSnapshotsCalls []string
	RunBackupCalls     []string
	VerifyBackupCalls  []string
}

MockTUIService implements ports.TUIService for testing.

func NewMockTUIService

func NewMockTUIService() *MockTUIService

NewMockTUIService creates a new mock TUI service.

func (*MockTUIService) ListProjects

func (m *MockTUIService) ListProjects(cfg *config.Config) ([]ports.TUIProjectInfo, error)

ListProjects returns all projects with their metadata.

func (*MockTUIService) ListSnapshots added in v1.0.0

func (m *MockTUIService) ListSnapshots(cfg *config.Config, tag string) ([]ports.TUISnapshotInfo, error)

ListSnapshots returns all restic snapshots for sensitive sources.

func (*MockTUIService) ListVersions

func (m *MockTUIService) ListVersions(cfg *config.Config, project string) ([]ports.TUIVersionInfo, error)

ListVersions returns all backup versions for a project.

func (*MockTUIService) LoadConfig

func (m *MockTUIService) LoadConfig() (*config.Config, error)

LoadConfig loads the application configuration.

func (*MockTUIService) RunBackup

func (m *MockTUIService) RunBackup(cfg *config.Config, project string) ports.TUIBackupResult

RunBackup performs a backup of the specified project.

func (*MockTUIService) VerifyBackup

func (m *MockTUIService) VerifyBackup(cfg *config.Config, project string) error

VerifyBackup verifies the latest backup of a project.

type WalkEntry

type WalkEntry struct {
	Path string
	Info os.FileInfo
	Err  error
}

WalkEntry represents a file or directory entry for Walk testing.

Jump to

Keyboard shortcuts

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