testutil

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testutil provides common utilities and helpers for testing dodot. It includes custom assertions, test fixture generators, and mock implementations to streamline the development of tests.

Index

Examples

Constants

This section is empty.

Variables

View Source
var CommonDotfiles = map[string]string{
	".vimrc":     `" vim configuration\nset number\nset autoindent`,
	".bashrc":    `# bash configuration\nexport PS1="$ "\nalias ll="ls -la"`,
	".zshrc":     `# zsh configuration\nexport PS1="$ "\nalias ll="ls -la"`,
	".gitconfig": `[user]\n    name = Test User\n    email = test@example.com`,
}

CommonDotfiles provides common dotfile content for testing

View Source
var StandardPackConfig = map[string]string{
	"symlink": `[[rules]]
trigger = "filename"
pattern = ".*"
powerup = "symlink"
`,
	"homebrew": `[[rules]]
trigger = "filename"  
pattern = "Brewfile"
powerup = "homebrew"
`,
	"install": `[[rules]]
trigger = "filename"
pattern = "install.sh"
powerup = "install_script"
`,
}

StandardPackConfig provides standard pack configurations

Functions

func AssertContains

func AssertContains(t *testing.T, str, substr string, msgAndArgs ...interface{})

AssertContains checks if a string contains a substring

func AssertDirExists

func AssertDirExists(t *testing.T, path string, msgAndArgs ...interface{})

AssertDirExists checks that a directory exists.

func AssertEqual

func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertEqual checks if two values are equal using deep equality

func AssertError

func AssertError(t *testing.T, err error, msgAndArgs ...interface{})

AssertError checks if an error occurred

func AssertErrorContains

func AssertErrorContains(t *testing.T, err error, substr string, msgAndArgs ...interface{})

AssertErrorContains checks if an error occurred and contains a specific substring

func AssertFalse

func AssertFalse(t *testing.T, value bool, msgAndArgs ...interface{})

AssertFalse checks if a value is false

func AssertFileContent

func AssertFileContent(t *testing.T, path, expected string)

AssertFileContent checks that a file exists and has the expected content.

func AssertFileExists

func AssertFileExists(t *testing.T, path string, msgAndArgs ...interface{})

AssertFileExists checks that a file exists.

func AssertMapEqual

func AssertMapEqual(t *testing.T, expected, actual map[string]string, msgAndArgs ...interface{})

AssertMapEqual checks if two string maps are equal

func AssertNil

func AssertNil(t *testing.T, value interface{}, msgAndArgs ...interface{})

AssertNil checks if a value is nil

func AssertNoError

func AssertNoError(t *testing.T, err error, msgAndArgs ...interface{})

AssertNoError checks if no error occurred

func AssertNoFile

func AssertNoFile(t *testing.T, path string)

AssertNoFile checks that a file does not exist.

func AssertNoPanic

func AssertNoPanic(t *testing.T, fn func(), msgAndArgs ...interface{})

AssertNoPanic checks if a function does not panic

func AssertNotContains

func AssertNotContains(t *testing.T, str, substr string, msgAndArgs ...interface{})

AssertNotContains checks if a string does not contain a substring

func AssertNotEmpty

func AssertNotEmpty(t *testing.T, value string, msgAndArgs ...interface{})

AssertNotEmpty checks that a string is not empty

func AssertNotEqual

func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{})

AssertNotEqual checks if two values are not equal

func AssertNotNil

func AssertNotNil(t *testing.T, value interface{}, msgAndArgs ...interface{})

AssertNotNil checks if a value is not nil

func AssertPanic

func AssertPanic(t *testing.T, fn func(), msgAndArgs ...interface{})

AssertPanic checks if a function panics

func AssertSliceEqual

func AssertSliceEqual(t *testing.T, expected, actual []string, msgAndArgs ...interface{})

AssertSliceEqual checks if two slices are equal (ignoring order)

func AssertSymlink(t *testing.T, link, expectedTarget string)

AssertSymlink checks that a symlink exists and points to the expected target.

func AssertTrue

func AssertTrue(t *testing.T, value bool, msgAndArgs ...interface{})

AssertTrue checks if a value is true

func CalculateFileChecksum

func CalculateFileChecksum(filepath string) (string, error)

CalculateFileChecksum calculates SHA256 checksum of a file

func Chmod

func Chmod(t *testing.T, path string, mode os.FileMode)

Chmod changes the permissions of a file or directory. It fails the test if the operation fails.

func CommandAvailable added in v0.1.0

func CommandAvailable(name string) bool

CommandAvailable checks if a command is available in the system PATH

func CreateComplexPack

func CreateComplexPack(t *testing.T, baseDir string) string

CreateComplexPack creates a pack with various file types for comprehensive testing

func CreateDir

func CreateDir(t *testing.T, parent, name string) string

CreateDir creates a directory in the specified parent directory. It fails the test if the directory cannot be created.

func CreateDirT added in v0.1.4

func CreateDirT(t *testing.T, fs types.FS, path string)

CreateDirT creates a directory in the given synthfs filesystem

func CreateDotfilesRepo

func CreateDotfilesRepo(t *testing.T) string

CreateDotfilesRepo creates a complete test dotfiles repository

func CreateFile

func CreateFile(t *testing.T, dir, name, content string) string

CreateFile creates a file with the given content in the specified directory. It fails the test if the file cannot be created.

func CreateFileT added in v0.1.4

func CreateFileT(t *testing.T, fs types.FS, path, content string)

CreateFileT creates a file in the given synthfs filesystem

func CreateMinimalPack

func CreateMinimalPack(t *testing.T, baseDir, packName string) string

CreateMinimalPack creates a minimal pack for simple tests

func CreatePackFixture

func CreatePackFixture(t *testing.T, baseDir string, fixture PackFixture) string

CreatePackFixture creates a pack fixture in the given directory

func CreateSymlink(t *testing.T, target, link string)

CreateSymlink creates a symbolic link pointing to target. It fails the test if the symlink cannot be created.

func DirExists

func DirExists(t *testing.T, path string) bool

DirExists checks if a directory exists.

func FileExists

func FileExists(t *testing.T, path string) bool

FileExists checks if a file exists and is not a directory.

func NewTestFS added in v0.1.4

func NewTestFS() types.FS

NewTestFS creates a new test filesystem that implements types.FS

func ReadFile

func ReadFile(t *testing.T, path string) string

ReadFile reads the content of a file and returns it as a string. It fails the test if the file cannot be read.

func ReadSymlink(t *testing.T, path string) string

ReadSymlink reads the target of a symbolic link. It fails the test if the link cannot be read.

func RequireRoot

func RequireRoot(t *testing.T)

RequireRoot skips the test if not running as root.

func RunCommand added in v0.1.0

func RunCommand(t *testing.T, name string, args ...string)

RunCommand runs a command and fails the test if it returns an error

func Setenv

func Setenv(t *testing.T, key, value string)

Setenv sets an environment variable for the duration of the test.

func SetupMultiplePacks added in v0.1.3

func SetupMultiplePacks(t *testing.T, packNames ...string) map[string]*TestPack

SetupMultiplePacks creates multiple test packs at once

func SkipOnWindows

func SkipOnWindows(t *testing.T)

SkipOnWindows skips the test if running on Windows.

func SymlinkExists

func SymlinkExists(t *testing.T, path string) bool

SymlinkExists checks if a path is a symbolic link.

func TempDir

func TempDir(t *testing.T, prefix string) string

TempDir creates a temporary directory for tests and returns its path. The directory is automatically cleaned up when the test completes.

Types

type MockPaths added in v0.1.4

type MockPaths struct {
	DotfilesRootPath string
	DataDirPath      string
	ConfigDirPath    string
	CacheDirPath     string
	StateDirPath     string
}

MockPaths is a test implementation of the Pather interface

func (*MockPaths) CacheDir added in v0.1.4

func (m *MockPaths) CacheDir() string

CacheDir returns the mock cache directory path

func (*MockPaths) ConfigDir added in v0.1.4

func (m *MockPaths) ConfigDir() string

