Documentation
¶
Overview ¶
Package testutil provides test environment setup and helpers for stellar-cli tests. It enables complete test isolation by redirecting all paths to temporary directories.
Index ¶
- func CaptureOutput(t *testing.T, fn func()) string
- func InvalidTOML() string
- func LargeTOML() string
- func RequireSymlinks(t *testing.T)
- func SampleTOML() string
- func SampleTOMLWithCustom() string
- type MockAPIHandler
- type MockTheme
- type MockVersion
- type TestEnv
- func (e *TestEnv) CreateConfig(content string) string
- func (e *TestEnv) CreateStarshipConfig(content string)
- func (e *TestEnv) CreateThemeFile(author, name, version, content string) string
- func (e *TestEnv) CreateTmpThemeFile(author, name, version, content string) string
- func (e *TestEnv) FileExists(path string) bool
- func (e *TestEnv) IsSymlink(path string) bool
- func (e *TestEnv) ReadFile(path string) string
- func (e *TestEnv) ReadSymlink(path string) string
- func (e *TestEnv) SetupMockAPI(handler *MockAPIHandler)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureOutput ¶ added in v1.2.0
CaptureOutput runs fn with stdout redirected to a pipe and returns everything written to it. It swaps both os.Stdout and color.Output: fatih/color captures os.Stdout at package-init time, so reassigning os.Stdout alone would miss any color.* output. Both are restored before returning. The pipe is drained in a goroutine so large output can't deadlock on the pipe buffer.
The restore is deferred and idempotent: if fn panics (e.g. a t.Fatal inside it), os.Stdout/color.Output are still put back before the panic propagates, instead of staying pointed at a pipe that no longer has a reader for every later test in the process.
func RequireSymlinks ¶ added in v1.2.0
RequireSymlinks skips the test if the environment can't create symlinks (e.g. Windows without Developer Mode or admin privileges). Call it at the top of any test that exercises symlink-mode behavior directly.
func SampleTOMLWithCustom ¶
func SampleTOMLWithCustom() string
SampleTOMLWithCustom returns a sample config with custom commands
Types ¶
type MockAPIHandler ¶
type MockAPIHandler struct {
DownloadCounts map[string]int // Track download increments for verification
// contains filtered or unexported fields
}
MockAPIHandler implements http.Handler for testing
func CreateDefaultMockAPI ¶
func CreateDefaultMockAPI() *MockAPIHandler
CreateDefaultMockAPI creates a mock API with sample themes for testing
func NewMockAPIHandler ¶
func NewMockAPIHandler() *MockAPIHandler
NewMockAPIHandler creates a new mock API handler
func (*MockAPIHandler) AddTheme ¶
func (h *MockAPIHandler) AddTheme(theme MockTheme)
AddTheme adds a theme to the mock API
func (*MockAPIHandler) GetDownloadCount ¶
func (h *MockAPIHandler) GetDownloadCount(author, slug string) int
GetDownloadCount returns the current download count for a theme
func (*MockAPIHandler) ServeHTTP ¶
func (h *MockAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler
type MockTheme ¶
type MockTheme struct {
ID string
Author string
Slug string
Name string
Description string
Downloads int
ColorScheme *string
Group string
Versions []MockVersion
CreatedAt string
UpdatedAt string
}
MockTheme represents a theme in the mock API
type MockVersion ¶
type MockVersion struct {
Version string
ConfigContent string // The actual TOML content
VersionNotes string
Dependencies []string
CreatedAt string
}
MockVersion represents a theme version
type TestEnv ¶
type TestEnv struct {
RootDir string // Root temp directory
StellarDir string // ~/.config/stellar equivalent
StarshipPath string // ~/.config/starship.toml equivalent
TmpDir string // /tmp/stellar equivalent
// Mock API server (if set up)
MockServer *httptest.Server
// contains filtered or unexported fields
}
TestEnv holds the test environment configuration
func SetupTestEnv ¶
SetupTestEnv creates an isolated test environment with temporary directories and sets environment variables to redirect all paths. Cleanup is automatic via t.Cleanup().
func (*TestEnv) CreateConfig ¶
CreateConfig creates a config.json file in the test stellar directory
func (*TestEnv) CreateStarshipConfig ¶
CreateStarshipConfig creates a starship.toml file (non-symlink)
func (*TestEnv) CreateThemeFile ¶
CreateThemeFile creates a theme config file in the test cache
func (*TestEnv) CreateTmpThemeFile ¶
CreateTmpThemeFile creates a theme file in the tmp cache (for preview)
func (*TestEnv) FileExists ¶
FileExists checks if a file exists
func (*TestEnv) ReadFile ¶
ReadFile reads a file and returns its contents, failing the test on error
func (*TestEnv) ReadSymlink ¶
ReadSymlink returns the target of a symlink
func (*TestEnv) SetupMockAPI ¶
func (e *TestEnv) SetupMockAPI(handler *MockAPIHandler)
SetupMockAPI starts a mock API server and sets STELLAR_API_URL