-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
[Testing Framework] Add test file HCL configuration and parser functionality #33325
Conversation
18aba77
to
b6dfa16
Compare
2dcb188
to
4e373dd
Compare
b6dfa16
to
7a8d712
Compare
@@ -88,7 +119,9 @@ func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnost | |||
return files, diags | |||
} | |||
|
|||
func (p *Parser) dirFiles(dir string) (primary, override []string, diags hcl.Diagnostics) { | |||
func (p *Parser) dirFiles(dir string, testsDir string) (primary, override, tests []string, diags hcl.Diagnostics) { |
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.
It took me a while to understand what this function is doing now. While I think the previous function signature was basically self-explanatory, a function called dirFiles
which takes two arguments isn't quite as intuitive. Perhaps a comment would help.
As a starting point, here's my understanding, which might of course be wrong:
dirFiles
finds Terraform configuration file paths which are direct descendants ofdir
, splitting them into primary files and overrides based on filename. IftestsDir
is non-empty and a sub-directory of that name exists,dirFiles
also finds test file paths in that sub-directory and returns them separately.
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.
Added a comment!
@@ -111,6 +168,13 @@ func (p *Parser) dirFiles(dir string) (primary, override []string, diags hcl.Dia | |||
continue | |||
} | |||
|
|||
if ext == ".tftest" || ext == ".tftest.json" { |
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.
Why do we handle .tftest.json
here, but not in fileExt
below?
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.
This was a bug, fixed and added a test that loads .tftest.json files to verify the fix.
internal/configs/test_file.go
Outdated
} | ||
|
||
if attr, exists := content.Attributes["command"]; exists { | ||
expr, exprDiags := shimTraversalInString(attr.Expr, true) |
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.
Why are we using shimTraversalInString
here (and for "mode") instead of using the expression directly? I thought this was just for legacy syntax compatibility.
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.
I liked the functionality of shimTraversalInString in that it allowed users to also specify the keyword with quotation marks without crashing, so I reused it. I didn't realise it was just for older parts of the config, and have now read the doc comment more thoroughly.
I've removed referencing to it and I'm just using the expression directly now.
Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch. |
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
This PR adds the HCL representation of our testing files into the configs package.
It includes updates to the parsing library with the introduction of a set of parallel
*WithTests
functions that load any testing files alongside the requested root module. New functions were introduced (instead of expanding existing functions) to ensure that import operations (such as plan or apply) aren't blocked by syntax errors in or partial implementations of testing files. This matches the behaviour of the Go programming languages, wherego build main.go
will not load any testing files. In a later PR, theterraform validate
command will be updated to load and also validate the testing files but for the time being onlyterraform test
will load and use the new testing files.This PR also includes the ability to customise the testing directory that contains the test files. This will eventually be hooked up to the test command itself.
Target Release
1.6.0
Terraform Testing Framework
This PR is part of a chain of PRs implementing the new Terraform test command.
The above PRs implement the core functionality of the new test command. More PRs will follow implementing specific features, but the test command will work end-to-end once the above PRs are merged.
These changes are implemented in a chain, with each change building on the previous one to ensure ease of review. To see the combined changes as one, navigate to the last PR in the chain and change it to merge directly into main.