You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor terraform-docs action for monorepo support with tests (#1130)
## 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
- **Monorepo support**: The action now detects changed Terraform files
using `git diff`, groups them by directory, and runs `terraform-docs`
only in affected directories instead of always running at the repo root
- **Configuration detection**: Added logic to use `.terraform-docs.yaml`
config file if present, otherwise falls back to the `markdown-table`
subcommand
- **Improved README detection**: Enhanced `find_unstaged_readmes()` to
catch untracked READMEs (`??` status) in addition to
modified-but-unstaged files, fixing a bug where newly generated
documentation could be missed
- **Comprehensive test suite**: Added `test_terraform_docs.py` with 16
unit tests covering:
- Directory grouping from file paths
- Terraform file extension detection (`.tf`, `.tofu`, `.tfvars`)
- Handling of deleted directories and empty input
- Git status parsing for various file states
- Configuration file detection
- **Code organization**: Refactored into testable functions
(`terraform_dirs_from_paths`, `get_changed_terraform_directories`,
`build_terraform_docs_cmd`, `find_unstaged_readmes`) and a `main()`
entry point
- **Enabled action**: Registered the terraform-docs action in
`.trunk/trunk.yaml`
## Notable Implementation Details
- Tests use temporary directories and don't require `terraform-docs` or
`git` to be installed
- The script gracefully handles deleted files by checking if directories
still exist on disk before attempting to run documentation updates
- Whitespace and blank lines in file paths are stripped to handle
various input formats
- Both staged (`--cached`) and unstaged changes are checked to support
developers iterating in the working tree
https://claude.ai/code/session_01QqUjwaZB4osZrUw4EMdWnM
---------
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments