|
1 | 1 | # Taskit Test Suite |
2 | 2 |
|
3 | | -## Purpose |
| 3 | +BATS test suite for Taskit. Tests validate Task commands using dry-run mode (`task -n`). |
4 | 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: |
| 5 | +## Setup |
10 | 6 |
|
11 | 7 | ```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 |
| 8 | +aqua install # Installs BATS and Task |
22 | 9 | ``` |
23 | 10 |
|
24 | 11 | ## Running Tests |
25 | 12 |
|
26 | | -### Local Development |
27 | | - |
28 | 13 | ```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/ |
| 14 | +bats tests/ # Run all |
| 15 | +bats tests/terraform_plan.bats # Run specific file |
| 16 | +bats --filter-tags precondition tests/ # Run by tag |
38 | 17 | ``` |
39 | 18 |
|
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. |
| 19 | +CI runs automatically on PRs and main branch pushes via `.github/workflows/test.yaml`. |
54 | 20 |
|
55 | 21 | ## Writing Tests |
56 | 22 |
|
57 | | -Tests leverage Task's dry-run mode (`task -v -n`) to verify command construction without execution: |
| 23 | +Tests use Task's dry-run mode to verify command construction: |
58 | 24 |
|
59 | 25 | ```bash |
60 | 26 | @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 |
| 27 | + touch "tfvars/test-env.tfvars" # Setup fixture |
65 | 28 | run task -v -n terraform:plan -- test-env |
66 | | - |
67 | | - # Assert: Verify output |
68 | 29 | [ "$status" -eq 0 ] |
69 | | - [[ "$output" =~ "expected text" ]] |
| 30 | + [[ "$output" =~ "tofu plan -var-file ./tfvars/test-env.tfvars" ]] |
70 | 31 | } |
71 | 32 | ``` |
72 | 33 |
|
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 |
| 34 | +Key helpers: `setup()`, `teardown()`, `run`, `$status`, `$output` |
80 | 35 |
|
81 | | -Use tags for selective test execution: |
| 36 | +## Quick Reference |
82 | 37 |
|
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 |
| 38 | +**Debug failed tests:** `echo "Output: $output" >&3` |
| 39 | +**Test pattern:** `task -v -n <task> -- <args>` |
| 40 | +**Assertions:** `[ ]` for exit codes, `[[ =~ ]]` for regex matching |
| 41 | +**Tags:** terraform, plan, basic, args, precondition, config, error |
| 42 | +**Fixtures:** Create in `setup()`, clean in `teardown()` |
97 | 43 |
|
98 | 44 | ## Troubleshooting |
99 | 45 |
|
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. |
| 46 | +**CI/local mismatch:** Check `teardown()` cleanup and committed test fixtures. |
122 | 47 |
|
123 | 48 | ## Resources |
124 | 49 |
|
|
0 commit comments