Skip to content

Commit 940fbfb

Browse files
authored
test: Add more test coverage of validate command's interaction with backend blocks (#37977)
* test: Show how the validate command doesn't detect when an unknown backend type is in the config. * test: Minor fixes to error messages when test fails * test: Add some TODOs in code comments These tests are here to define existing behaviour, not define desired behaviour. I wanted to make that clear!
1 parent 33d54b9 commit 940fbfb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
backend "shipping-container" {}
3+
}

internal/command/validate_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,43 @@ func TestValidate_backendBlocks(t *testing.T) {
538538
if code != 1 {
539539
t.Fatalf("unexpected successful exit code %d\n\n%s", code, output.Stdout())
540540
}
541-
if !strings.Contains(output.Stderr(), "Error: Attribute redefined") {
541+
expectedErr := "Error: Attribute redefined"
542+
if !strings.Contains(output.Stderr(), expectedErr) {
542543
t.Fatalf("unexpected error content: wanted %q, got: %s",
543-
"Error: Attribute redefined",
544+
expectedErr,
544545
output.Stderr(),
545546
)
546547
}
547548
})
548549

550+
// TODO: Should this validation be added?
551+
t.Run("NOT invalid when the backend type is unknown", func(t *testing.T) {
552+
output, code := setupTest(t, "invalid-backend-configuration/unknown-backend-type")
553+
if code != 0 {
554+
t.Fatalf("expected a successful exit code %d\n\n%s", code, output.Stderr())
555+
}
556+
expectedMsg := "Success! The configuration is valid."
557+
if !strings.Contains(output.Stdout(), expectedMsg) {
558+
t.Fatalf("unexpected output content: wanted %q, got: %s",
559+
expectedMsg,
560+
output.Stdout(),
561+
)
562+
}
563+
})
564+
549565
// Backend blocks aren't validated using their schemas currently.
566+
// TODO: Should this validation be added?
550567
t.Run("NOT invalid when there's an unknown attribute present", func(t *testing.T) {
551-
if output, code := setupTest(t, "invalid-backend-configuration/unknown-attr"); code != 0 {
552-
t.Fatalf("unexpected non-successful exit code %d\n\n%s", code, output.Stderr())
568+
output, code := setupTest(t, "invalid-backend-configuration/unknown-attr")
569+
if code != 0 {
570+
t.Fatalf("expected a successful exit code %d\n\n%s", code, output.Stderr())
571+
}
572+
expectedMsg := "Success! The configuration is valid."
573+
if !strings.Contains(output.Stdout(), expectedMsg) {
574+
t.Fatalf("unexpected output content: wanted %q, got: %s",
575+
expectedMsg,
576+
output.Stdout(),
577+
)
553578
}
554579
})
555580
}

0 commit comments

Comments
 (0)