|
| 1 | +# Testing |
| 2 | + |
| 3 | +This project uses [Bats](https://github.com/bats-core/bats-core) (Bash Automated Testing System) for testing the shell scripts. |
| 4 | + |
| 5 | +## Running Tests |
| 6 | + |
| 7 | +```bash |
| 8 | +# Run all tests |
| 9 | +bats tests/ |
| 10 | + |
| 11 | +# Run specific test file |
| 12 | +bats tests/install.bats |
| 13 | +bats tests/sync.bats |
| 14 | +bats tests/validation.bats |
| 15 | + |
| 16 | +# Run with verbose output (show test names) |
| 17 | +bats --tap tests/ |
| 18 | +``` |
| 19 | + |
| 20 | +## Test Structure |
| 21 | + |
| 22 | +``` |
| 23 | +tests/ |
| 24 | +├── test_helper.bash # Shared setup/teardown and utilities |
| 25 | +├── install.bats # Tests for install.sh |
| 26 | +├── sync.bats # Tests for sync.sh commands |
| 27 | +└── validation.bats # Tests for skills validation |
| 28 | +``` |
| 29 | + |
| 30 | +## Writing Tests |
| 31 | + |
| 32 | +### Test File Format |
| 33 | + |
| 34 | +```bash |
| 35 | +#!/usr/bin/env bats |
| 36 | + |
| 37 | +load 'test_helper' |
| 38 | + |
| 39 | +setup() { |
| 40 | + setup_test_env |
| 41 | +} |
| 42 | + |
| 43 | +teardown() { |
| 44 | + teardown_test_env |
| 45 | +} |
| 46 | + |
| 47 | +@test "description of what this tests" { |
| 48 | + # Arrange |
| 49 | + create_fake_skill "my-skill" |
| 50 | + |
| 51 | + # Act |
| 52 | + run_install |
| 53 | + |
| 54 | + # Assert |
| 55 | + assert_symlink "$FAKE_HOME/.claude/skills/my-skill" "$FAKE_REPO/skills/my-skill" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Key Conventions |
| 60 | + |
| 61 | +1. **Always use the test environment** - Call `setup_test_env` in setup and `teardown_test_env` in teardown |
| 62 | +2. **Use helper functions** - Use `run_install`, `run_sync`, `create_fake_skill`, etc. from test_helper.bash |
| 63 | +3. **Test in isolation** - Tests use temp directories (`$FAKE_HOME`, `$FAKE_REPO`) and never touch real config |
| 64 | + |
| 65 | +### Available Test Helpers |
| 66 | + |
| 67 | +**Environment:** |
| 68 | +- `setup_test_env` - Creates isolated temp directories |
| 69 | +- `teardown_test_env` - Cleans up temp directories |
| 70 | +- `$FAKE_HOME` - Temp directory simulating user's home |
| 71 | +- `$FAKE_REPO` - Temp directory simulating the repo |
| 72 | + |
| 73 | +**Creating Test Data:** |
| 74 | +- `create_fake_skill "name"` - Creates a valid skill with SKILL.md |
| 75 | +- `create_invalid_skill "name"` - Creates skill without frontmatter |
| 76 | +- `create_skill_no_md "name"` - Creates skill without SKILL.md |
| 77 | +- `create_fake_agent "name"` - Creates an agent file |
| 78 | +- `create_fake_rule "name"` - Creates a rule file |
| 79 | +- `create_fake_settings` - Creates settings.json |
| 80 | +- `create_fake_statusline` - Creates statusline.sh |
| 81 | + |
| 82 | +**Running Scripts:** |
| 83 | +- `run_install [args]` - Runs install.sh in test environment |
| 84 | +- `run_sync [args]` - Runs sync.sh in test environment |
| 85 | + |
| 86 | +**Assertions:** |
| 87 | +- `assert_symlink "path" "expected_target"` - Verifies symlink exists and points to target |
| 88 | +- `assert_regular_file "path"` - Verifies file exists and is not a symlink |
| 89 | +- `assert_dir "path"` - Verifies directory exists |
| 90 | +- `assert_backup_exists` - Verifies a backup was created |
| 91 | +- `assert_manifest_operation "op"` - Verifies manifest contains operation |
| 92 | + |
| 93 | +**Backup Helpers:** |
| 94 | +- `get_latest_backup` - Returns name of most recent backup |
| 95 | + |
| 96 | +### Testing Tips |
| 97 | + |
| 98 | +1. **Test both success and failure cases** - Verify error messages and exit codes |
| 99 | +2. **Test dry-run mode** - Ensure `--dry-run` doesn't modify anything |
| 100 | +3. **Test idempotency** - Running the same command twice should work |
| 101 | +4. **Group related tests** - Use comment headers to organize test sections |
| 102 | + |
| 103 | +## Adding New Tests |
| 104 | + |
| 105 | +When adding new functionality to install.sh or sync.sh: |
| 106 | + |
| 107 | +1. Add tests to the appropriate .bats file |
| 108 | +2. Add any new helper functions to test_helper.bash |
| 109 | +3. Run `bats tests/` to verify all tests pass |
| 110 | +4. Consider edge cases (missing files, conflicts, dry-run) |
| 111 | + |
| 112 | +## CI Integration |
| 113 | + |
| 114 | +Tests run automatically on push/PR via GitHub Actions. See `.github/workflows/test.yml`. |
| 115 | + |
| 116 | +The workflow: |
| 117 | +1. Runs on `macos-latest` (matches dev environment) |
| 118 | +2. Installs bats-core via Homebrew |
| 119 | +3. Runs all tests with `bats tests/` |
| 120 | +4. Validates all skills with `./sync.sh validate` |
| 121 | + |
| 122 | +To run locally before pushing: |
| 123 | +```bash |
| 124 | +bats tests/ && ./sync.sh validate |
| 125 | +``` |
0 commit comments