-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command: New -compact-warnings option
When warnings appear in isolation (not accompanied by an error) it's reasonable to want to defer resolving them for a while because they are not actually blocking immediate work. However, our warning messages tend to be long by default in order to include all of the necessary context to understand the implications of the warning, and that can make them overwhelming when combined with other output. As a compromise, this adds a new CLI option -compact-warnings which is supported for all the main operation commands and which uses a more compact format to print out warnings as long as they aren't also accompanied by errors. The default remains unchanged except that the threshold for consolidating warning messages is reduced to one so that we'll now only show one of each distinct warning summary. Full warning messages are always shown if there's at least one error included in the diagnostic set too, because in that case the warning message could contain additional context to help understand the error.
- Loading branch information
1 parent
5421a62
commit c06675c
Showing
11 changed files
with
198 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package format | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/mitchellh/colorstring" | ||
|
||
"github.com/hashicorp/terraform/tfdiags" | ||
) | ||
|
||
func TestDiagnosticWarningsCompact(t *testing.T) { | ||
var diags tfdiags.Diagnostics | ||
diags = diags.Append(tfdiags.SimpleWarning("foo")) | ||
diags = diags.Append(tfdiags.SimpleWarning("foo")) | ||
diags = diags.Append(tfdiags.SimpleWarning("bar")) | ||
diags = diags.Append(&hcl.Diagnostic{ | ||
Severity: hcl.DiagWarning, | ||
Summary: "source foo", | ||
Detail: "...", | ||
Subject: &hcl.Range{ | ||
Filename: "source.tf", | ||
Start: hcl.Pos{Line: 2, Column: 1, Byte: 5}, | ||
End: hcl.Pos{Line: 2, Column: 1, Byte: 5}, | ||
}, | ||
}) | ||
diags = diags.Append(&hcl.Diagnostic{ | ||
Severity: hcl.DiagWarning, | ||
Summary: "source foo", | ||
Detail: "...", | ||
Subject: &hcl.Range{ | ||
Filename: "source.tf", | ||
Start: hcl.Pos{Line: 3, Column: 1, Byte: 7}, | ||
End: hcl.Pos{Line: 3, Column: 1, Byte: 7}, | ||
}, | ||
}) | ||
diags = diags.Append(&hcl.Diagnostic{ | ||
Severity: hcl.DiagWarning, | ||
Summary: "source bar", | ||
Detail: "...", | ||
Subject: &hcl.Range{ | ||
Filename: "source2.tf", | ||
Start: hcl.Pos{Line: 1, Column: 1, Byte: 1}, | ||
End: hcl.Pos{Line: 1, Column: 1, Byte: 1}, | ||
}, | ||
}) | ||
|
||
// ConsolidateWarnings groups together the ones | ||
// that have source location information and that | ||
// have the same summary text. | ||
diags = diags.ConsolidateWarnings(1) | ||
|
||
// A zero-value Colorize just passes all the formatting | ||
// codes back to us, so we can test them literally. | ||
got := DiagnosticWarningsCompact(diags, &colorstring.Colorize{}) | ||
want := `[bold][yellow]Warnings:[reset] | ||
- foo | ||
- foo | ||
- bar | ||
- source foo | ||
on source.tf line 2 (and 1 more) | ||
- source bar | ||
on source2.tf line 1 | ||
` | ||
if got != want { | ||
t.Errorf( | ||
"wrong result\ngot:\n%s\n\nwant:\n%s\n\ndiff:\n%s", | ||
got, want, cmp.Diff(want, got), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters