tests

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 5 Imported by: 0

README

Hitch Testing Framework

A simple, clean testing framework for Hitch that avoids complexity and provides easy-to-use utilities for testing git operations and Hitch functionality.

Quick Start

func TestYourFeature(t *testing.T) {
    testRepo := tests.NewTestRepo(t)
    defer testRepo.Cleanup()

    // Create files and commits
    testRepo.CreateFile("README.md", "# Test")
    testRepo.AddAndCommit("Initial commit")

    // Create branches
    testRepo.CreateBranch("feature/test")
    testRepo.AssertCurrentBranch("feature/test")

    // Setup Hitch metadata
    testRepo.CreateBasicHitchMetadata()
    testRepo.AssertFileExists(".hitch/metadata.json")
}

Available Methods

Repository Setup
  • NewTestRepo(t *testing.T) - Creates a fresh temporary git repository
  • SetupGitConfig() - Configures git user for the test repo
  • Cleanup() - Removes temporary files
File Operations
  • CreateFile(path, content string) - Creates a file with content
  • AssertFileExists(path string) - Verifies a file exists
  • AssertFileContains(path, content string) - Verifies file contains content
Git Operations
  • AddAndCommit(message string) - Stage and commit all changes
  • CreateBranch(name string) - Create and switch to new branch
  • Checkout(branch string) - Switch to existing branch
  • GetCurrentBranch() string - Get current branch name
  • AssertCurrentBranch(expected string) - Verify current branch
Hitch Metadata
  • CreateBasicHitchMetadata() - Creates standard dev/prod environments
  • WriteHitchFile(content string) - Write custom metadata JSON

Design Principles

  1. Simple over complex - Uses direct git commands, not go-git
  2. Secure by default - Uses 0600 file permissions
  3. Fast execution - Tests run in < 1 second
  4. Clean isolation - Each test gets a fresh repository
  5. Easy assertions - Built-in helpers for common verifications

Best Practices

  • Always call defer testRepo.Cleanup() immediately after NewTestRepo()
  • Use assertion helpers instead of manual checks for cleaner tests
  • Use CreateBasicHitchMetadata() for standard Hitch setup
  • Keep tests focused on single behaviors
  • Use descriptive commit messages in AddAndCommit()

Adding New Tests

  1. Create a new test function following Go conventions
  2. Use the helper methods for common operations
  3. Add assertions to verify expected behavior
  4. Follow the existing patterns in example_test.go

The framework is designed to be extended as needed while keeping simplicity and avoiding the complexity that caused issues with the previous test system.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestRepo

type TestRepo struct {
	T    *testing.T
	Path string
}

TestRepo provides a clean, simple test repository

func NewTestRepo

func NewTestRepo(t *testing.T) *TestRepo

NewTestRepo creates a fresh test repository for each test

func (*TestRepo) AddAndCommit

func (tr *TestRepo) AddAndCommit(message string)

AddAndCommit adds a file and commits it

func (*TestRepo) AssertCurrentBranch

func (tr *TestRepo) AssertCurrentBranch(expected string)

AssertCurrentBranch verifies we are on the expected branch

func (*TestRepo) AssertFileContains

func (tr *TestRepo) AssertFileContains(path, expectedContent string)

AssertFileContains verifies a file contains specific content

func (*TestRepo) AssertFileExists

func (tr *TestRepo) AssertFileExists(path string)

AssertFileExists verifies a file exists in the repository

func (*TestRepo) Checkout

func (tr *TestRepo) Checkout(branch string)

Checkout switches to a branch

func (*TestRepo) Cleanup

func (tr *TestRepo) Cleanup()

Cleanup removes the temporary repository

func (*TestRepo) CreateBasicHitchMetadata

func (tr *TestRepo) CreateBasicHitchMetadata()

CreateBasicHitchMetadata creates a simple Hitch metadata with default environments

func (*TestRepo) CreateBranch

func (tr *TestRepo) CreateBranch(name string)

CreateBranch creates a new branch from the current HEAD

func (*TestRepo) CreateCustomHitchMetadata

func (tr *TestRepo) CreateCustomHitchMetadata(environments map[string]string)

CreateCustomHitchMetadata creates Hitch metadata with custom environments

func (*TestRepo) CreateFile

func (tr *TestRepo) CreateFile(path, content string)

CreateFile creates a file with the given content

func (*TestRepo) GetCurrentBranch

func (tr *TestRepo) GetCurrentBranch() string

GetCurrentBranch returns the current branch name

func (*TestRepo) RunGit

func (tr *TestRepo) RunGit(args ...string) string

RunGit executes a git command in the test repository

func (*TestRepo) SetupGitConfig

func (tr *TestRepo) SetupGitConfig()

SetupGitConfig configures git user for the test repository

func (*TestRepo) WriteHitchFile

func (tr *TestRepo) WriteHitchFile(content string)

WriteHitchFile creates a basic .hitch directory and metadata file

Jump to

Keyboard shortcuts

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