Skip to content

Testing Framework

Adrian Thomas edited this page Oct 2, 2025 · 8 revisions

Testing Framework Guide

This guide explains symlinkit 2.0's testing system using bats (Bash Automated Testing System), how to run tests, add new tests, and ensure your contributions meet quality standards.


Overview

symlinkit 2.0 uses bats for automated testing with a comprehensive suite of 27 tests covering all functionality.

Key Principles

  • Bats-based Testing - Professional, standardized testing framework
  • Comprehensive Coverage - Tests cover all CRUD operations, inspection tools, and edge cases
  • Cross-platform Support - Tests work on Linux, macOS (Bash 3.2+), and WSL
  • No Optional Dependencies - Tests run with bats and core tools only
  • Clear Reporting - TAP-compliant output with pass/fail information
  • CI/CD Integration - All tests run automatically on GitHub Actions

Getting Started with Tests

Quick Start

# Run the complete test suite
make test

# Or run bats directly
bats tests/symlinkit.bats

# Run a specific test
bats tests/symlinkit.bats --filter "Version flag"

# Run with verbose output
bats tests/symlinkit.bats --verbose-run

Test Requirements

Required:

  • Bash 3.2+
  • bats - Bash Automated Testing System
  • realpath (Linux/WSL) or grealpath (macOS via coreutils)

Installing bats:

# Ubuntu/Debian
sudo apt install bats

# macOS
brew install bats-core

# Manual install
git clone https://github.com/bats-core/bats-core.git
cd bats-core
sudo ./install.sh /usr/local

Supported platforms:

  • ✅ Linux (all distributions)
  • ✅ macOS (via Homebrew coreutils)
  • ✅ WSL

Test Architecture

Test Suite Structure

tests/
├── symlinkit.bats    # Complete bats test suite (27 tests)
└── helpers.bash      # Test setup, teardown, and helper functions

Test File Components

helpers.bash:

setup() {
  TMPDIR=$(mktemp -d)
  mkdir -p "$TMPDIR"/{source,dest,json_test}
  echo "test file" > "$TMPDIR/source/file.txt"
  ln -sf "$TMPDIR/source/file.txt" "$TMPDIR/json_test/good_link"
  ln -sf "/nonexistent/path" "$TMPDIR/json_test/broken_link"
}

teardown() {
  rm -rf "$TMPDIR"
}

run_symlinkit() {
  "$BATS_TEST_DIRNAME/../symlinkit" "$@"
}

symlinkit.bats:

#!/usr/bin/env bats

load ./helpers.bash

@test "Version flag" {
  run run_symlinkit --version
  [ "$status" -eq 0 ]
  [[ "$output" =~ symlinkit ]]
}

Writing Tests with Bats

Basic Test Structure

@test "Test description" {
  run run_symlinkit --version
  [ "$status" -eq 0 ]
  [[ "$output" =~ symlinkit ]]
}

Testing Exit Codes

@test "Invalid flag combination -cd" {
  run run_symlinkit -cd
  [ "$status" -ne 0 ]  # Should fail
  [[ "$output" =~ Invalid ]]
}

Testing File Operations

@test "Create mode actual" {
  run run_symlinkit -c "$TMPDIR/source/file.txt" "$TMPDIR/dest/link"
  [ "$status" -eq 0 ]
  [ -L "$TMPDIR/dest/link" ]
}

Testing Dry-Run Modes

@test "Create mode dry-run" {
  run run_symlinkit --dry-run -c "$TMPDIR/source/file.txt" "$TMPDIR/dest/new_link"
  [ "$status" -eq 0 ]
  [[ "$output" =~ Would ]]
  [ ! -e "$TMPDIR/dest/new_link" ]  # Should not exist
}

Testing Interactive Commands

@test "Merge mode actual" {
  run bash -c "echo s | $BATS_TEST_DIRNAME/../symlinkit -m \"$TMPDIR/source\" \"$TMPDIR/dest\""
  [ "$status" -eq 0 ]
  [ -d "$TMPDIR/dest" ]
}

Testing Error Conditions

@test "Create mode fails if exists" {
  ln -s "$TMPDIR/source/file.txt" "$TMPDIR/dest/existing"
  run run_symlinkit -c "$TMPDIR/source/file.txt" "$TMPDIR/dest/existing"
  [ "$status" -ne 0 ]
  [[ "$output" =~ exists ]]
}

Running Tests

Local Testing

# Run full test suite (recommended)
make test

# Or run bats directly
bats tests/symlinkit.bats

# Run specific test
bats tests/symlinkit.bats --filter "Version flag"

# Run with timing information
bats tests/symlinkit.bats --timing

# Run with verbose output
bats tests/symlinkit.bats --verbose-run

# Check exit code
echo $?  # 0 = all passed, 1 = some failed

GitHub Actions

Tests run automatically on:

  • Pull requests to main
  • Pushes to main

Workflow: .github/workflows/test.yml

Test matrix:

  • ubuntu-latest
  • macos-latest

Test Coverage

Current Tests (v2.0.3)

The test suite includes 27 comprehensive tests:

Basic Commands (5 tests):

  • Version flag
  • Help flag
  • Invalid flag combination -cd
  • Invalid flag combination -md
  • Invalid flag combination -co

CRUD Operations (12 tests):

  • Create mode (dry-run and actual)
  • Create mode (fail if exists)
  • Require operation flag with source path
  • Overwrite mode (dry-run and actual)
  • Merge mode (dry-run and actual)
  • Delete mode (dry-run and actual)
  • Delete preserves source file

