testutil

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: MIT Imports: 11 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

Constants

This section is empty.

Variables

This section is empty.

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 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 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 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 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 Setenv

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

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

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 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
}

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) 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{})
}

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.

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

Jump to

Keyboard shortcuts

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