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 ¶
- func AssertDir(t *testing.T, path string)
- func AssertDirEmpty(t *testing.T, path string)
- func AssertDirHasEntries(t *testing.T, path string, count int)
- func AssertFile(t *testing.T, path, expectedContent string)
- func AssertFileContains(t *testing.T, path, expectedSubstring string)
- func AssertFileMode(t *testing.T, path string, expectedMode os.FileMode)
- func AssertLink(t *testing.T, linkPath, expectedTarget string)
- func AssertLinkContains(t *testing.T, linkPath, targetSubstring string)
- func AssertNotExists(t *testing.T, path string)
- func AssertStateChanges(t *testing.T, before, after *StateSnapshot, expectedChanges []string)
- func AssertStateUnchanged(t *testing.T, before, after *StateSnapshot)
- func AssertSymlinkChain(t *testing.T, linkPath string, expectedChain []string)
- func CompareStates(t *testing.T, before, after *StateSnapshot) []string
- func NewTestClient(t testing.TB, env *TestEnvironment) *dot.Client
- func NewTestClientWithOptions(t testing.TB, env *TestEnvironment, opts ClientOptions) *dot.Client
- type ClientOptions
- type FileState
- type FileTreeBuilder
- func (ftb *FileTreeBuilder) Dir(path string) *FileTreeBuilder
- func (ftb *FileTreeBuilder) File(path, content string) *FileTreeBuilder
- func (ftb *FileTreeBuilder) FileWithMode(path, content string, mode os.FileMode) *FileTreeBuilder
- func (ftb *FileTreeBuilder) Symlink(oldname, newname string) *FileTreeBuilder
- type FixtureBuilder
- type GoldenTest
- type GoldenTestSuite
- type PackageBuilder
- type StateSnapshot
- type TestEnvironment
- func (te *TestEnvironment) AddCleanup(fn func())
- func (te *TestEnvironment) Cleanup()
- func (te *TestEnvironment) Context() context.Context
- func (te *TestEnvironment) CreatePackage(name string, files map[string]string) string
- func (te *TestEnvironment) CreateSimplePackage(name, filename, content string) string
- func (te *TestEnvironment) FixtureBuilder() *FixtureBuilder
- func (te *TestEnvironment) ManifestPath() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertDirEmpty ¶
AssertDirEmpty verifies a directory is empty.
func AssertDirHasEntries ¶
AssertDirHasEntries verifies a directory has expected number of entries.
func AssertFile ¶
AssertFile verifies a file exists and has expected content.
func AssertFileContains ¶
AssertFileContains verifies a file exists and contains expected substring.
func AssertFileMode ¶
AssertFileMode verifies a file has expected permissions.
func AssertLink ¶
AssertLink verifies a symlink exists and points to the expected target.
func AssertLinkContains ¶
AssertLinkContains verifies a symlink exists and its target contains the expected substring.
func AssertNotExists ¶
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 ¶
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 (*FileTreeBuilder) Symlink ¶
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 ¶
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 (*StateSnapshot) CountSymlinks ¶
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.