Skip to content
Closed
5 changes: 3 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v4
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0

- name: Trunk Check
uses: trunk-io/trunk-action@v1
uses: trunk-io/trunk-action@4d5ecc89b2691705fd08c747c78652d2fc806a94 # v1.1.19
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: googleapis/release-please-action@7987652d64b4581673a76e33ad5e98e3dd56832f #v4.1.3
- uses: googleapis/release-please-action@16a9c90856f42705d54a6fda1823352bdc62cf38 # v4.4.0
with:
release-type: terraform-module
28 changes: 28 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test

on:
pull_request: {}
push:
branches:
- main

permissions:
actions: read
checks: write
contents: read
pull-requests: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0

- name: Install tools via aqua
uses: aquaproj/aqua-installer@11dd79b4e498d471a9385aa9fb7f62bb5f52a73c # v4.0.4
with:
aqua_version: v2.55.1

- name: Run BATS tests
run: bats tests/
2 changes: 2 additions & 0 deletions aqua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ packages:
tags: [cursor]
- name: cli/cli@v2.83.0
tags: [gh]
- name: bats-core/bats-core@v1.13.0
tags: [cicd]
52 changes: 52 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Taskit Test Suite

BATS test suite for Taskit. Tests validate Task commands using dry-run mode (`task -n`).

## Setup

```bash
aqua install # Installs BATS and Task
```

## Running Tests

```bash
bats tests/ # Run all
bats tests/terraform_plan.bats # Run specific file
bats --filter-tags precondition tests/ # Run by tag
```

CI runs automatically on PRs and main branch pushes via `.github/workflows/test.yaml`.

## Writing Tests

Tests use Task's dry-run mode to verify command construction:

```bash
@test "terraform:plan validates environment argument" {
touch "tfvars/test-env.tfvars" # Setup fixture
run task -v -n terraform:plan -- test-env
[ "$status" -eq 0 ]
[[ "$output" =~ "tofu plan -var-file ./tfvars/test-env.tfvars" ]]
}
```

Key helpers: `setup()`, `teardown()`, `run`, `$status`, `$output`

## Quick Reference

**Debug failed tests:** `echo "Output: $output" >&3`
**Test pattern:** `task -v -n <task> -- <args>`
**Assertions:** `[ ]` for exit codes, `[[ =~ ]]` for regex matching
**Tags:** terraform, plan, basic, args, precondition, config, error
**Fixtures:** Create in `setup()`, clean in `teardown()`

## Troubleshooting

**CI/local mismatch:** Check `teardown()` cleanup and committed test fixtures.

## Resources

- [BATS Documentation](https://bats-core.readthedocs.io/en/stable/)
- [Writing BATS Tests](https://bats-core.readthedocs.io/en/stable/writing-tests.html)
- [Task Documentation](https://taskfile.dev/)
60 changes: 60 additions & 0 deletions tests/terraform_plan.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bats
# Test suite for terraform:plan task command

setup() {
cd "$(dirname "$BATS_TEST_FILENAME")/.."
export TEST_WORKSPACE="test-env"
mkdir -p tfvars backend-configurations
touch "tfvars/${TEST_WORKSPACE}.tfvars"
touch "backend-configurations/${TEST_WORKSPACE}.backend.tf"
}

teardown() {
rm -rf tfvars backend-configurations
}

# bats test_tags=terraform,plan,basic
@test "terraform:plan generates correct command with workspace" {
run task -v -n tf:plan -- "$TEST_WORKSPACE"

echo "Output: $output" >&3

[ "$status" -eq 0 ]
[[ "$output" =~ terraform.*workspace.*select.*-or-create.*${TEST_WORKSPACE} ]]
[[ "$output" =~ terraform.*plan.*-var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]]
}

# bats test_tags=terraform,plan,args
@test "terraform:plan passes additional arguments to terraform" {
run task -v -n tf:plan -- "$TEST_WORKSPACE" -out=tfplan.out -lock=false

echo "Output: $output" >&3

[ "$status" -eq 0 ]
[[ "$output" =~ -out=tfplan.out ]]
[[ "$output" =~ -lock=false ]]
[[ "$output" =~ -var-file.*tfvars/${TEST_WORKSPACE}.tfvars ]]
}

# bats test_tags=terraform,plan,precondition
@test "terraform:plan fails when tfvars file does not exist" {
rm -f "tfvars/${TEST_WORKSPACE}.tfvars"

run task -v -n tf:plan -- "$TEST_WORKSPACE"

echo "Output: $output" >&3

[ "$status" -ne 0 ]
[[ "$output" =~ "Variables file does not exist" ]]
}

# bats test_tags=terraform,plan,config
@test "terraform:plan uses terraform when USE_TERRAFORM=true" {
run env USE_TERRAFORM=true task -v -n tf:plan -- "$TEST_WORKSPACE"

echo "Output: $output" >&3

[ "$status" -eq 0 ]
[[ "$output" =~ \[tf:plan\]\ terraform\ workspace\ select ]]
[[ "$output" =~ \[tf:plan\]\ terraform\ plan ]]
}