Skip to content
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

formatter(compact): print hcl.Diagnostics errors in compact format #1482

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions formatter/compact.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package formatter

import (
"errors"
"fmt"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint/tflint"
)

Expand All @@ -25,6 +27,24 @@ func (f *Formatter) compactPrint(issues tflint.Issues, appErr error, sources map
}

if appErr != nil {
var diags hcl.Diagnostics
if errors.As(appErr, &diags) {
for _, diag := range diags {
fmt.Fprintf(
f.Stdout,
"%s:%d:%d: %s - %s. %s\n",
diag.Subject.Filename,
diag.Subject.Start.Line,
diag.Subject.Start.Column,
fromHclSeverity(diag.Severity),
diag.Summary,
diag.Detail,
)
}

return
}

f.prettyPrintErrors(appErr, sources)
}
}
25 changes: 24 additions & 1 deletion formatter/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package formatter

import (
"bytes"
"errors"
"testing"

hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/terraform-linters/tflint/tflint"
)

Expand All @@ -14,6 +16,7 @@ func Test_compactPrint(t *testing.T) {
Issues tflint.Issues
Error error
Stdout string
Stderr string
}{
{
Name: "no issues",
Expand All @@ -38,6 +41,16 @@ func Test_compactPrint(t *testing.T) {
test.tf:1:1: Error - test (test_rule)
`,
},
{
Name: "error",
Error: errors.New("an error occurred"),
Stderr: "an error occurred\n",
},
{
Name: "diagnostics",
Error: hclDiags(`resource "foo" "bar" {`),
Stdout: "main.tf:1:22: error - Unclosed configuration block. There is no closing brace for this block before the end of the file. This may be caused by incorrect brace nesting elsewhere in this file.\n",
},
}

for _, tc := range cases {
Expand All @@ -48,7 +61,17 @@ test.tf:1:1: Error - test (test_rule)
formatter.compactPrint(tc.Issues, tc.Error, map[string][]byte{})

if stdout.String() != tc.Stdout {
t.Fatalf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String())
t.Errorf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String())
}

if stderr.String() != tc.Stderr {
t.Errorf("Failed %s test: expected=%s, stderr=%s", tc.Name, tc.Stderr, stderr.String())
}
}
}

func hclDiags(src string) hcl.Diagnostics {
parser := hclparse.NewParser()
_, diags := parser.ParseHCL([]byte(src), "main.tf")
return diags
}