|
| 1 | +# Taskit Test Suite |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +This directory contains the BATS (Bash Automated Testing System) test suite for Taskit. Tests validate Task command functionality using dry-run mode, ensuring reliability without requiring actual tool installations. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +Install BATS: |
| 10 | + |
| 11 | +```bash |
| 12 | +# macOS |
| 13 | +brew install bats-core |
| 14 | + |
| 15 | +# Debian/Ubuntu |
| 16 | +apt-get install bats |
| 17 | + |
| 18 | +# Manual installation |
| 19 | +git clone https://github.com/bats-core/bats-core.git |
| 20 | +cd bats-core |
| 21 | +./install.sh /usr/local |
| 22 | +``` |
| 23 | + |
| 24 | +## Running Tests |
| 25 | + |
| 26 | +### Local Development |
| 27 | + |
| 28 | +```bash |
| 29 | +# Run all tests |
| 30 | +bats tests/ |
| 31 | + |
| 32 | +# Run specific test file |
| 33 | +bats tests/terraform_plan.bats |
| 34 | + |
| 35 | +# Run tests by tag |
| 36 | +bats --filter-tags basic tests/ |
| 37 | +bats --filter-tags precondition tests/ |
| 38 | +``` |
| 39 | + |
| 40 | +### Continuous Integration |
| 41 | + |
| 42 | +Tests run automatically on: |
| 43 | + |
| 44 | +- **Pull Requests**: All PRs trigger the test suite |
| 45 | +- **Main Branch**: Tests run on every push to main |
| 46 | + |
| 47 | +The GitHub Actions workflow (`.github/workflows/test.yaml`) handles: |
| 48 | + |
| 49 | +1. BATS and Task installation |
| 50 | +2. Test execution across the `tests/` directory |
| 51 | +3. Result artifacts upload (30-day retention) |
| 52 | + |
| 53 | +View results in the GitHub Actions tab or PR checks section. |
| 54 | + |
| 55 | +## Writing Tests |
| 56 | + |
| 57 | +Tests leverage Task's dry-run mode (`task -v -n`) to verify command construction without execution: |
| 58 | + |
| 59 | +```bash |
| 60 | +@test "terraform:plan validates environment argument" { |
| 61 | + # Arrange: Create test fixture |
| 62 | + touch "tfvars/test-env.tfvars" |
| 63 | + |
| 64 | + # Act: Execute task in dry-run mode |
| 65 | + run task -v -n terraform:plan -- test-env |
| 66 | + |
| 67 | + # Assert: Verify output |
| 68 | + [ "$status" -eq 0 ] |
| 69 | + [[ "$output" =~ "expected text" ]] |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Test Structure |
| 74 | + |
| 75 | +- `setup()`: Pre-test fixture creation and environment setup |
| 76 | +- `@test "description"`: Individual test case |
| 77 | +- `teardown()`: Post-test cleanup |
| 78 | + |
| 79 | +### Test Tags |
| 80 | + |
| 81 | +Use tags for selective test execution: |
| 82 | + |
| 83 | +- `terraform`: Terraform-related functionality |
| 84 | +- `plan`: Plan command behavior |
| 85 | +- `basic`: Core functionality |
| 86 | +- `args`: Argument handling |
| 87 | +- `precondition`: Validation logic |
| 88 | +- `config`: Configuration management |
| 89 | +- `error`: Error handling |
| 90 | + |
| 91 | +## Best Practices |
| 92 | + |
| 93 | +1. **Dry-run Testing**: Use `task -v -n` to test without requiring actual tool installations |
| 94 | +2. **Debug Output**: Include `echo "Output: $output" >&3` for troubleshooting failed tests |
| 95 | +3. **Fixture Management**: Create temporary files in `setup()`, clean up in `teardown()` |
| 96 | +4. **Assertions**: Use `[ ]` for exit codes, `[[ =~ ]]` for pattern matching |
| 97 | + |
| 98 | +## Troubleshooting |
| 99 | + |
| 100 | +**Tests fail in CI but pass locally:** |
| 101 | + |
| 102 | +- Ensure proper cleanup in `teardown()` |
| 103 | +- Verify tests don't depend on local environment state |
| 104 | +- Confirm all required files are committed |
| 105 | + |
| 106 | +**Workflow doesn't run:** |
| 107 | + |
| 108 | +- Verify `.github/workflows/test.yaml` exists |
| 109 | +- Check GitHub Actions are enabled for the repository |
| 110 | +- Review branch protection rules |
| 111 | + |
| 112 | +## Workflow Permissions |
| 113 | + |
| 114 | +The CI workflow uses minimal permissions: |
| 115 | + |
| 116 | +- `actions: read` - Access workflow logs |
| 117 | +- `checks: write` - Report check results |
| 118 | +- `contents: read` - Read repository |
| 119 | +- `pull-requests: read` - Access PR information |
| 120 | + |
| 121 | +No secrets or credentials required for test execution. |
| 122 | + |
| 123 | +## Resources |
| 124 | + |
| 125 | +- [BATS Documentation](https://bats-core.readthedocs.io/en/stable/) |
| 126 | +- [Writing BATS Tests](https://bats-core.readthedocs.io/en/stable/writing-tests.html) |
| 127 | +- [Task Documentation](https://taskfile.dev/) |
0 commit comments