test: Make tests tolerant of warnings in the returned diagnostics

Prior to this change the strings.Contains assertion was only receiving a string representing the warning, and the error diagnostic was no longer being asserted against.
pss/change-experiment-control
Sarah French 6 months ago
parent 6c4f73914b
commit f104b0b2d0

@ -551,7 +551,12 @@ func TestModule_conflicting_backend_cloud_stateStore(t *testing.T) {
t.Fatal("module should have error diags, but does not")
}
if got := diags.Error(); !strings.Contains(got, tc.wantMsg) {
if tc.allowExperiments {
if len(diags) != 2 && len(diags.Errs()) != 1 {
t.Fatalf("expected 2 diagnostics (1 error, 1 warning), but got: %#v", diags)
}
}
if got := diags.Errs()[0]; !strings.Contains(got.Error(), tc.wantMsg) {
t.Fatalf("expected error to contain %q\nerror was:\n%s", tc.wantMsg, got)
}
})
@ -674,9 +679,13 @@ func TestModule_state_store_multiple(t *testing.T) {
if !diags.HasErrors() {
t.Fatal("module should have error diags, but does not")
}
if len(diags.Errs()) != 1 {
t.Fatalf("expected 1 error, but received %d", len(diags.Errs()))
}
want := `Duplicate 'state_store' configuration block`
if got := diags.Error(); !strings.Contains(got, want) {
err := diags.Errs()[0]
if got := err.Error(); !strings.Contains(got, want) {
t.Fatalf("expected error to contain %q\nerror was:\n%s", want, got)
}
})

@ -36,11 +36,16 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
t.Run(name, func(t *testing.T) {
parser := NewParser(nil)
var expectWarning bool
if strings.Contains(name, "state-store") {
// The PSS project is currently gated as experimental
// TODO(SarahFrench/radeksimko) - remove this from the test once
// the feature is GA.
parser.allowExperiments = true
// While the feature is experimental a warning will always be returned
// about the feature.
expectWarning = true
}
path := filepath.Join("testdata/valid-modules", name)
@ -73,8 +78,20 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
}
diags = filterDiags
}
if len(diags) != 0 {
t.Errorf("unexpected diagnostics")
if diags.HasErrors() {
t.Errorf("unexpected error diagnostics")
for _, diag := range diags.Errs() {
t.Logf("- %s", diag)
}
}
if !expectWarning && len(diags) != 0 {
t.Errorf("unexpected diagnostics: expected no warnings")
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
if expectWarning && len(diags) != 1 {
t.Errorf("unexpected diagnostics: expected a single warning")
for _, diag := range diags {
t.Logf("- %s", diag)
}

Loading…
Cancel
Save