FFFF feat(`terraform_fmt`): Add support for `.tofutest.hcl` and `.tfquery.hcl` files by tvanriel · Pull Request #935 · antonbabenko/pre-commit-terraform · GitHub
[go: up one dir, main page]

Skip to content

feat(terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files#935

Merged
MaxymVlasov merged 1 commit intoantonbabenko:masterfrom
BauWatch:master
Sep 30, 2025
Merged

feat(terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files#935
MaxymVlasov merged 1 commit intoantonbabenko:masterfrom
BauWatch:master

Conversation

@tvanriel
Copy link
Contributor
@tvanriel tvanriel commented Sep 23, 2025

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

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

---
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 460038084e27d0056f34f85ae53fe885a6786173
    hooks:
      - id: terraform_fmt

main.tofutest.hcl

run {
  assert {
    condition = false 
    error_message = "This test is supposed to fail."
  }
}

main.tf

terraform {
  required_version = "~> 1.10.0"

  required_providers {
    null = {
      source  = "hashicorp/null"
      version = "3.2.4"
    }
  }
}

provider "null" {
  # Configuration options
}

resource "null_resource" "test" {
  x = "1"
  this_is_a_test = "2"
}

Run pre-commit install && pre-commit

  1. Terraform fmt should fail
  2. line 17 in main.tf should have the '=' correctly aligned.
  3. line 3 in main.tofutest.hcl should have the '=' correctly aligned.

@coderabbitai
Copy link
coderabbitai bot commented Sep 23, 2025
📝 Walkthrough

Summary by CodeRabbit

  • Chores
    • Expanded Terraform formatting pre-commit hook to cover additional HCL variants, including tofutest and tfquery files.
    • Maintains existing coverage for tf, tofu, tfvars, tftest.hcl, and tfmock.hcl while broadening the pattern to ensure consistent formatting across more configuration and test files.

Walkthrough

Relaxed the .pre-commit-hooks.yaml terraform_fmt hook files regex to include additional HCL variants (tofutest, tfquery) alongside existing tftest/tfmock; no runtime code changes.

Changes

Cohort / File(s) Summary
Pre-commit hook config
/.pre-commit-hooks.yaml
Updated terraform_fmt hook files pattern from `.(tf

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested reviewers

  • antonbabenko
  • yermulnik

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title clearly and concisely describes the primary change to the terraform_fmt hook by specifying the addition of support for .tofutest.hcl and .tfquery.hcl files, making its intent immediately clear to reviewers.
Description Check ✅ Passed The pull request description is directly related to the changeset as it explains the bug fix for the terraform_fmt hook, references the source of the new filename patterns in the OpenTofu documentation, and includes concrete testing instructions to validate the update.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🧪 Early access (Sonnet 4.5): enabled

We 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:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
@coderabbitai coderabbitai bot left a 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_fmt

terraform_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

📥 Commits

Reviewing files that changed from the base of the PR and between 48b3a29 and d4b00d8.

📒 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 support

Pattern 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.

yermulnik
yermulnik previously approved these changes Sep 23, 2025
Copy link
Collaborator
@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
@MaxymVlasov FYI

@yermulnik yermulnik changed the title fix(terraform_fmt): add support for tofutest files fix(terraform_fmt): Add support for tofutest files Sep 23, 2025
yermulnik
yermulnik previously approved these changes Sep 23, 2025
# 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)$
Copy link
Collaborator

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

Suggested change
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tofumock)\.hcl)$
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock)\.hcl)$

MaxymVlasov
MaxymVlasov previously approved these changes Sep 27, 2025
# 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)$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tvanriel can you please merge lastest changes from master and commit suggestion above? Thank you

@MaxymVlasov MaxymVlasov changed the title fix(terraform_fmt): Add support for tofutest files feat(terraform_fmt): Add support for .tofutest.hcl and .tfquery.hcl files Sep 27, 2025
Copy link
@coderabbitai coderabbitai bot left a 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 (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.hcl is from Terraform 1.14 (per previous review comments)
  • .tofutest.hcl is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 9e8e1bc and 4600380.

📒 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.hcl and .tfquery.hcl files 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_validate

The terraform_fmt hook supports test file extensions (tftest, tofutest, tfmock, tfquery) via

files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl)$

whereas terraform_validate currently only matches

\.(tf|tofu|tfvars|terraform\.lock\.hcl)$

For consistency, consider updating terraform_validate to:

files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tfquery)\.hcl|terraform\.lock\.hcl)$

Verify that Terraform/OpenTofu’s validate command processes these test file types before applying this change.

@MaxymVlasov MaxymVlasov merged commit ba7cf0e into antonbabenko:master Sep 30, 2025
42 of 44 checks passed
antonbabenko pushed a commit that referenced this pull request Sep 30, 2025
# [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)
@antonbabenko
Copy link
Owner

This PR is included in version 1.101.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0