跳到主要内容
知仓学习社ZHICANG

bats-test-scaffolder

Generate BATS test structure and fixtures for shell script testing with setup/teardown, assertions, and mocking.

不碰外部(只输出文字)无严重或高危命中a5c-ai/babysitter

它会碰到什么

扫了多少2 个文本文件,4 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

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

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 1,796
本站分层T1
该仓技能数2115
原文件路径library/specializations/cli-mcp-development/skills/bats-test-scaffolder/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 2115 个技能