testutil

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testutil provides utilities for testing dodot components.

This package follows the testing guidelines defined in docs/testing/guide.txxt and provides the infrastructure described in docs/testing/infrastructure-utils-setup.txxt.

Key components:

  • TestEnvironment: Core test orchestrator with isolation and cleanup
  • MemoryFS: In-memory filesystem implementation for fast, isolated tests
  • MockDataStore: Mock state management without filesystem operations
  • PackBuilder: Declarative pack setup builder

Usage guidelines:

  • 95% of tests should use EnvMemoryOnly for speed and isolation
  • Only pkg/datastore tests should use real filesystem operations
  • All test data should be defined inline, not in external files
  • Each test should be completely isolated with no shared state

Package testutil provides testing utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTestChecksum added in v0.3.0

func GetTestChecksum(content string) string

GetTestChecksum calculates a SHA256 checksum for test content This is used in tests to generate predictable checksums

Types

type EnvType added in v0.3.0

type EnvType int

EnvType defines the type of test environment

const (
	EnvMemoryOnly EnvType = iota // Pure in-memory, no real filesystem
	EnvIsolated                  // Real filesystem in temp directory
)

type FileTree added in v0.3.0

type FileTree map[string]interface{}

FileTree represents a directory structure for testing

type MemoryFS added in v0.3.0

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

MemoryFS implements types.FS interface with in-memory storage

func NewMemoryFS added in v0.3.0

func NewMemoryFS() *MemoryFS

NewMemoryFS creates a new in-memory filesystem

func (*MemoryFS) Chdir added in v0.3.0

func (m *MemoryFS) Chdir(dir string) error

Chdir changes the current working directory

func (*MemoryFS) Getwd added in v0.3.0

func (m *MemoryFS) Getwd() (string, error)

Getwd returns the current working directory

func (*MemoryFS) Lstat added in v0.3.0

func (m *MemoryFS) Lstat(name string) (os.FileInfo, error)

Lstat returns file info without following symlinks

func (*MemoryFS) MkdirAll added in v0.3.0

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

MkdirAll creates a directory and all necessary parents

func (*MemoryFS) Open added in v0.3.0

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

Open opens a file for reading

func (*MemoryFS) ReadDir added in v0.3.0

func (m *MemoryFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads a directory and returns its entries

func (*MemoryFS) ReadFile added in v0.3.0

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

ReadFile reads the entire file content

func (m *MemoryFS) Readlink(name string) (string, error)

Readlink returns the destination of a symbolic link

func (*MemoryFS) Remove added in v0.3.0

func (m *MemoryFS) Remove(name string) error

Remove removes a file or empty directory

func (*MemoryFS) RemoveAll added in v0.3.0

func (m *MemoryFS) RemoveAll(path string) error

RemoveAll removes a file or directory recursively

func (*MemoryFS) Rename added in v0.3.0

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

Rename renames (moves) a file or directory

func (*MemoryFS) Stat added in v0.3.0

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

Stat returns file info

func (*MemoryFS) Stats added in v0.3.0

func (m *MemoryFS) Stats() (reads, writes int)

Stats returns filesystem operation statistics

func (m *MemoryFS) Symlink(target, link string) error

Symlink creates a symbolic link

func (*MemoryFS) WithError added in v0.3.0

func (m *MemoryFS) WithError(path string, err error) *MemoryFS

WithError configures the filesystem to return an error for a specific path

func (*MemoryFS) WriteFile added in v0.3.0

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

WriteFile writes data to a file, creating it if necessary

type MockDataStore added in v0.3.0

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

MockDataStore is a mock implementation of datastore.DataStore for testing

func NewMockDataStore added in v0.3.0

func NewMockDataStore() *MockDataStore

NewMockDataStore creates a new mock DataStore

func (m *MockDataStore) CreateDataLink(pack, handlerName, sourceFile string) (string, error)

CreateDataLink creates an intermediate symlink in the datastore

func (m *MockDataStore) CreateUserLink(datastorePath, userPath string) error

CreateUserLink creates a user-facing symlink

func (*MockDataStore) GetCalls added in v0.3.0

func (m *MockDataStore) GetCalls() []string

GetCalls returns all recorded method calls

func (m *MockDataStore) GetDataLinks() map[string]string

GetDataLinks returns all data links (for testing)

func (*MockDataStore) GetSentinels added in v0.3.0

func (m *MockDataStore) GetSentinels() map[string]bool

GetSentinels returns all sentinels (for testing)

func (m *MockDataStore) GetUserLinks() map[string]string

GetUserLinks returns all user links (for testing)

func (*MockDataStore) HasHandlerState added in v0.3.0

func (m *MockDataStore) HasHandlerState(pack, handlerName string) (bool, error)

HasHandlerState checks if any state exists for a handler in a pack

func (*MockDataStore) HasSentinel added in v0.3.0

func (m *MockDataStore) HasSentinel(pack, handlerName, sentinel string) (bool, error)

HasSentinel checks if an operation has been completed

func (*MockDataStore) ListHandlerSentinels added in v0.3.0

func (m *MockDataStore) ListHandlerSentinels(pack, handlerName string) ([]string, error)

ListHandlerSentinels returns all sentinel files for a specific handler in a pack

func (*MockDataStore) ListPackHandlers added in v0.3.0

func (m *MockDataStore) ListPackHandlers(pack string) ([]string, error)

ListPackHandlers returns a list of all handlers that have state for a given pack

func (*MockDataStore) RemoveState added in v0.3.0

func (m *MockDataStore) RemoveState(pack, handlerName string) error

RemoveState removes all state for a handler in a pack

func (*MockDataStore) Reset added in v0.3.0

func (m *MockDataStore) Reset()

Reset clears all state

func (*MockDataStore) RunAndRecord added in v0.3.0

func (m *MockDataStore) RunAndRecord(pack, handlerName, command, sentinel string) error

RunAndRecord executes a command and records completion with a sentinel

func (*MockDataStore) SetSentinel added in v0.3.0

func (m *MockDataStore) SetSentinel(pack, handlerName, sentinel string, exists bool)

SetSentinel sets a sentinel value for testing

func (m *MockDataStore) WithDataLink(pack, handler, source, datastorePath string) *MockDataStore

WithDataLink pre-configures a data link

func (*MockDataStore) WithError added in v0.3.0

func (m *MockDataStore) WithError(method string, err error) *MockDataStore

WithError configures the mock to return an error for a specific method

func (*MockDataStore) WithSentinel added in v0.3.0

func (m *MockDataStore) WithSentinel(pack, handler, sentinel string, exists bool) *MockDataStore

WithSentinel pre-configures a sentinel

func (m *MockDataStore) WithUserLink(userPath, datastorePath string) *MockDataStore

WithUserLink pre-configures a user link

type MockPathResolver added in v0.3.0

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

MockPathResolver provides mock path resolution for testing

func NewMockPathResolver added in v0.3.0

func NewMockPathResolver(home, xdgConfig, xdgData string) *MockPathResolver

NewMockPathResolver creates a new mock path resolver

func (*MockPathResolver) CacheDir added in v0.3.0

func (m *MockPathResolver) CacheDir() string

CacheDir returns the XDG cache directory

func (*MockPathResolver) ConfigDir added in v0.3.0

func (m *MockPathResolver) ConfigDir() string

ConfigDir returns the XDG config directory

func (*MockPathResolver) DataDir added in v0.3.0

func (m *MockPathResolver) DataDir() string

DataDir returns the XDG data directory

func (*MockPathResolver) DotfilesRoot added in v0.3.0

func (m *MockPathResolver) DotfilesRoot() string

DotfilesRoot returns the dotfiles root directory

func (*MockPathResolver) Home added in v0.3.0

func (m *MockPathResolver) Home() string

Home returns the home directory path

func (*MockPathResolver) MapPackFileToSystem added in v0.3.0

func (m *MockPathResolver) MapPackFileToSystem(pack *types.Pack, relPath string) string

MapPackFileToSystem maps a pack file to its system location

func (*MockPathResolver) PackHandlerDir added in v0.3.0

func (m *MockPathResolver) PackHandlerDir(packName, handlerName string) string

PackHandlerDir returns the directory for a pack's handler state

func (*MockPathResolver) StateDir added in v0.3.0

func (m *MockPathResolver) StateDir() string

StateDir returns the XDG state directory

func (*MockPathResolver) WithDotfilesRoot added in v0.3.0

func (m *MockPathResolver) WithDotfilesRoot(path string) *MockPathResolver

WithDotfilesRoot sets a custom dotfiles root

type MockSimpleDataStore added in v0.3.0

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

MockSimpleDataStore provides a simple mock DataStore for testing

func NewMockSimpleDataStore added in v0.3.0

func NewMockSimpleDataStore(fs types.FS) *MockSimpleDataStore

NewMockSimpleDataStore creates a new mock datastore

func (m *MockSimpleDataStore) CreateDataLink(pack, handlerName, sourceFile string) (string, error)
func (m *MockSimpleDataStore) CreateUserLink(datastorePath, userPath string) error

func (*MockSimpleDataStore) GetCommands added in v0.3.0

func (m *MockSimpleDataStore) GetCommands() map[string]string
func (m *MockSimpleDataStore) GetDataLinks() map[string]string

Helper methods for testing

func (*MockSimpleDataStore) GetSentinels added in v0.3.0

func (m *MockSimpleDataStore) GetSentinels() map[string]bool
func (m *MockSimpleDataStore) GetUserLinks() map[string]string

func (*MockSimpleDataStore) HasHandlerState added in v0.3.0

func (m *MockSimpleDataStore) HasHandlerState(pack, handlerName string) (bool, error)

HasHandlerState checks if any state exists for a handler in a pack

func (*MockSimpleDataStore) HasSentinel added in v0.3.0

func (m *MockSimpleDataStore) HasSentinel(pack, handlerName, sentinel string) (bool, error)

func (*MockSimpleDataStore) ListHandlerSentinels added in v0.3.0

func (m *MockSimpleDataStore) ListHandlerSentinels(pack, handlerName string) ([]string, error)

ListHandlerSentinels returns all sentinel files for a specific handler in a pack

func (*MockSimpleDataStore) ListPackHandlers added in v0.3.0

func (m *MockSimpleDataStore) ListPackHandlers(pack string) ([]string, error)

ListPackHandlers returns a list of all handlers that have state for a given pack

func (*MockSimpleDataStore) RemoveState added in v0.3.0

func (m *MockSimpleDataStore) RemoveState(pack, handlerName string) error

func (*MockSimpleDataStore) RunAndRecord added in v0.3.0

func (m *MockSimpleDataStore) RunAndRecord(pack, handlerName, command, sentinel string) error

func (*MockSimpleDataStore) SetSentinel added in v0.3.0

func (m *MockSimpleDataStore) SetSentinel(pack, handlerName, sentinel string, exists bool)

SetSentinel sets a sentinel value for testing

type PackConfig added in v0.3.0

type PackConfig struct {
	Files map[string]string // Path -> Content
	Rules []Rule            // Rules configuration
}

PackConfig defines configuration for a test pack

func GitPack added in v0.3.0

func GitPack() PackConfig

GitPack returns a pre-configured git pack

func ShellPack added in v0.3.0

func ShellPack() PackConfig

ShellPack returns a pre-configured shell pack

func ToolsPack added in v0.3.0

func ToolsPack() PackConfig

ToolsPack returns a pre-configured tools pack

func VimPack added in v0.3.0

func VimPack() PackConfig

VimPack returns a pre-configured vim pack

type Rule added in v0.3.0

type Rule struct {
	Type    string // "filename", "directory", etc.
	Pattern string
	Handler string
	Options map[string]interface{}
}

Rule represents a test rule configuration

type TestEnvironment added in v0.1.1

type TestEnvironment struct {
	// Core paths
	DotfilesRoot string
	HomeDir      string
	XDGData      string

	// Core dependencies
	DataStore datastore.DataStore
	FS        types.FS
	Paths     types.Pather

	// Environment type
	Type EnvType
	// contains filtered or unexported fields
}

TestEnvironment provides a complete test environment with all dependencies

func NewTestEnvironment added in v0.1.1

func NewTestEnvironment(t *testing.T, envType EnvType) *TestEnvironment

NewTestEnvironment creates a new test environment

func (*TestEnvironment) Cleanup added in v0.1.1

func (env *TestEnvironment) Cleanup()

Cleanup performs environment cleanup

func (*TestEnvironment) SetupPack added in v0.3.0

func (env *TestEnvironment) SetupPack(name string, config PackConfig) types.Pack

SetupPack creates a test pack with the given configuration

func (*TestEnvironment) WithFileTree added in v0.3.0

func (env *TestEnvironment) WithFileTree(tree FileTree)

WithFileTree creates a complete file tree structure

Jump to

Keyboard shortcuts

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