add tests for init syntax error handling

With the demise of the early config loader, we want to show core
version errors first, followed by backend errors, and only then
show other errors with the configuration.
This commit is contained in:
CJ Horton 2023-02-15 19:29:58 -08:00
parent 71774c2a37
commit 727e22e762
4 changed files with 108 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/configs/configschema"
@ -2620,6 +2621,96 @@ func TestInit_invalidBuiltInProviders(t *testing.T) {
}
}
func TestInit_invalidSyntaxNoBackend(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("init-syntax-invalid-no-backend"), td)
defer testChdir(t, td)()
ui := cli.NewMockUi()
view, _ := testView(t)
m := Meta{
Ui: ui,
View: view,
}
c := &InitCommand{
Meta: m,
}
if code := c.Run(nil); code == 0 {
t.Fatalf("succeeded, but was expecting error\nstdout:\n%s\nstderr:\n%s", ui.OutputWriter, ui.ErrorWriter)
}
errStr := ui.ErrorWriter.String()
if subStr := "There are some problems with the configuration, described below"; !strings.Contains(errStr, subStr) {
t.Errorf("Error output should include preamble\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
if subStr := "Error: Unsupported block type"; !strings.Contains(errStr, subStr) {
t.Errorf("Error output should mention the syntax problem\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
}
func TestInit_invalidSyntaxWithBackend(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("init-syntax-invalid-with-backend"), td)
defer testChdir(t, td)()
ui := cli.NewMockUi()
view, _ := testView(t)
m := Meta{
Ui: ui,
View: view,
}
c := &InitCommand{
Meta: m,
}
if code := c.Run(nil); code == 0 {
t.Fatalf("succeeded, but was expecting error\nstdout:\n%s\nstderr:\n%s", ui.OutputWriter, ui.ErrorWriter)
}
errStr := ui.ErrorWriter.String()
if subStr := "There are some problems with the configuration, described below"; !strings.Contains(errStr, subStr) {
t.Errorf("Error output should include preamble\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
if subStr := "Error: Unsupported block type"; !strings.Contains(errStr, subStr) {
t.Errorf("Error output should mention the syntax problem\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
}
func TestInit_invalidSyntaxInvalidBackend(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("init-syntax-invalid-backend-invalid"), td)
defer testChdir(t, td)()
ui := cli.NewMockUi()
view, _ := testView(t)
m := Meta{
Ui: ui,
View: view,
}
c := &InitCommand{
Meta: m,
}
if code := c.Run(nil); code == 0 {
t.Fatalf("succeeded, but was expecting error\nstdout:\n%s\nstderr:\n%s", ui.OutputWriter, ui.ErrorWriter)
}
errStr := ui.ErrorWriter.String()
if subStr := "There are some problems with the configuration, described below"; strings.Contains(errStr, subStr) {
t.Errorf("Error output should not include preamble\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
if subStr := "Error: Unsupported block type"; strings.Contains(errStr, subStr) {
t.Errorf("Error output should not mention syntax errors\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
if subStr := "Error: Unsupported backend type"; !strings.Contains(errStr, subStr) {
t.Errorf("Error output should mention the invalid backend\nwant substr: %s\ngot:\n%s", subStr, errStr)
}
}
// newMockProviderSource is a helper to succinctly construct a mock provider
// source that contains a set of packages matching the given provider versions
// that are available for installation (from temporary local files).

View File

@ -0,0 +1,7 @@
terraform {
backend "nonexistent" {}
}
bad_block {
}

View File

@ -0,0 +1,3 @@
bad_block {
}

View File

@ -0,0 +1,7 @@
terraform {
backend "local" {}
}
bad_block {
}