opentofu/internal/tfdiags/consolidate_warnings_test.go
Martin Atkins 05caff2ca3 Move tfdiags/ to internal/tfdiags/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

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)
}
}