feat(terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files#935
feat(terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files#935MaxymVlasov merged 1 commit intoantonbabenko:masterfrom
terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files#935Conversation
📝 WalkthroughSummary by CodeRabbit
WalkthroughRelaxed the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests
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. 🧪 Early access (Sonnet 4.5): enabledWe are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience. Note:
Comment |
There was a problem hiding this comment.
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.
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.
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)$ |
.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)\.hcl)$ |
There was a problem hiding this comment.
| files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock)\.hcl)$ | |
| files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl)$ |
And as we here, let's also add support for .tfquery.hcl, as it will be introduced in TF 1.14
https://github.com/hashicorp/terraform/blob/eae5ac13dd47eafd2d338b21db7311bbfa861b68/internal/command/fmt.go#L56
https://github.com/hashicorp/terraform/releases/tag/v1.14.0-beta2
There was a problem hiding this comment.
@tvanriel can you please merge lastest changes from master and commit suggestion above? Thank you
terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files
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
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
.pre-commit-hooks.yaml (1)
16-17: Consider updating the source comment to reflect multiple upstream sources.While the comment references Terraform's fmt.go for supported extensions, the newly added extensions come from different sources:
.tfquery.hclis from Terraform 1.14 (per previous review comments).tofutest.hclis from OpenTofu documentation (per PR description)Consider updating the comment to acknowledge these additional sources for clarity.
Apply this diff to make the comment more comprehensive:
# Supported extensions by Terraform specified in # https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35 + # Additional extensions: tfquery (Terraform 1.14+), tofutest (OpenTofu) files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl)$
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.pre-commit-hooks.yaml(1 hunks)
🔇 Additional comments (2)
.pre-commit-hooks.yaml (2)
18-18: LGTM! Regex pattern correctly adds support for OpenTofu test files.The updated pattern correctly matches
.tofutest.hcland.tfquery.hclfiles as intended, while preserving support for existing Terraform test file extensions. The grouping syntax is correct and aligns with the suggestion from previous reviewers.
58-58: Include test file patterns in terraform_validateThe
terraform_fmthook supports test file extensions (tftest,tofutest,tfmock,tfquery) viafiles: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl)$whereas
terraform_validatecurrently only matches\.(tf|tofu|tfvars|terraform\.lock\.hcl)$For consistency, consider updating
terraform_validateto:files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl|terraform\.lock\.hcl)$Verify that Terraform/OpenTofu’s
validatecommand processes these test file types before applying this change.
# [1.101.0](v1.100.1...v1.101.0) (2025-09-30) ### Features * **`terraform_fmt`:** Add support for `.tofutest.hcl` and `.tfquery.hcl` files ([#935](#935)) ([ba7cf0e](ba7cf0e)), closes [/github.com/hashicorp/terraform/blob/eae5ac13dd47eafd2d338b21db7311bbfa861b68/internal/command/fmt.go#L56](https://github.com//github.com/hashicorp/terraform/blob/eae5ac13dd47eafd2d338b21db7311bbfa861b68/internal/command/fmt.go/issues/L56)
|
This PR is included in version 1.101.0 🎉 |
Put an
xinto 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.yamlmain.tofutest.hclmain.tfRun
pre-commit install && pre-commit