mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
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:
parent
71774c2a37
commit
727e22e762
@ -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).
|
||||
|
7
internal/command/testdata/init-syntax-invalid-backend-invalid/main.tf
vendored
Normal file
7
internal/command/testdata/init-syntax-invalid-backend-invalid/main.tf
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
terraform {
|
||||
backend "nonexistent" {}
|
||||
}
|
||||
|
||||
bad_block {
|
||||
}
|
||||
|
3
internal/command/testdata/init-syntax-invalid-no-backend/main.tf
vendored
Normal file
3
internal/command/testdata/init-syntax-invalid-no-backend/main.tf
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
bad_block {
|
||||
}
|
||||
|
7
internal/command/testdata/init-syntax-invalid-with-backend/main.tf
vendored
Normal file
7
internal/command/testdata/init-syntax-invalid-with-backend/main.tf
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
terraform {
|
||||
backend "local" {}
|
||||
}
|
||||
|
||||
bad_block {
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user