Repository navigation
Testing Framework
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.
symlinkit 2.0 uses bats for automated testing with a comprehensive suite of 27 tests covering all functionality.
- 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
# 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-runRequired:
- Bash 3.2+
-
bats- Bash Automated Testing System -
realpath(Linux/WSL) orgrealpath(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/localSupported platforms:
- ✅ Linux (all distributions)
- ✅ macOS (via Homebrew coreutils)
- ✅ WSL
tests/
├── symlinkit.bats # Complete bats test suite (27 tests)
└── helpers.bash # Test setup, teardown, and helper functions
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 ]]
}@test "Test description" {
run run_symlinkit --version
[ "$status" -eq 0 ]
[[ "$output" =~ symlinkit ]]
}@test "Invalid flag combination -cd" {
run run_symlinkit -cd
[ "$status" -ne 0 ] # Should fail
[[ "$output" =~ Invalid ]]
}@test "Create mode actual" {
run run_symlinkit -c "$TMPDIR/source/file.txt" "$TMPDIR/dest/link"
[ "$status" -eq 0 ]
[ -L "$TMPDIR/dest/link" ]
}@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
}@test "Merge mode actual" {
run bash -c "echo s | $BATS_TEST_DIRNAME/../symlinkit -m \"$TMPDIR/source\" \"$TMPDIR/dest\""
[ "$status" -eq 0 ]
[ -d "$TMPDIR/dest" ]
}@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 ]]
}# 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 failedTests run automatically on:
- Pull requests to
main - Pushes to
main
Workflow: .github/workflows/test.yml
Test matrix:
ubuntu-latestmacos-latest
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
- Identify the feature to test
-
Write the test in
tests/symlinkit.bats -
Use helpers from
tests/helpers.bash -
Run locally to verify:
make test - Submit PR with test included
# 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" ]
}Use clear, descriptive test names:
@test "Create mode dry-run" # Good
@test "Test 1" # Bad
@test "Delete preserves source file" # Good
@test "Check delete" # BadEach 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" { ... }Leverage bats' automatic setup/teardown:
# helpers.bash handles this automatically
setup() {
TMPDIR=$(mktemp -d)
# ... create test environment
}
teardown() {
rm -rf "$TMPDIR" # Automatic cleanup
}# GOOD: Multiple clear assertions
[ "$status" -eq 0 ]
[[ "$output" =~ symlinkit ]]
[ -L "$link" ]
# BAD: Vague single assertion
[[ -n "$output" ]]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 ]]
}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)[ "$status" -eq 0 ] # Command succeeded
[ "$status" -ne 0 ] # Command failed
[ "$status" -eq 1 ] # Command exited with code 1[[ "$output" =~ pattern ]] # Output contains pattern
[[ "$output" == "exact" ]] # Output matches exactly
[[ ! "$output" =~ pattern ]] # Output doesn't contain pattern[ -e "$path" ] # Path exists
[ ! -e "$path" ] # Path doesn't exist
[ -f "$path" ] # Regular file exists
[ -d "$path" ] # Directory exists
[ -L "$path" ] # Symlink exists[ "$var" = "value" ] # String equals
[ "$var" != "value" ] # String not equals
[ -z "$var" ] # String is empty
[ -n "$var" ] # String is not emptyLocal 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)
# 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"command not found: bats":
# Install bats
sudo apt install bats # Ubuntu/Debian
brew install bats-core # macOSTests fail with "TMPDIR not set":
- Ensure
load ./helpers.bashis at the top of the test file - Verify
setup()function runs before each test
Required for all contributions:
- ✅ Run shellcheck:
shellcheck scripts/symlinkit scripts/install.sh scripts/uninstall.sh tests/helpers.bash - ✅ Run test suite:
make test - ✅ Add tests for new features
- ✅ Update tests if behavior changes
Bats best practices:
- ✅ Use
@test "description"format - ✅ Leverage setup/teardown helpers
- ✅ Use
runto capture command output - ✅ Check
$statusand$outputvariables - ✅ 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
- Bats Documentation
- Bats GitHub
- Contributing - Guidelines for contributing with test requirements
- API Reference - Understanding what to test
- Symlink Modes - Features that need test coverage
Symlinkit Wiki Navigation
Getting Started
Features & Functionality
Development & Contributing