testutil

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package testutil provides utilities for creating isolated, hermetic test environments

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertPathsEqual

func AssertPathsEqual(t *testing.T, expected, actual, message string)

AssertPathsEqual compares two paths after canonicalizing them to handle symlink differences across operating systems.

Usage:

testutil.AssertPathsEqual(t, expectedPath, actualPath, "project root should match")

func AutoCleanupTestContainers

func AutoCleanupTestContainers() error

AutoCleanupTestContainers automatically cleans up test containers based on the current REACTOR_ISOLATION_PREFIX environment variable. This should be called in test cleanup functions.

func CanonicalPath

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

CanonicalPath resolves symlinks and returns the canonical absolute path. This is useful for path comparisons in tests that might encounter symlink differences between different operating systems (e.g., /var vs /private/var on macOS).

Usage:

expected := testutil.CanonicalPath(t, expectedPath)
actual := testutil.CanonicalPath(t, actualPath)
assert.Equal(t, expected, actual)

func CleanupAllTestContainers

func CleanupAllTestContainers() error

CleanupAllTestContainers cleans up all test containers that match common test prefixes. This is useful for cleaning up accumulated state from previous failed test runs.

func CleanupTestContainers

func CleanupTestContainers(isolationPrefix string) error

CleanupTestContainers provides comprehensive cleanup for reactor containers created during integration tests. This function implements the self-cleaning container approach to resolve permission issues where containers create files with different ownership that Go test cleanup cannot remove.

func RobustRemoveAll

func RobustRemoveAll(t *testing.T, path string) error

RobustRemoveAll attempts standard removal first, falling back to Docker-based force removal if permission errors occur. This is the primary cleanup function that should be used for test directories that may contain Docker-created files.

func SetupIsolatedTest

func SetupIsolatedTest(t *testing.T) (homeDir, workspaceDir string, cleanup func())

SetupIsolatedTest provides complete test isolation including HOME directory, workspace directory, and robust cleanup for Docker-created files. This is the most comprehensive test isolation helper.

The cleanup function now uses RobustRemoveAll which handles permission issues caused by Docker containers creating files with different ownership.

Usage:

func TestSomething(t *testing.T) {
    homeDir, workspaceDir, cleanup := testutil.SetupIsolatedTest(t)
    defer cleanup() // Recommended for tests that use Docker

    // Change to workspace directory for the test
    originalWD, _ := os.Getwd()
    os.Chdir(workspaceDir)
    defer os.Chdir(originalWD)
}

func WithIsolatedHome

func WithIsolatedHome(t *testing.T) string

WithIsolatedHome creates a temporary home directory and sets the HOME environment variable for the duration of the test. This ensures tests run in a clean, isolated environment without depending on the actual user's home directory.

Usage:

func TestSomething(t *testing.T) {
    testutil.WithIsolatedHome(t)
    // Test code that depends on HOME directory
}

func WithIsolatedWorkspace

func WithIsolatedWorkspace(t *testing.T) (homeDir, workspaceDir string)

WithIsolatedWorkspace creates both an isolated home directory and a temporary workspace directory for tests that need both. Returns both paths.

Usage:

func TestSomething(t *testing.T) {
    homeDir, workspaceDir := testutil.WithIsolatedWorkspace(t)
    // Test code that needs both home and workspace isolation
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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