Skip to content

Refactor terraform-docs action for monorepo support with tests#1130

Merged
EliSchleifer merged 7 commits intomainfrom
claude/review-pr-1070-4zZcf
Apr 29, 2026
Merged

Refactor terraform-docs action for monorepo support with tests#1130
EliSchleifer merged 7 commits intomainfrom
claude/review-pr-1070-4zZcf

Conversation

@EliSchleifer
Copy link
Copy Markdown
Member

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

claude added 3 commits April 29, 2026 17:20
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.
@trunk-io
Copy link
Copy Markdown

trunk-io Bot commented Apr 29, 2026

Static BadgeStatic BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

claude added 4 commits April 29, 2026 20:21
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.
@EliSchleifer
Copy link
Copy Markdown
Member Author

@chris3ware - I think this should do it

@EliSchleifer EliSchleifer marked this pull request as ready for review April 29, 2026 23:02
@EliSchleifer EliSchleifer merged commit d8a09fc into main Apr 29, 2026
14 checks passed
@EliSchleifer EliSchleifer deleted the claude/review-pr-1070-4zZcf branch April 29, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants