mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
c06675c616
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.
180 lines
4.5 KiB
Go
180 lines
4.5 KiB
Go
package tfdiags
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/hcl/v2"
|
|
)
|
|
|
|
func TestConsolidateWarnings(t *testing.T) {
|
|
var diags Diagnostics
|
|
|
|
for i := 0; i < 4; i++ {
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
Severity: hcl.DiagWarning,
|
|
Summary: "Warning 1",
|
|
Detail: fmt.Sprintf("This one has a subject %d", i),
|
|
Subject: &hcl.Range{
|
|
Filename: "foo.tf",
|
|
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
End: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
})
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Error 1",
|
|
Detail: fmt.Sprintf("This one has a subject %d", i),
|
|
Subject: &hcl.Range{
|
|
Filename: "foo.tf",
|
|
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
End: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
})
|
|
diags = diags.Append(Sourceless(
|
|
Warning,
|
|
"Warning 2",
|
|
fmt.Sprintf("This one is sourceless %d", i),
|
|
))
|
|
diags = diags.Append(SimpleWarning("Warning 3"))
|
|
}
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
Severity: hcl.DiagWarning,
|
|
Summary: "Warning 4",
|
|
Detail: "Only one of this one",
|
|
Subject: &hcl.Range{
|
|
Filename: "foo.tf",
|
|
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
End: hcl.Pos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
})
|
|
|
|
// We're using ForRPC here to force the diagnostics to be of a consistent
|
|
// type that we can easily assert against below.
|
|
got := diags.ConsolidateWarnings(2).ForRPC()
|
|
want := Diagnostics{
|
|
// First set
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 1",
|
|
Detail_: "This one has a subject 0",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Error,
|
|
Summary_: "Error 1",
|
|
Detail_: "This one has a subject 0",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 2",
|
|
Detail_: "This one is sourceless 0",
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 3",
|
|
},
|
|
|
|
// Second set (consolidation begins; note additional paragraph in Warning 1 detail)
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 1",
|
|
Detail_: "This one has a subject 1\n\n(and 2 more similar warnings elsewhere)",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Error,
|
|
Summary_: "Error 1",
|
|
Detail_: "This one has a subject 1",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 2",
|
|
Detail_: "This one is sourceless 1",
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 3",
|
|
},
|
|
|
|
// Third set (no more Warning 1, because it's consolidated)
|
|
&rpcFriendlyDiag{
|
|
Severity_: Error,
|
|
Summary_: "Error 1",
|
|
Detail_: "This one has a subject 2",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 2",
|
|
Detail_: "This one is sourceless 2",
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 3",
|
|
},
|
|
|
|
// Fourth set (still no warning 1)
|
|
&rpcFriendlyDiag{
|
|
Severity_: Error,
|
|
Summary_: "Error 1",
|
|
Detail_: "This one has a subject 3",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 2",
|
|
Detail_: "This one is sourceless 3",
|
|
},
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 3",
|
|
},
|
|
|
|
// Special straggler warning gets to show up unconsolidated, because
|
|
// there is only one of it.
|
|
&rpcFriendlyDiag{
|
|
Severity_: Warning,
|
|
Summary_: "Warning 4",
|
|
Detail_: "Only one of this one",
|
|
Subject_: &SourceRange{
|
|
Filename: "foo.tf",
|
|
Start: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
End: SourcePos{Line: 1, Column: 1, Byte: 0},
|
|
},
|
|
},
|
|
}
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Errorf("wrong result\n%s", diff)
|
|
}
|
|
}
|