testutil

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testutil provides utilities for integration testing.

This package includes:

  • FixtureBuilder: Create test packages and directory structures
  • TestEnvironment: Isolated test execution environment
  • Assertions: Specialized assertions for symlinks, files, and directories
  • StateSnapshot: Capture and compare filesystem states
  • GoldenTest: Compare outputs against golden files

Example usage:

func TestManageWorkflow(t *testing.T) {
    env := testutil.NewTestEnvironment(t)

    // Create test package
    env.FixtureBuilder().Package("vim").
        WithFile("dot-vimrc", "set nocompatible").
        Create()

    // Capture state before operation
    before := testutil.CaptureState(t, env.TargetDir)

    // Perform operation
    // ...

    // Capture state after operation
    after := testutil.CaptureState(t, env.TargetDir)

    // Verify changes
    testutil.AssertLink(t, filepath.Join(env.TargetDir, ".vimrc"), "...")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertDir

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

AssertDir verifies a directory exists.

func AssertDirEmpty

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

AssertDirEmpty verifies a directory is empty.

func AssertDirHasEntries

func AssertDirHasEntries(t *testing.T, path string, count int)

AssertDirHasEntries verifies a directory has expected number of entries.

func AssertFile

func AssertFile(t *testing.T, path, expectedContent string)

AssertFile verifies a file exists and has expected content.

func AssertFileContains

func AssertFileContains(t *testing.T, path, expectedSubstring string)

AssertFileContains verifies a file exists and contains expected substring.

func AssertFileMode

func AssertFileMode(t *testing.T, path string, expectedMode os.FileMode)

AssertFileMode verifies a file has expected permissions.

func AssertLink(t *testing.T, linkPath, expectedTarget string)

AssertLink verifies a symlink exists and points to the expected target.

func AssertLinkContains

func AssertLinkContains(t *testing.T, linkPath, targetSubstring string)

AssertLinkContains verifies a symlink exists and its target contains the expected substring.

func AssertNotExists

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

AssertNotExists verifies a path does not exist.

func AssertStateChanges

func AssertStateChanges(t *testing.T, before, after *StateSnapshot, expectedChanges []string)

AssertStateChanges verifies expected state changes occurred.

func AssertStateUnchanged

func AssertStateUnchanged(t *testing.T, before, after *StateSnapshot)

AssertStateUnchanged verifies that state has not changed.

func AssertSymlinkChain

func AssertSymlinkChain(t *testing.T, linkPath string, expectedChain []string)

AssertSymlinkChain verifies a chain of symlinks.

func CompareStates

func CompareStates(t *testing.T, before, after *StateSnapshot) []string

CompareStates compares two state snapshots and returns differences.

func NewTestClient

func NewTestClient(t testing.TB, env *TestEnvironment) *dot.Client

NewTestClient creates a dot client configured for the test environment.

func NewTestClientWithOptions

func NewTestClientWithOptions(t testing.TB, env *TestEnvironment, opts ClientOptions) *dot.Client

NewTestClientWithOptions creates a client with custom configuration.

Types

type ClientOptions

type ClientOptions struct {
	FS          dot.FS
	Logger      dot.Logger
	LinkMode    dot.LinkMode
	Folding     bool
	DryRun      bool
	Verbosity   int
	Concurrency int
}

ClientOptions holds custom options for client creation.

func DefaultClientOptions

func DefaultClientOptions() ClientOptions

DefaultClientOptions returns default client options.

type FileState

type FileState struct {
	Path      string
	IsDir     bool
	IsSymlink bool
	Target    string // for symlinks
	Mode      os.FileMode
	Size      int64
}

FileState represents the state of a file or directory.

type FileTreeBuilder

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

FileTreeBuilder builds arbitrary directory trees.

func (*FileTreeBuilder) Dir

func (ftb *FileTreeBuilder) Dir(path string) *FileTreeBuilder

Dir creates a directory.

func (*FileTreeBuilder) File

func (ftb *FileTreeBuilder) File(path, content string) *FileTreeBuilder

File creates a file with given content.

func (*FileTreeBuilder) FileWithMode

func (ftb *FileTreeBuilder) FileWithMode(path, content string, mode os.FileMode) *FileTreeBuilder

FileWithMode creates a file with specific permissions.

func (ftb *FileTreeBuilder) Symlink(oldname, newname string) *FileTreeBuilder

Symlink creates a symlink.

type FixtureBuilder

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

FixtureBuilder provides methods for building test fixtures.

func NewFixtureBuilder

func NewFixtureBuilder(t testing.TB, baseDir string) *FixtureBuilder

NewFixtureBuilder creates a new fixture builder.

func (*FixtureBuilder) FileTree

func (fb *FixtureBuilder) FileTree(base string) *FileTreeBuilder

FileTree starts building a file tree at the given base path.

func (*FixtureBuilder) Package

func (fb *FixtureBuilder) Package(name string) *PackageBuilder

Package starts building a new package.

type GoldenTest

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

GoldenTest provides golden file testing capabilities.

func NewGoldenTest

func NewGoldenTest(t *testing.T, goldenDir, testName, extension string) *GoldenTest

NewGoldenTest creates a new golden test.

func (*GoldenTest) AssertMatch

func (gt *GoldenTest) AssertMatch(actual string)

AssertMatch compares actual content with golden file.

func (*GoldenTest) AssertMatchBytes

func (gt *GoldenTest) AssertMatchBytes(actual []byte)

AssertMatchBytes compares actual bytes with golden file.

func (*GoldenTest) Exists

func (gt *GoldenTest) Exists() bool

Exists checks if the golden file exists.

func (*GoldenTest) Path

func (gt *GoldenTest) Path() string

Path returns the path to the golden file.

func (*GoldenTest) Update

func (gt *GoldenTest) Update(content string)

Update forces update of the golden file with given content.

type GoldenTestSuite

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

GoldenTestSuite manages multiple golden tests for a test suite.

func NewGoldenTestSuite

func NewGoldenTestSuite(t *testing.T, goldenDir string) *GoldenTestSuite

NewGoldenTestSuite creates a new golden test suite.

func (*GoldenTestSuite) JSONTest

func (gts *GoldenTestSuite) JSONTest(testName string) *GoldenTest

JSONTest creates a golden test for JSON output.

func (*GoldenTestSuite) Test

func (gts *GoldenTestSuite) Test(testName, extension string) *GoldenTest

Test creates a golden test for a specific test case.

func (*GoldenTestSuite) TextTest

func (gts *GoldenTestSuite) TextTest(testName string) *GoldenTest

TextTest creates a golden test for text output.

func (*GoldenTestSuite) YAMLTest

func (gts *GoldenTestSuite) YAMLTest(testName string) *GoldenTest

YAMLTest creates a golden test for YAML output.

type PackageBuilder

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

PackageBuilder builds test packages.

func (*PackageBuilder) Create

func (pb *PackageBuilder) Create() string

Create creates the package on the filesystem.

func (*PackageBuilder) WithDir

func (pb *PackageBuilder) WithDir(path string) *PackageBuilder

WithDir adds an empty directory to the package.

func (*PackageBuilder) WithFile

func (pb *PackageBuilder) WithFile(path, content string) *PackageBuilder

WithFile adds a file to the package with given content.

type StateSnapshot

type StateSnapshot struct {
	Root  string
	Files []FileState
}

StateSnapshot captures the state of a directory tree.

func CaptureState

func CaptureState(t *testing.T, root string) *StateSnapshot

CaptureState captures the current state of a directory tree.

func (*StateSnapshot) CountDirs

func (s *StateSnapshot) CountDirs() int

CountDirs counts the number of directories in a snapshot.

func (*StateSnapshot) CountFiles

func (s *StateSnapshot) CountFiles() int

CountFiles counts the number of files (not directories) in a snapshot.

func (s *StateSnapshot) CountSymlinks() int

CountSymlinks counts the number of symlinks in a snapshot.

func (*StateSnapshot) GetState

func (s *StateSnapshot) GetState(path string) (FileState, bool)

GetState returns the state of a specific path.

func (*StateSnapshot) HasPath

func (s *StateSnapshot) HasPath(path string) bool

HasPath checks if a path exists in the snapshot.

type TestEnvironment

type TestEnvironment struct {
	PackageDir string
	TargetDir  string
	// contains filtered or unexported fields
}

TestEnvironment provides an isolated environment for integration tests.

func NewTestEnvironment

func NewTestEnvironment(t testing.TB) *TestEnvironment

NewTestEnvironment creates a new isolated test environment.

func WithTimeout

func WithTimeout(t testing.TB, timeout time.Duration) *TestEnvironment

WithTimeout creates a new environment with custom timeout.

func (*TestEnvironment) AddCleanup

func (te *TestEnvironment) AddCleanup(fn func())

AddCleanup registers a cleanup function to be called during cleanup.

func (*TestEnvironment) Cleanup

func (te *TestEnvironment) Cleanup()

Cleanup performs cleanup operations.

func (*TestEnvironment) Context

func (te *TestEnvironment) Context() context.Context

Context returns the test context.

func (*TestEnvironment) CreatePackage

func (te *TestEnvironment) CreatePackage(name string, files map[string]string) string

CreatePackage creates a simple test package with files.

func (*TestEnvironment) CreateSimplePackage

func (te *TestEnvironment) CreateSimplePackage(name, filename, content string) string

CreateSimplePackage creates a package with a single dotfile.

func (*TestEnvironment) FixtureBuilder

func (te *TestEnvironment) FixtureBuilder() *FixtureBuilder

FixtureBuilder returns a fixture builder for this environment.

func (*TestEnvironment) ManifestPath

func (te *TestEnvironment) ManifestPath() string

ManifestPath returns the path where manifest would be stored.

Jump to

Keyboard shortcuts

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