terraform-linters / tflint

A Pluggable Terraform Linter
Mozilla Public License 2.0
4.97k stars 357 forks source link

Linting tftest.hcl files #2128

Open wyardley opened 2 months ago

wyardley commented 2 months ago

Summary

It sounds from #2030 like tftest.hcl files should be able to be linted.

It seems, however, as if tflint may not be parsing *.tftest.hcl files (it should also recurse into subdirectories when in recursive mode if there are directories with no .tf files but with .tftest.hcl files).

Taking a look at the code, including here, seems like some adjustments and tests might be needed for this to work?

In the example below, if the file is renamed to .tf, tflint will correctly flag google_storage_bucket.foo.bar.0.main_page_suffix vs google_storage_bucket.foo.bar[0].main_page_suffix being used.

Terraform Configuration

provider "google" {
  project = "foo-testproject"
}
variables {
  name    = "test-me"
}
run "basic" {
  command = plan
  plan_options {
    refresh = false
  }

  assert {
    condition     = google_storage_bucket.foo.bar.0.main_page_suffix == "index.html"
    error_message = "Incorrect website main page suffix."
  }
}
wata727 commented 1 month ago

Thank you for raising this feature request.

We probably can't just use the current evaluator and parser as is because the test language has different semantics than the Terraform language. We would need to implement a dedicated emulator stack for the test language, which would require a fairly large refactoring.

See also https://github.com/terraform-linters/tflint/issues/1949

wyardley commented 1 month ago

A lot of the things can be parsed as-is, since the assertions are used in things like validation conditions. Even though there are some new fields possibly, I get at least some value (list index style for example) simply by renaming the files, and don’t notice any errors from the attributes that maybe tflint doesn’t know about.

wyardley commented 1 month ago

https://developer.hashicorp.com/terraform/language/tests/mocking

.tfmock.hcl and .tfmock.json -- two more test specific files