ConfigDir returns the mock config directory path

func (*MockPaths) DataDir added in v0.1.4

func (m *MockPaths) DataDir() string

DataDir returns the mock data directory path

func (*MockPaths) DeployedPath added in v0.1.4

func (m *MockPaths) DeployedPath() string

DeployedPath returns the deployed path directory

func (*MockPaths) DeployedShellProfile added in v0.1.4

func (m *MockPaths) DeployedShellProfile() string

DeployedShellProfile returns the deployed shell profile directory

func (m *MockPaths) DeployedSymlink() string

DeployedSymlink returns the deployed symlink directory

func (*MockPaths) DotfilesRoot added in v0.1.4

func (m *MockPaths) DotfilesRoot() string

DotfilesRoot returns the mock dotfiles root path

func (*MockPaths) InitScript added in v0.1.4

func (m *MockPaths) InitScript() string

InitScript returns the path to the init script

func (*MockPaths) LogFile added in v0.1.4

func (m *MockPaths) LogFile() string

LogFile returns the path to the log file

func (*MockPaths) StateDir added in v0.1.4

func (m *MockPaths) StateDir() string

StateDir returns the mock state directory path

type MockPowerUp

type MockPowerUp struct {
	NameFunc               func() string
	DescriptionFunc        func() string
	RunModeFunc            func() types.RunMode
	ProcessFunc            func(matches []types.TriggerMatch) ([]types.Action, error)
	ValidateOptionsFunc    func(options map[string]interface{}) error
	GetTemplateContentFunc func() string
}

MockPowerUp is a mock implementation of the types.PowerUp interface for testing.

func (*MockPowerUp) Description

func (m *MockPowerUp) Description() string

Description returns the mock's description.

func (*MockPowerUp) GetTemplateContent added in v0.1.1

func (m *MockPowerUp) GetTemplateContent() string

GetTemplateContent returns the mock's template content.

func (*MockPowerUp) Name

func (m *MockPowerUp) Name() string

Name returns the mock's name.

func (*MockPowerUp) Process

func (m *MockPowerUp) Process(matches []types.TriggerMatch) ([]types.Action, error)

Process runs the mock's process function.

func (*MockPowerUp) RunMode

func (m *MockPowerUp) RunMode() types.RunMode

RunMode returns the mock's run mode.

func (*MockPowerUp) ValidateOptions

func (m *MockPowerUp) ValidateOptions(options map[string]interface{}) error

ValidateOptions runs the mock's validate options function.

type MockTrigger

type MockTrigger struct {
	NameFunc        func() string
	DescriptionFunc func() string
	PriorityFunc    func() int
	MatchFunc       func(path string, info fs.FileInfo) (bool, map[string]interface{})
	TypeFunc        func() types.TriggerType
}

MockTrigger is a mock implementation of the types.Trigger interface for testing.

func (*MockTrigger) Description

func (m *MockTrigger) Description() string

Description returns the mock's description.

func (*MockTrigger) Match

func (m *MockTrigger) Match(path string, info fs.FileInfo) (bool, map[string]interface{})

Match runs the mock's match function.

func (*MockTrigger) Name

func (m *MockTrigger) Name() string

Name returns the mock's name.

func (*MockTrigger) Priority

func (m *MockTrigger) Priority() int

Priority returns the mock's priority.

func (*MockTrigger) Type added in v0.1.1

func (m *MockTrigger) Type() types.TriggerType

Type returns the mock's trigger type.

type PackFixture

type PackFixture struct {
	Name  string
	Files map[string]string // path -> content
	Dirs  []string
}

PackFixture represents a test pack with files and directories

func CommonPackFixtures

func CommonPackFixtures() []PackFixture

CommonPackFixtures returns common pack fixtures for testing

type TB

type TB interface {
	Helper()
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Skip(args ...interface{})
	Skipf(format string, args ...interface{})
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	TempDir() string
}

TB is a subset of testing.TB interface that we use for our test helpers This allows us to use both *testing.T and mock implementations

type TestEnvironment added in v0.1.1

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

TestEnvironment provides isolated directories for integration testing

func NewTestEnvironment added in v0.1.1

func NewTestEnvironment(t *testing.T, name string) *TestEnvironment

NewTestEnvironment creates a new test environment with isolated directories

func (*TestEnvironment) Cleanup added in v0.1.1

func (te *TestEnvironment) Cleanup()

Cleanup is a no-op as t.TempDir() handles cleanup automatically

func (*TestEnvironment) CreatePack added in v0.1.1

func (te *TestEnvironment) CreatePack(name string) string

CreatePack creates a new pack directory and returns its path

func (*TestEnvironment) DataDir added in v0.1.1

func (te *TestEnvironment) DataDir() string

DataDir returns the dodot data directory

func (*TestEnvironment) DotfilesRoot added in v0.1.1

func (te *TestEnvironment) DotfilesRoot() string

DotfilesRoot returns the dotfiles root directory

func (*TestEnvironment) Home added in v0.1.1

func (te *TestEnvironment) Home() string

Home returns the test home directory

type TestFS added in v0.1.4

type TestFS struct {
	*filesystem.TestFileSystem
}

TestFS wraps filesystem.TestFileSystem to implement types.FS

func (*TestFS) Lstat added in v0.1.4

func (t *TestFS) Lstat(name string) (fs.FileInfo, error)

Lstat implements types.FS For testing, we treat Lstat the same as Stat since TestFileSystem doesn't distinguish between regular files and symlinks

func (*TestFS) ReadDir added in v0.1.4

func (t *TestFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir implements types.FS

type TestPack added in v0.1.3

type TestPack struct {
	Root string // Dotfiles root directory
	Name string // Pack name
	Dir  string // Full path to pack directory
}

TestPack represents a test pack with its directory structure

Example

Example of using pack helpers in a test

package main

import (
	"testing"

	"github.com/arthur-debert/dodot/pkg/testutil"
)

func main() {
	// This would normally be in a test function
	t := &testing.T{}

	// Old way:
	// tmpDir := t.TempDir()
	// dotfilesRoot := filepath.Join(tmpDir, "dotfiles")
	// vimDir := filepath.Join(dotfilesRoot, "vim")
	// require.NoError(t, os.MkdirAll(vimDir, 0755))
	// require.NoError(t, os.WriteFile(filepath.Join(vimDir, ".vimrc"), []byte("set number"), 0644))

	// New way:
	pack := testutil.SetupTestPack(t, "vim")
	pack.AddFile(t, ".vimrc", "set number")
}

func SetupTestPack added in v0.1.3

func SetupTestPack(t *testing.T, packName string) *TestPack

SetupTestPack creates a basic test pack structure

func SetupTestPackWithHome added in v0.1.3

func SetupTestPackWithHome(t *testing.T, packName string) (*TestPack, string)

SetupTestPackWithHome creates a test pack with home directory

func (*TestPack) AddCommonDotfile added in v0.1.3

func (tp *TestPack) AddCommonDotfile(t *testing.T, filename string) string

AddCommonDotfile adds a common dotfile with standard content

func (*TestPack) AddDodotConfig added in v0.1.3

func (tp *TestPack) AddDodotConfig(t *testing.T, config string)

AddDodotConfig adds a .dodot.toml configuration to the test pack

func (*TestPack) AddExecutable added in v0.1.3

func (tp *TestPack) AddExecutable(t *testing.T, filename, content string) string

AddExecutable adds an executable file to the test pack

func (*TestPack) AddFile added in v0.1.3

func (tp *TestPack) AddFile(t *testing.T, filename, content string) string

AddFile adds a file to the test pack

func (*TestPack) AddStandardConfig added in v0.1.3

func (tp *TestPack) AddStandardConfig(t *testing.T, configType string)

AddStandardConfig adds a standard configuration for common scenarios

func (*TestPack) AddSymlinkRule added in v0.1.3

func (tp *TestPack) AddSymlinkRule(t *testing.T, pattern string)

AddSymlinkRule adds a standard symlink rule to the pack's config

Jump to

Keyboard shortcuts

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