Skip to content

Commit 84e96d8

Browse files
feat: rewrite terraform test and validate scripts dynamically using paths-filter
1 parent 90873e8 commit 84e96d8

File tree

4 files changed

+139
-15
lines changed

4 files changed

+139
-15
lines changed

.github/workflows/ci.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ jobs:
4949
ALL_CHANGED_FILES: ${{ steps.filter.outputs.all_files }}
5050
SHARED_CHANGED: ${{ steps.filter.outputs.shared }}
5151
MODULE_CHANGED_FILES: ${{ steps.filter.outputs.modules_files }}
52-
run: ./scripts/ts_test_auto.sh
52+
run: bun tstest
5353
- name: Run Terraform tests
54-
run: ./scripts/terraform_test_all.sh
54+
env:
55+
ALL_CHANGED_FILES: ${{ steps.filter.outputs.all_files }}
56+
SHARED_CHANGED: ${{ steps.filter.outputs.shared }}
57+
MODULE_CHANGED_FILES: ${{ steps.filter.outputs.modules_files }}
58+
run: bun tftest
5559
- name: Run Terraform Validate
60+
env:
61+
ALL_CHANGED_FILES: ${{ steps.filter.outputs.all_files }}
62+
SHARED_CHANGED: ${{ steps.filter.outputs.shared }}
63+
MODULE_CHANGED_FILES: ${{ steps.filter.outputs.modules_files }}
5664
run: bun terraform-validate
5765
validate-style:
5866
name: Check for typos and unformatted code

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"fmt": "bun x prettier --write . && terraform fmt -recursive -diff",
55
"fmt:ci": "bun x prettier --check . && terraform fmt -check -recursive -diff",
66
"terraform-validate": "./scripts/terraform_validate.sh",
7-
"test": "./scripts/terraform_test_all.sh",
7+
"tftest": "./scripts/terraform_test_all.sh",
8+
"tstest": "./scripts/ts_test_auto.sh",
89
"update-version": "./update-version.sh"
910
},
1011
"devDependencies": {

scripts/terraform_test_all.sh

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,87 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# Find all directories that contain any .tftest.hcl files and run terraform test in each
4+
# Auto-detect which Terraform tests to run based on changed files from paths-filter
5+
# Uses paths-filter outputs from GitHub Actions:
6+
# ALL_CHANGED_FILES - all files changed in the PR (for logging)
7+
# SHARED_CHANGED - boolean indicating if shared infrastructure changed
8+
# MODULE_CHANGED_FILES - only files in registry/**/modules/** (for processing)
9+
# Runs all tests if shared infrastructure changes or if env vars are not set (local dev)
10+
#
11+
# This script only runs tests for changed modules. Documentation and template changes are ignored.
512

613
run_dir() {
714
local dir="$1"
815
echo "==> Running terraform test in $dir"
916
(cd "$dir" && terraform init -upgrade -input=false -no-color > /dev/null && terraform test -no-color -verbose)
1017
}
1118

12-
mapfile -t test_dirs < <(find . -type f -name "*.tftest.hcl" -print0 | xargs -0 -I{} dirname {} | sort -u)
19+
echo "==> Detecting changed files..."
20+
21+
if [[ -n "${ALL_CHANGED_FILES:-}" ]]; then
22+
echo "Changed files in PR:"
23+
echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | sed 's/^/ - /'
24+
echo ""
25+
fi
26+
27+
if [[ "${SHARED_CHANGED:-false}" == "true" ]]; then
28+
echo "==> Shared infrastructure changed"
29+
echo "==> Running all tests for safety"
30+
mapfile -t test_dirs < <(find . -type f -name "*.tftest.hcl" -print0 | xargs -0 -I{} dirname {} | sort -u)
31+
elif [[ -z "${MODULE_CHANGED_FILES:-}" ]]; then
32+
echo "✓ No module files changed, skipping tests"
33+
exit 0
34+
else
35+
CHANGED_FILES=$(echo "$MODULE_CHANGED_FILES" | tr ' ' '\n')
36+
37+
MODULE_DIRS=()
38+
while IFS= read -r file; do
39+
if [[ "$file" =~ \.(md|png|jpg|jpeg|svg)$ ]]; then
40+
continue
41+
fi
42+
43+
if [[ "$file" =~ ^registry/([^/]+)/modules/([^/]+)/ ]]; then
44+
namespace="${BASH_REMATCH[1]}"
45+
module="${BASH_REMATCH[2]}"
46+
module_dir="registry/${namespace}/modules/${module}"
47+
48+
if [[ -d "$module_dir" ]] && [[ ! " ${MODULE_DIRS[*]} " =~ " ${module_dir} " ]]; then
49+
MODULE_DIRS+=("$module_dir")
50+
fi
51+
fi
52+
done <<< "$CHANGED_FILES"
53+
54+
if [[ ${#MODULE_DIRS[@]} -eq 0 ]]; then
55+
echo "✓ No Terraform tests to run"
56+
echo " (documentation, templates, namespace files, or modules without changes)"
57+
exit 0
58+
fi
59+
60+
echo "==> Finding .tftest.hcl files in ${#MODULE_DIRS[@]} changed module(s):"
61+
for dir in "${MODULE_DIRS[@]}"; do
62+
echo " - $dir"
63+
done
64+
echo ""
65+
66+
test_dirs=()
67+
for module_dir in "${MODULE_DIRS[@]}"; do
68+
while IFS= read -r test_file; do
69+
test_dir=$(dirname "$test_file")
70+
if [[ ! " ${test_dirs[*]} " =~ " ${test_dir} " ]]; then
71+
test_dirs+=("$test_dir")
72+
fi
73+
done < <(find "$module_dir" -type f -name "*.tftest.hcl")
74+
done
75+
fi
1376

1477
if [[ ${#test_dirs[@]} -eq 0 ]]; then
15-
echo "No .tftest.hcl tests found."
78+
echo "No .tftest.hcl tests found in changed modules"
1679
exit 0
1780
fi
1881

82+
echo "==> Running terraform test in ${#test_dirs[@]} directory(ies)"
83+
echo ""
84+
1985
status=0
2086
for d in "${test_dirs[@]}"; do
2187
if ! run_dir "$d"; then

scripts/terraform_validate.sh

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
set -euo pipefail
44

5+
# Auto-detect which Terraform modules to validate based on changed files from paths-filter
6+
# Uses paths-filter outputs from GitHub Actions:
7+
# ALL_CHANGED_FILES - all files changed in the PR (for logging)
8+
# SHARED_CHANGED - boolean indicating if shared infrastructure changed
9+
# MODULE_CHANGED_FILES - only files in registry/**/modules/** (for processing)
10+
# Validates all modules if shared infrastructure changes or if env vars are not set (local dev)
11+
#
12+
# This script only validates changed modules. Documentation and template changes are ignored.
13+
514
validate_terraform_directory() {
615
local dir="$1"
716
echo "Running \`terraform validate\` in $dir"
@@ -12,18 +21,58 @@ validate_terraform_directory() {
1221
}
1322

1423
main() {
15-
# Get the directory of the script
16-
local script_dir=$(dirname "$(readlink -f "$0")")
24+
echo "==> Detecting changed files..."
25+
26+
if [[ -n "${ALL_CHANGED_FILES:-}" ]]; then
27+
echo "Changed files in PR:"
28+
echo "$ALL_CHANGED_FILES" | tr ' ' '\n' | sed 's/^/ - /'
29+
echo ""
30+
fi
1731

18-
# Code assumes that registry directory will always be in same position
19-
# relative to the main script directory
32+
local script_dir=$(dirname "$(readlink -f "$0")")
2033
local registry_dir="$script_dir/../registry"
2134

22-
# Get all module subdirectories in the registry directory. Code assumes that
23-
# Terraform module directories won't begin to appear until three levels deep into
24-
# the registry (e.g., registry/coder/modules/coder-login, which will then
25-
# have a main.tf file inside it)
26-
local subdirs=$(find "$registry_dir" -mindepth 3 -path "*/modules/*" -type d | sort)
35+
if [[ "${SHARED_CHANGED:-false}" == "true" ]]; then
36+
echo "==> Shared infrastructure changed"
37+
echo "==> Validating all modules for safety"
38+
local subdirs=$(find "$registry_dir" -mindepth 3 -path "*/modules/*" -type d | sort)
39+
elif [[ -z "${MODULE_CHANGED_FILES:-}" ]]; then
40+
echo "✓ No module files changed, skipping validation"
41+
exit 0
42+
else
43+
CHANGED_FILES=$(echo "$MODULE_CHANGED_FILES" | tr ' ' '\n')
44+
45+
MODULE_DIRS=()
46+
while IFS= read -r file; do
47+
if [[ "$file" =~ \.(md|png|jpg|jpeg|svg)$ ]]; then
48+
continue
49+
fi
50+
51+
if [[ "$file" =~ ^registry/([^/]+)/modules/([^/]+)/ ]]; then
52+
namespace="${BASH_REMATCH[1]}"
53+
module="${BASH_REMATCH[2]}"
54+
module_dir="registry/${namespace}/modules/${module}"
55+
56+
if [[ -d "$module_dir" ]] && [[ ! " ${MODULE_DIRS[*]} " =~ " ${module_dir} " ]]; then
57+
MODULE_DIRS+=("$module_dir")
58+
fi
59+
fi
60+
done <<< "$CHANGED_FILES"
61+
62+
if [[ ${#MODULE_DIRS[@]} -eq 0 ]]; then
63+
echo "✓ No modules to validate"
64+
echo " (documentation, templates, namespace files, or modules without changes)"
65+
exit 0
66+
fi
67+
68+
echo "==> Validating ${#MODULE_DIRS[@]} changed module(s):"
69+
for dir in "${MODULE_DIRS[@]}"; do
70+
echo " - $dir"
71+
done
72+
echo ""
73+
74+
local subdirs="${MODULE_DIRS[*]}"
75+
fi
2776

2877
for dir in $subdirs; do
2978
# Skip over any directories that obviously don't have the necessary

0 commit comments

Comments
 (0)