testutil

package
v0.74.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 8 Imported by: 0

README

testutil Package

The testutil package provides shared test helpers for isolating test artifacts and capturing output.

Overview

This package is imported only in test files (_test.go). It provides:

  • A shared, isolated temporary directory for each test run (outside the git repository).
  • Per-test subdirectories that are cleaned up automatically.
  • Helpers for capturing os.Stderr output during tests.
  • A helper for stripping YAML comment headers from compiled workflow output.

Public API

Functions
Function Signature Description
GetTestRunDir func() string Returns the path to the unique top-level directory for the current test run, created once per process under $TMPDIR/gh-aw-test-runs/<timestamp>-<pid>
TempDir func(t *testing.T, pattern string) string Creates a temporary subdirectory inside the test run directory matching pattern; the directory is automatically removed when the test completes via t.Cleanup
CaptureStderr func(t *testing.T, fn func()) string Runs fn and returns everything written to os.Stderr during its execution; os.Stderr is restored automatically via t.Cleanup
StripYAMLCommentHeader func(yamlContent string) string Removes the leading comment block from a generated YAML file and returns only the non-comment content

Usage Examples

import "github.com/github/gh-aw/pkg/testutil"

func TestMyFunction(t *testing.T) {
    // Create an isolated temp directory for test artifacts
    dir := testutil.TempDir(t, "my-test-*")
    // dir is cleaned up automatically when the test ends

    // Capture output written to os.Stderr
    output := testutil.CaptureStderr(t, func() {
        myFunction() // function that writes to os.Stderr
    })
    assert.Contains(t, output, "expected message")
}

Design Notes

  • GetTestRunDir uses sync.Once so the directory is created exactly once per process even when multiple test packages run concurrently.
  • TempDir delegates to os.MkdirTemp to generate unique subdirectory names.
  • Test artifacts placed in the test run directory are outside any git repository, which prevents git commands executed by tests from picking them up as untracked files.

This specification is automatically maintained by the spec-extractor workflow.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureStderr added in v0.50.2

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

CaptureStderr runs fn and returns everything written to os.Stderr during its execution. It restores os.Stderr automatically via t.Cleanup.

func GetTestRunDir

func GetTestRunDir() string

GetTestRunDir returns the unique directory for this test run. It creates a directory in the system temp directory with a unique subdirectory based on the current timestamp and process ID. This ensures test directories are completely isolated from any git repository.

func StripYAMLCommentHeader

func StripYAMLCommentHeader(yamlContent string) string

StripYAMLCommentHeader removes the comment header from generated YAML files and returns only the non-comment YAML content. This is useful for tests that need to verify content without matching strings in the comment header.

func TempDir

func TempDir(t *testing.T, pattern string) string

TempDir creates a temporary directory for testing within the test run directory. It automatically cleans up the directory when the test completes. This replaces the use of os.MkdirTemp or t.TempDir() to ensure all test artifacts are isolated in a known location outside any git repository.

Types

This section is empty.

Jump to

Keyboard shortcuts

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