testutil

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureOutput added in v1.2.0

func CaptureOutput(t *testing.T, fn func()) string

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 InvalidTOML

func InvalidTOML() string

InvalidTOML returns an invalid TOML string

func LargeTOML

func LargeTOML() string

LargeTOML returns a TOML string larger than 100KB

func RequireSymlinks(t *testing.T)

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 SampleTOML

func SampleTOML() string

SampleTOML returns a valid sample starship config

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

func SetupTestEnv(t *testing.T) *TestEnv

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

func (e *TestEnv) CreateConfig(content string) string

CreateConfig creates a config.json file in the test stellar directory

func (*TestEnv) CreateStarshipConfig

func (e *TestEnv) CreateStarshipConfig(content string)

CreateStarshipConfig creates a starship.toml file (non-symlink)

func (*TestEnv) CreateThemeFile

func (e *TestEnv) CreateThemeFile(author, name, version, content string) string

CreateThemeFile creates a theme config file in the test cache

func (*TestEnv) CreateTmpThemeFile

func (e *TestEnv) CreateTmpThemeFile(author, name, version, content string) string

CreateTmpThemeFile creates a theme file in the tmp cache (for preview)

func (*TestEnv) FileExists

func (e *TestEnv) FileExists(path string) bool

FileExists checks if a file exists

func (e *TestEnv) IsSymlink(path string) bool

IsSymlink checks if a path is a symlink

func (*TestEnv) ReadFile

func (e *TestEnv) ReadFile(path string) string

ReadFile reads a file and returns its contents, failing the test on error

func (e *TestEnv) ReadSymlink(path string) string

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

Jump to

Keyboard shortcuts

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