terraform: test helpers for printing diagnostics in a useful way
We often want to bail out of a test if diagnostics are present, and it's easiest to debug that when the diagnostics are printed in a compact but complete manner that is non-trivial to produce. Rather than duplicating that diagnostic formatting in every test, these helpers allow us to succinctly print diagnostics and bail out when they are present.
This commit is contained in:
parent
efe631d9ec
commit
ac81511ba6
|
@ -14,7 +14,6 @@ import (
|
|||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
"github.com/hashicorp/hil"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
@ -31,6 +30,7 @@ import (
|
|||
"github.com/hashicorp/terraform/provisioners"
|
||||
"github.com/hashicorp/terraform/states"
|
||||
"github.com/hashicorp/terraform/states/statefile"
|
||||
"github.com/hashicorp/terraform/tfdiags"
|
||||
tfversion "github.com/hashicorp/terraform/version"
|
||||
)
|
||||
|
||||
|
@ -944,6 +944,64 @@ func legacyDiffComparisonString(changes *plans.Changes) string {
|
|||
return buf.String()
|
||||
}
|
||||
|
||||
// assertNoDiagnostics fails the test in progress (using t.Fatal) if the given
|
||||
// diagnostics is non-empty.
|
||||
func assertNoDiagnostics(t *testing.T, diags tfdiags.Diagnostics) {
|
||||
t.Helper()
|
||||
if len(diags) == 0 {
|
||||
return
|
||||
}
|
||||
logDiagnostics(t, diags)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// assertNoDiagnostics fails the test in progress (using t.Fatal) if the given
|
||||
// diagnostics has any errors.
|
||||
func assertNoErrors(t *testing.T, diags tfdiags.Diagnostics) {
|
||||
t.Helper()
|
||||
if !diags.HasErrors() {
|
||||
return
|
||||
}
|
||||
logDiagnostics(t, diags)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// logDiagnostics is a test helper that logs the given diagnostics to to the
|
||||
// given testing.T using t.Log, in a way that is hopefully useful in debugging
|
||||
// a test. It does not generate any errors or fail the test. See
|
||||
// assertNoDiagnostics and assertNoErrors for more specific helpers that can
|
||||
// also fail the test.
|
||||
func logDiagnostics(t *testing.T, diags tfdiags.Diagnostics) {
|
||||
for _, diag := range diags {
|
||||
desc := diag.Description()
|
||||
rng := diag.Source()
|
||||
|
||||
var severity string
|
||||
switch diag.Severity() {
|
||||
case tfdiags.Error:
|
||||
severity = "ERROR"
|
||||
case tfdiags.Warning:
|
||||
severity = "WARN"
|
||||
default:
|
||||
severity = "???" // should never happen
|
||||
}
|
||||
|
||||
if subj := rng.Subject; subj != nil {
|
||||
if desc.Detail == "" {
|
||||
t.Logf("[%s@%s] %s", severity, subj.StartString(), desc.Summary)
|
||||
} else {
|
||||
t.Logf("[%s@%s] %s: %s", severity, subj.StartString(), desc.Summary, desc.Detail)
|
||||
}
|
||||
} else {
|
||||
if desc.Detail == "" {
|
||||
t.Logf("[%s] %s", severity, desc.Summary)
|
||||
} else {
|
||||
t.Logf("[%s] %s: %s", severity, desc.Summary, desc.Detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const testContextGraph = `
|
||||
root: root
|
||||
aws_instance.bar
|
||||
|
|
Loading…
Reference in New Issue