-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix #2072 make sure the generated
code is formatted before writing it to a file.
#3917
fix #2072 make sure the generated
code is formatted before writing it to a file.
#3917
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis pull request introduces a new pointer boolean field Changes
Sequence Diagram(s)sequenceDiagram
participant T as Test
participant W as WriteToFile
participant H as hclwrite.Format
participant OS as os.WriteFile
T->>W: Call WriteToFile(contents)
W->>H: Format(contents)
H-->>W: Return formattedContents
W->>OS: Write formattedContents
OS-->>W: Write success/error
W->>T: Return result
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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)
codegen/generate.go (1)
141-141
: Nice improvement! 👍Good call on using
hclwrite.Format
to ensure consistent formatting. The placement right before writing to file is perfect.Consider extracting the formatting into a separate function for better testability and reuse:
-if err := os.WriteFile(targetPath, hclwrite.Format([]byte(contentsToWrite)), ownerWriteGlobalReadPerms); err != nil { +formattedContent := hclwrite.Format([]byte(contentsToWrite)) +if err := os.WriteFile(targetPath, formattedContent, ownerWriteGlobalReadPerms); err != nil {codegen/generate_test.go (1)
148-201
: Great test structure! Let's add more test cases.The table-driven test pattern is perfect for this, and the first test case is a good start. However, we could strengthen the test coverage with more cases.
Consider adding these test cases:
tc := []struct { name string disabled bool path string contents string expected string ifExists codegen.GenerateConfigExists }{ { "generate-simple-example", false, fmt.Sprintf("%s/%s", testDir, "simple_example"), "variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n", "variable \"msg\" {\n type = string\n default = \"hello\"\n}\n", codegen.ExistsError, }, + { + "generate-block-with-nested-blocks", + false, + fmt.Sprintf("%s/%s", testDir, "nested_blocks"), + "resource\"aws_instance\"\"example\"{\nami=\"ami-123\"\ntags={\nName=\"test\"\n}\n}\n", + "resource \"aws_instance\" \"example\" {\n ami = \"ami-123\"\n tags = {\n Name = \"test\"\n }\n}\n", + codegen.ExistsError, + }, + { + "generate-with-comments", + false, + fmt.Sprintf("%s/%s", testDir, "with_comments"), + "#test comment\nvariable\"foo\"{#another comment\ntype=string}\n", + "# test comment\nvariable \"foo\" { # another comment\n type = string\n}\n", + codegen.ExistsError, + }, },
generated
code is formatted before writing it to a file.generated
code is formatted before writing it to a file.
60d607e
to
db053bd
Compare
@coderabbitai review |
✅ Actions performedReview triggered.
|
generated
code is formatted before writing it to a file.generated
code is formatted before writing it to a file.
hey @yhakbar, I added an |
We're very close now. What I thought would be done is to define the value of |
…ore writing it to a file.
db053bd
to
7c9f0d6
Compare
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 (1)
codegen/generate_test.go (1)
148-221
: Great test coverage! Consider adding one more case.The test cases thoroughly cover the formatting scenarios. Nice job using table-driven tests and temporary directories! 🎯
Would you consider adding one more test case for a non-HCL file extension (e.g., .txt) to verify it's not formatted by default?
Here's a suggested test case to add:
tc := []struct { // ... existing fields ... }{ + { + name: "ignore-non-hcl-by-default", + path: fmt.Sprintf("%s/%s", testDir, "unformatted.txt"), + contents: "variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n", + expected: "variable \"msg\"{\ntype=string\n default=\"hello\"\n}\n", + ifExists: codegen.ExistsError, + }, // ... existing test cases ... }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
codegen/generate.go
(2 hunks)codegen/generate_test.go
(2 hunks)docs/_docs/04_reference/04-config-blocks-and-attributes.md
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- docs/_docs/04_reference/04-config-blocks-and-attributes.md
🔇 Additional comments (3)
codegen/generate.go (3)
89-89
: LGTM! Smart use of pointer boolean for the formatting flag.The
HclFmt
field is well-designed, using a pointer boolean to support three states: nil (auto), true (force format), and false (no format).
138-153
: Nice implementation of the formatting logic! 👍The code elegantly handles both automatic and explicit formatting:
- Automatically formats known HCL extensions (.hcl, .tf, .tofu)
- Respects user's explicit preference when set
- Uses a map for efficient extension lookup
156-158
: LGTM! Clean formatting implementation.Good use of
hclwrite.Format
to ensure consistent HCL formatting.
@yhakbar I modified the code based on your suggestions. |
Description
Fixes #2072 make sure the
generated
code is formatted before writing it to a file.TODOs
Read the Gruntwork contribution guidelines.
Release Notes (draft)
Updated the
generated
block to format theHCL
syntax before writing it to a file, fix for #2072.Migration Guide
n/a
Summary by CodeRabbit
New Features
hcl_fmt
attribute in thegenerate
block for automatic formatting of.hcl
,.tf
, and.tofu
files before writing.Tests