Skip to content

Commit 77cd52f

Browse files
committed
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.
1 parent 159326a commit 77cd52f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

internal/configs/module_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,12 @@ func TestModule_conflicting_backend_cloud_stateStore(t *testing.T) {
551551
t.Fatal("module should have error diags, but does not")
552552
}
553553

554-
if got := diags.Error(); !strings.Contains(got, tc.wantMsg) {
554+
if tc.allowExperiments {
555+
if len(diags) != 2 && len(diags.Errs()) != 1 {
556+
t.Fatalf("expected 2 diagnostics (1 error, 1 warning), but got: %#v", diags)
557+
}
558+
}
559+
if got := diags.Errs()[0]; !strings.Contains(got.Error(), tc.wantMsg) {
555560
t.Fatalf("expected error to contain %q\nerror was:\n%s", tc.wantMsg, got)
556561
}
557562
})
@@ -674,9 +679,13 @@ func TestModule_state_store_multiple(t *testing.T) {
674679
if !diags.HasErrors() {
675680
t.Fatal("module should have error diags, but does not")
676681
}
682+
if len(diags.Errs()) != 1 {
683+
t.Fatalf("expected 1 error, but received %d", len(diags.Errs()))
684+
}
677685

678686
want := `Duplicate 'state_store' configuration block`
679-
if got := diags.Error(); !strings.Contains(got, want) {
687+
err := diags.Errs()[0]
688+
if got := err.Error(); !strings.Contains(got, want) {
680689
t.Fatalf("expected error to contain %q\nerror was:\n%s", want, got)
681690
}
682691
})

internal/configs/parser_config_dir_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
3636
t.Run(name, func(t *testing.T) {
3737
parser := NewParser(nil)
3838

39+
var expectWarning bool
3940
if strings.Contains(name, "state-store") {
4041
// The PSS project is currently gated as experimental
4142
// TODO(SarahFrench/radeksimko) - remove this from the test once
4243
// the feature is GA.
4344
parser.allowExperiments = true
45+
46+
// While the feature is experimental a warning will always be returned
47+
// about the feature.
48+
expectWarning = true
4449
}
4550

4651
path := filepath.Join("testdata/valid-modules", name)
@@ -73,8 +78,20 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
7378
}
7479
diags = filterDiags
7580
}
76-
if len(diags) != 0 {
77-
t.Errorf("unexpected diagnostics")
81+
if diags.HasErrors() {
82+
t.Errorf("unexpected error diagnostics")
83+
for _, diag := range diags.Errs() {
84+
t.Logf("- %s", diag)
85+
}
86+
}
87+
if !expectWarning && len(diags) != 0 {
88+
t.Errorf("unexpected diagnostics: expected no warnings")
89+
for _, diag := range diags {
90+
t.Logf("- %s", diag)
91+
}
92+
}
93+
if expectWarning && len(diags) != 1 {
94+
t.Errorf("unexpected diagnostics: expected a single warning")
7895
for _, diag := range diags {
7996
t.Logf("- %s", diag)
8097
}

0 commit comments

Comments
 (0)