testutil

package
v1.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertFileContains

func AssertFileContains(t *testing.T, filePath, expected string)

AssertFileContains verifies that a file contains expected content

func AssertFileNotContains

func AssertFileNotContains(t *testing.T, filePath, unexpected string)

AssertFileNotContains verifies that a file does not contain specific content

func ChecksumAwareDownloadFn

func ChecksumAwareDownloadFn(
	t *testing.T,
) func(context.Context, string, string, downloader.RetryConfig) error

ChecksumAwareDownloadFn returns a downloadFn for commands.InstallGitHubBinary tests: it writes fake archive bytes to the archive destination and a matching sha256sum-format checksums file to the checksums destination, so the helper's mandatory SHA-256 verification passes with zero network access. The checksums destination is recognized by the "-checksums.txt" suffix InstallGitHubBinary uses; the archive is always requested first, so its file name is known by the time the checksums file is written.

func CreateGlobalConfigFile

func CreateGlobalConfigFile(t *testing.T, configDir string, content string) string

CreateGlobalConfigFile creates a global_config.yaml file with specified content

func CreateGlobalConfigTemplate

func CreateGlobalConfigTemplate(t *testing.T, templatesDir string)

CreateGlobalConfigTemplate creates a basic global_config.yaml template

func CreateShellConfigTemplate

func CreateShellConfigTemplate(t *testing.T, templatesDir string, content string)

CreateShellConfigTemplate creates a basic devgeta.zsh.tmpl template

func InitLogger

func InitLogger()

InitLogger initializes the logger for tests Call this in your init() function

func IsolateXDGDirs

func IsolateXDGDirs(t *testing.T) string

IsolateXDGDirs redirects all XDG base directories to a temp directory. This prevents tests from reading, writing, or deleting real user data in ~/.local/share, ~/.local/state, ~/.cache, or ~/.config.

CRITICAL: Any test that calls Uninstall(), ForceInstall(), or any code path that uses paths.GetDataDir, paths.GetStateDir, paths.GetCacheDir, or paths.GetConfigDir MUST call this helper first.

Usage:

testutil.IsolateXDGDirs(t)
// XDG dirs now point to temp directories; restored automatically after test

func SetupTestDirs

func SetupTestDirs(t *testing.T) (appDir, configDir, templatesDir, devgetaConfigDir string)

SetupTestDirs creates a complete test directory structure Returns paths to key directories

func VerifyNoRealCommands

func VerifyNoRealCommands(t *testing.T, mockBase *commands.MockBaseCommand)

VerifyNoRealCommands verifies that no real commands were executed

func VerifyNoRealConfigChanges

func VerifyNoRealConfigChanges(t *testing.T)

VerifyNoRealConfigChanges verifies that no real configuration files were modified

Types

type MockApp

type MockApp struct {
	Cmd  *commands.MockCommand
	Base *commands.MockBaseCommand
}

NewMockApp creates a mock app instance with isolated mocks This ensures tests don't execute real commands

func NewMockApp

func NewMockApp() *MockApp

NewMockApp creates a new mock app instance

func (*MockApp) Reset

func (m *MockApp) Reset()

Reset resets all mock state

type PathsCleanup

type PathsCleanup func()

PathsCleanup is a function that restores original paths

func SetupIsolatedPaths

func SetupIsolatedPaths(t *testing.T) PathsCleanup

SetupIsolatedPaths overrides global path variables with test-specific paths Returns a cleanup function to restore original paths

This ensures tests don't: - Read from real configuration directories - Write to real configuration directories - Execute real system commands - Modify the user's actual .zshrc or shell configuration

Usage:

cleanup := testutil.SetupIsolatedPaths(t)
defer cleanup()

func SetupTestEnvironment

func SetupTestEnvironment(t *testing.T) (appDir, configDir, templatesDir string, cleanup PathsCleanup)

SetupTestEnvironment creates isolated paths and directory structure Returns paths and cleanup function

Usage:

appDir, configDir, templatesDir, cleanup := testutil.SetupTestEnvironment(t)
defer cleanup()

type TestConfig

type TestConfig struct {
	AppDir        string
	ConfigDir     string
	TemplatesDir  string
	Cleanup       PathsCleanup
	MockApp       *MockApp
	ConfigPath    string
	ZshConfigPath string
}

TestConfig holds common test configuration

func SetupCompleteTest

func SetupCompleteTest(t *testing.T) *TestConfig

SetupCompleteTest creates a complete test environment with all common setup This is the most convenient function for testing apps that use configuration

Usage:

tc := testutil.SetupCompleteTest(t)
defer tc.Cleanup()

// Use tc.MockApp for mock instances
// Use tc.AppDir, tc.ConfigDir, etc. for paths

type TestPaths

type TestPaths struct {
	AppRoot            string
	ConfigRoot         string
	TemplatesConfigDir string
}

TestPaths holds the original path values for restoration after tests

Jump to

Keyboard shortcuts

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