Get the FREE Ultimate OpenClaw Setup Guide →

bats-test-scaffolder

npx machina-cli add skill a5c-ai/babysitter/bats-test-scaffolder --openclaw
Files (1)
SKILL.md
3.2 KB

BATS Test Scaffolder

Generate BATS test structure for shell script testing.

Capabilities

  • Generate BATS test files
  • Create setup and teardown fixtures
  • Implement custom assertions
  • Set up mocking helpers
  • Configure test isolation
  • Generate test helpers

Usage

Invoke this skill when you need to:

  • Set up BATS testing for shell scripts
  • Create test fixtures and helpers
  • Implement mock functions
  • Generate test cases

Generated Test Structure

tests/
├── bats/
│   └── bats-*.bash        # BATS submodules
├── test_helper/
│   ├── common-setup.bash  # Common setup
│   └── mocks.bash         # Mock helpers
├── fixtures/
│   ├── input.txt          # Test fixtures
│   └── expected.txt
└── *.bats                  # Test files

Test File Template

#!/usr/bin/env bats

# Load test helpers
load 'test_helper/common-setup'

# Setup runs before each test
setup() {
    common_setup
    # Test-specific setup
    export TEST_DIR="$(mktemp -d)"
}

# Teardown runs after each test
teardown() {
    # Cleanup
    rm -rf "${TEST_DIR}"
}

@test "script displays help with --help" {
    run ./script.sh --help

    assert_success
    assert_output --partial "Usage:"
}

@test "script fails without required argument" {
    run ./script.sh

    assert_failure
    assert_output --partial "Missing required argument"
}

@test "script processes input file" {
    cp fixtures/input.txt "${TEST_DIR}/"

    run ./script.sh "${TEST_DIR}/input.txt"

    assert_success
    assert_output --partial "Processing complete"
}

@test "script creates output file" {
    run ./script.sh -o "${TEST_DIR}/output.txt" fixtures/input.txt

    assert_success
    assert_file_exists "${TEST_DIR}/output.txt"
}

@test "script handles special characters in filename" {
    local special_file="${TEST_DIR}/file with spaces.txt"
    echo "test" > "${special_file}"

    run ./script.sh "${special_file}"

    assert_success
}

Test Helper (test_helper/common-setup.bash)

#!/usr/bin/env bash

# Load BATS support libraries
load 'bats/bats-support/load'
load 'bats/bats-assert/load'
load 'bats/bats-file/load'

# Common setup for all tests
common_setup() {
    # Get the containing directory
    PROJECT_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." && pwd)"

    # Add scripts to PATH
    PATH="${PROJECT_ROOT}/bin:${PATH}"

    # Set up test temp directory
    TEST_TEMP_DIR="$(mktemp -d)"
}

# Common teardown
common_teardown() {
    rm -rf "${TEST_TEMP_DIR}"
}

# Custom assertion: check exit code
assert_exit_code() {
    local expected="$1"
    if [[ "${status}" -ne "${expected}" ]]; then
        echo "Expected exit code ${expected}, got ${status}" >&2
        return 1
    fi
}

# Mock a command
mock_command() {
    local cmd="$1"
    local response="$2"

    eval "${cmd}() { echo '${response}'; }"
    export -f "${cmd}"
}

Target Processes

  • shell-script-development
  • cli-unit-integration-testing
  • cross-platform-cli-compatibility

Source

git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/bats-test-scaffolder/SKILL.mdView on GitHub

Overview

Generates a ready-to-run BATS test suite for shell scripts, including fixtures, setup/teardown, mocks, and assertion helpers. It creates a standard project structure that isolates tests and simplifies mocking.

How This Skill Works

The skill outputs a bats-friendly layout under tests/, including bats-*.bash modules, test_helper/common-setup.bash, and mocks.bash. It provides a test template showing setup(), teardown(), and several example tests that exercise help output, argument validation, and file processing with fixtures and mock commands.

When to Use It

  • Starting a new shell-script project and you need robust, repeatable tests
  • Adding unit and integration tests for a CLI shell script
  • Creating consistent scaffolding for fixtures, mocks, and helpers
  • Ensuring test isolation with a shared setup/teardown across tests
  • Preparing cross-platform or edge-case testing for CLI tools

Quick Start

  1. Step 1: Generate the standard structure (tests/bats, test_helper/, fixtures/)
  2. Step 2: Fill in a test file from the template with setup/teardown and assertions
  3. Step 3: Populate fixtures and mocks in test_helper/common-setup.bash and mocks.bash

Best Practices

  • Follow the generated structure and keep fixtures in fixtures/
  • Use mocks.bash to simulate external commands and side effects
  • Leverage setup()/teardown() to ensure test isolation and cleanup
  • Write focused tests for common edge cases (help, missing args, special chars)
  • Regularly run and maintain the scaffold with BATS tooling

Example Use Cases

  • Bootstrapping tests for a deploy.sh CLI tool
  • Testing a data-processing script with fixtures/input.txt and fixtures/expected.txt
  • Mocking network calls in a CLI script via mocks.bash
  • Verifying stable help output across script updates
  • Cross-platform path and filename handling tests in different environments

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers