Refactor terraform-docs action for monorepo support with tests#1130
Merged
EliSchleifer merged 7 commits intomainfrom Apr 29, 2026
Merged
Refactor terraform-docs action for monorepo support with tests#1130EliSchleifer merged 7 commits intomainfrom
EliSchleifer merged 7 commits intomainfrom
Conversation
Find directories where Terraform files have changed via `git diff` and run terraform-docs in each, honoring a root .terraform-docs.yaml when present. Skips quietly when no Terraform files changed. Refactors the script into testable functions and adds a unit test suite covering directory discovery, config-vs-default command selection, and unstaged-README detection (including newly-created untracked READMEs). Builds on #1070 with these fixes: - Restore executable bit on terraform-docs.py. - Use `markdown-table` (single subcommand) for the no-config fallback. - Use `subprocess(cwd=...)` instead of os.chdir to avoid global side effects. - Prefer `git diff --cached` (the pre-commit source of truth), then union unstaged changes. - Skip directories that no longer exist (e.g. when a module was deleted) so the script doesn't crash mid-run. - Detect untracked README.md files in addition to modified ones.
Trunk Check (black) failed on the previous commit. No logic change.
Replaces the standalone Python unittest file with a TypeScript Jest test that follows the same pattern as actions/poetry/poetry.test.ts and actions/commitlint/commitlint.test.ts: spin up a sandbox via TrunkActionDriver, sync git hooks, and exercise the pre-commit hook through gitDriver.commit(). Two cases: - Staged .tf change → commit rejected with "Please stage any README changes before committing." (verifies the hook regenerates docs and catches the resulting unstaged/untracked README). - No Terraform changes → commit succeeds and stdout confirms the early-exit path. Enables tools.terraform-docs@0.16.0 in the sandbox trunk.yaml so the action's shell-out to `terraform-docs` resolves on PATH.
Trunk's prettier config uses printWidth=100; default local prettier (printWidth=80) was producing different output. Reformatted to match.
The Action Tests CI job failed because `terraform-docs` is not on PATH inside the action sandbox. The action's plugin.yaml has no tool declaration, and the sandbox's tool plugin install path doesn't shim the binary into the hook's PATH the same way a bare CI host would. Mirror the hello-world-python pattern: keep the test file in the repo to document the intended structure (single actionRunTest, preCheck + testCallback, gitDriver.commit assertion), but mark it skipped via skipTestIf with a NOTE explaining that it can be enabled once a terraform-docs setup step is added to .github/actions/action_tests/ action.yaml (alongside the existing astral-sh/setup-uv step).
The terraform-docs action's pre-commit hook shells out to the `terraform-docs` binary. Without it on PATH inside the test sandbox, the action exits with "terraform-docs not found" and the assertion on the README-staging error never fires. Mirrors the existing pattern for uv: install the binary in the Action Tests composite step (ubuntu-latest only, which matches the job's runs-on). With the binary available, the test can drop its skipTestIf guard and actually exercise the monorepo-aware behavior.
Member
Author
|
@chris3ware - I think this should do it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactored the terraform-docs pre-commit hook to support monorepos by detecting which directories contain changed Terraform files and running documentation updates only in those directories. Added comprehensive unit tests to validate the core logic without requiring external dependencies.
Key Changes
git diff, groups them by directory, and runsterraform-docsonly in affected directories instead of always running at the repo root.terraform-docs.yamlconfig file if present, otherwise falls back to themarkdown-tablesubcommandfind_unstaged_readmes()to catch untracked READMEs (??status) in addition to modified-but-unstaged files, fixing a bug where newly generated documentation could be missedtest_terraform_docs.pywith 16 unit tests covering:.tf,.tofu,.tfvars)terraform_dirs_from_paths,get_changed_terraform_directories,build_terraform_docs_cmd,find_unstaged_readmes) and amain()entry point.trunk/trunk.yamlNotable Implementation Details
terraform-docsorgitto be installed--cached) and unstaged changes are checked to support developers iterating in the working treehttps://claude.ai/code/session_01QqUjwaZB4osZrUw4EMdWnM