Recursive Operations (5 tests):

  • Recursive delete (dry-run)
  • Recursive delete (dry-run with [a]ll)
  • Recursive delete (skip all)
  • Recursive delete (delete all)

Flag Chaining (2 tests):

  • -cr (create recursive)
  • -or (overwrite recursive)

Inspection Tools (3 tests):

  • List mode
  • Broken mode
  • Fix-broken with [a]ll delete option
  • Fix-broken dry-run with [a]ll delete
  • List mode with no symlinks

Adding New Tests

Test Development Workflow

  1. Identify the feature to test
  2. Write the test in tests/symlinkit.bats
  3. Use helpers from tests/helpers.bash
  4. Run locally to verify: make test
  5. Submit PR with test included

Example: Adding a New Test

# In tests/symlinkit.bats

@test "New feature description" {
  # Setup (if needed beyond standard setup)
  mkdir -p "$TMPDIR/custom_setup"

  # Run symlinkit
  run run_symlinkit --new-feature "$TMPDIR/custom_setup"

  # Assert exit code
  [ "$status" -eq 0 ]

  # Assert output contains expected string
  [[ "$output" =~ "expected output" ]]

  # Assert file system state
  [ -d "$TMPDIR/custom_setup" ]
}

Test Naming

Use clear, descriptive test names:

@test "Create mode dry-run"                    # Good
@test "Test 1"                                  # Bad

@test "Delete preserves source file"           # Good
@test "Check delete"                            # Bad

Best Practices

1. Test One Thing

Each test should verify a single behavior:

# GOOD: One test per behavior
@test "Create mode succeeds with valid paths" { ... }
@test "Create mode fails if destination exists" { ... }

# BAD: Multiple behaviors in one test
@test "Create mode works" { ... }

2. Use Setup/Teardown

Leverage bats' automatic setup/teardown:

# helpers.bash handles this automatically
setup() {
  TMPDIR=$(mktemp -d)
  # ... create test environment
}

teardown() {
  rm -rf "$TMPDIR"  # Automatic cleanup
}

3. Use Descriptive Assertions

# GOOD: Multiple clear assertions
[ "$status" -eq 0 ]
[[ "$output" =~ symlinkit ]]
[ -L "$link" ]

# BAD: Vague single assertion
[[ -n "$output" ]]

4. Test Edge Cases

Include tests for edge cases and error conditions:

@test "Create mode fails if destination exists" {
  ln -s "$TMPDIR/source/file.txt" "$TMPDIR/dest/existing"
  run run_symlinkit -c "$TMPDIR/source/file.txt" "$TMPDIR/dest/existing"
  [ "$status" -ne 0 ]
  [[ "$output" =~ exists ]]
}

5. Platform Compatibility

Ensure tests work on macOS (Bash 3.2):

# ❌ BAD: Uses mapfile (Bash 4+)
mapfile -t items < <(command)

# ✅ GOOD: Uses while read (Bash 3.2+)
items=()
while IFS= read -r line; do
  items+=("$line")
done < <(command)

Bats Assertions Reference

Exit Status

[ "$status" -eq 0 ]      # Command succeeded
[ "$status" -ne 0 ]      # Command failed
[ "$status" -eq 1 ]      # Command exited with code 1

Output Matching

[[ "$output" =~ pattern ]]           # Output contains pattern
[[ "$output" == "exact" ]]           # Output matches exactly
[[ ! "$output" =~ pattern ]]         # Output doesn't contain pattern

File System

[ -e "$path" ]           # Path exists
[ ! -e "$path" ]         # Path doesn't exist
[ -f "$path" ]           # Regular file exists
[ -d "$path" ]           # Directory exists
[ -L "$path" ]           # Symlink exists

String Comparisons

[ "$var" = "value" ]     # String equals
[ "$var" != "value" ]    # String not equals
[ -z "$var" ]            # String is empty
[ -n "$var" ]            # String is not empty

Troubleshooting

Test Failures

Local tests pass, CI fails:

  • Check for Bash 4+ features (macOS uses 3.2)
  • Verify shellcheck passes on helpers.bash
  • Ensure all required dependencies are installed

Intermittent failures:

  • Check for race conditions
  • Ensure proper cleanup in teardown
  • Verify temp directory uniqueness (bats handles this)

Debugging Tests

# Run with verbose output
bats tests/symlinkit.bats --verbose-run

# Run specific test
bats tests/symlinkit.bats --filter "Create mode"

# Show timing information
bats tests/symlinkit.bats --timing

# Check test file syntax
bash -n tests/symlinkit.bats
bash -n tests/helpers.bash

Common Issues

"command not found: bats":

# Install bats
sudo apt install bats          # Ubuntu/Debian
brew install bats-core         # macOS

Tests fail with "TMPDIR not set":

  • Ensure load ./helpers.bash is at the top of the test file
  • Verify setup() function runs before each test

Test Guidelines Summary

Required for all contributions:

  1. ✅ Run shellcheck: shellcheck scripts/symlinkit scripts/install.sh scripts/uninstall.sh tests/helpers.bash
  2. ✅ Run test suite: make test
  3. ✅ Add tests for new features
  4. ✅ Update tests if behavior changes

Bats best practices:

  • ✅ Use @test "description" format
  • ✅ Leverage setup/teardown helpers
  • ✅ Use run to capture command output
  • ✅ Check $status and $output variables
  • ✅ One assertion per test (or closely related assertions)

Bash compatibility:

  • ✅ Use Bash 3.2+ compatible features only
  • ❌ No mapfile, associative arrays, or Bash 4+ features

Resources

Symlinkit Wiki Navigation

Getting Started

Features & Functionality

Development & Contributing

Clone this wiki locally