-
-
Notifications
You must be signed in to change notification settings - Fork 566
fix(terraform_fmt): Add support for tofutest files #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughUpdated the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal). Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
.pre-commit-hooks.yaml (2)
16-18
: Update the comment to reflect OpenTofu additions.The comment references Terraform’s list only; we’re also adding OpenTofu-specific files. Tighten the comment to avoid confusion.
- # Supported extensions by Terraform specified in - # https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35 + # Supported extensions by Terraform (link below) plus OpenTofu test/mock files. + # https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35 + # Also includes: *.tofutest.hcl, *.tofumock.hcl
18-18
: Prefer OpenTofu for .tofu/.tofutest files or exclude them from terraform_fmtterraform_fmt runs "$tf_path" fmt (hooks/terraform_fmt.sh) and common::get_tf_binary_path (hooks/_common.sh) currently prefers the terraform binary over tofu, so when both are present terraform will format .tofu/.tofutest.hcl files matched by .pre-commit-hooks.yaml (files regex). Either change discovery to prefer tofu for tofu-specific extensions, add per-extension selection in terraform_fmt.sh (call tofu for .tofu/.tofutest/.tofumock), or remove those tofu-specific globs from the terraform_fmt hook.
- .pre-commit-hooks.yaml: files regex includes tftest/tofutest/tfmock/tofumock (line ~18)
- hooks/_common.sh: common::get_tf_binary_path (selection order)
- hooks/terraform_fmt.sh: per_dir_hook_unique_part -> "$tf_path" fmt
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.pre-commit-hooks.yaml
(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-12T19:49:13.257Z
Learnt from: actuarysailor
PR: antonbabenko/pre-commit-terraform#925
File: .pre-commit-hooks.yaml:216-227
Timestamp: 2025-08-12T19:49:13.257Z
Learning: In Terraform projects, most module folders are downloaded dependencies (similar to GitHub Actions) rather than locally maintained code. Users typically want to document only the root module to avoid commit noise from modules they consume but don't maintain. The terraform_docs_docker hook's current design with pass_filenames: false and args targeting the current directory (.) is appropriate for this common single-module repository pattern.
Applied to files:
.pre-commit-hooks.yaml
🔇 Additional comments (1)
.pre-commit-hooks.yaml (1)
18-18
: Approve tofutest addition; confirm mock-file supportPattern correctly matches .tftest.hcl and .tofutest.hcl; OpenTofu docs list tftest/tofutest but do not document .tfmock/.tofumock — confirm fmt support for mock files or remove them from the pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@MaxymVlasov FYI
.pre-commit-hooks.yaml
Outdated
# Supported extensions by Terraform specified in | ||
# https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35 | ||
files: \.(tf|tofu|tfvars|tftest\.hcl|tfmock\.hcl)$ | ||
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tofumock)\.hcl)$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Techincally, .tofumock.hcl does not exist yet, and looks like it will not be implemented soon (as they asked for community PR) - opentofu/opentofu#1778
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tofumock)\.hcl)$ | |
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock)\.hcl)$ |
OpenTofu has their own file pattern for defining tests: *.tofutest.hcl As taken from https://opentofu.org/docs/cli/commands/test/#the-tftesthcl--tofutesthcl-file-structure
Put an
x
into the box if that apply:Description of your changes
I included supported filenames taken from the opentofu documentation in the terraform_fmt hook.
How can we test changes
.pre-commit-config.yaml
main.tofutest.hcl
main.tf
Run
pre-commit install && pre-commit