mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
configs: Another simple test for _invalid_ config files
Much like TestParserLoadConfigFileSuccess, this is intended to be an easy-to-maintain collection of bad examples to test different permutations of our error handling. As with TestParserLoadConfigFileSuccess, we should also have more specific tests alongside this that check that the error outcome is what was expected, since this test just accepts any error and may thus not be testing what we think it is.
This commit is contained in:
parent
e524d1eb95
commit
9be399d49c
@ -4,6 +4,8 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/hcl2/hcl"
|
||||
)
|
||||
|
||||
// TestParseLoadConfigFileSuccess is a simple test that just verifies that
|
||||
@ -41,3 +43,102 @@ func TestParserLoadConfigFileSuccess(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseLoadConfigFileFailure is a simple test that just verifies that
|
||||
// a number of test configuration files (in test-fixtures/invalid-files)
|
||||
// produce errors as expected.
|
||||
//
|
||||
// This test does not verify specific error messages, so more detailed
|
||||
// assertions should be made on some subset of these configuration files in
|
||||
// other tests.
|
||||
func TestParserLoadConfigFileFailure(t *testing.T) {
|
||||
files, err := ioutil.ReadDir("test-fixtures/invalid-files")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, info := range files {
|
||||
name := info.Name()
|
||||
t.Run(name, func(t *testing.T) {
|
||||
src, err := ioutil.ReadFile(filepath.Join("test-fixtures/invalid-files", name))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parser := testParser(map[string]string{
|
||||
name: string(src),
|
||||
})
|
||||
|
||||
_, diags := parser.LoadConfigFile(name)
|
||||
if !diags.HasErrors() {
|
||||
t.Errorf("LoadConfigFile succeeded; want errors")
|
||||
}
|
||||
for _, diag := range diags {
|
||||
t.Logf("- %s", diag)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This test uses a subset of the same fixture files as
|
||||
// TestParserLoadConfigFileFailure, but additionally verifies that each
|
||||
// file produces the expected diagnostic summary.
|
||||
func TestParserLoadConfigFileFailureMessages(t *testing.T) {
|
||||
tests := []struct {
|
||||
Filename string
|
||||
WantError string
|
||||
}{
|
||||
{
|
||||
"data-resource-lifecycle.tf",
|
||||
"Unsupported lifecycle block",
|
||||
},
|
||||
{
|
||||
"variable-type-unknown.tf",
|
||||
"Invalid variable type hint",
|
||||
},
|
||||
{
|
||||
"variable-type-quoted.tf",
|
||||
"Invalid variable type hint",
|
||||
},
|
||||
{
|
||||
"unexpected-attr.tf",
|
||||
"Unsupported attribute",
|
||||
},
|
||||
{
|
||||
"unexpected-block.tf",
|
||||
"Unsupported block type",
|
||||
},
|
||||
{
|
||||
"resource-lifecycle-badbool.tf",
|
||||
"Unsuitable value type",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Filename, func(t *testing.T) {
|
||||
src, err := ioutil.ReadFile(filepath.Join("test-fixtures/invalid-files", test.Filename))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parser := testParser(map[string]string{
|
||||
test.Filename: string(src),
|
||||
})
|
||||
|
||||
_, diags := parser.LoadConfigFile(test.Filename)
|
||||
if len(diags) != 1 {
|
||||
t.Errorf("Wrong number of diagnostics %d; want 1", len(diags))
|
||||
for _, diag := range diags {
|
||||
t.Logf("- %s", diag)
|
||||
}
|
||||
return
|
||||
}
|
||||
if diags[0].Severity != hcl.DiagError {
|
||||
t.Errorf("Wrong diagnostic severity %s; want %s", diags[0].Severity, hcl.DiagError)
|
||||
}
|
||||
if diags[0].Summary != test.WantError {
|
||||
t.Errorf("Wrong diagnostic summary\ngot: %s\nwant: %s", diags[0].Summary, test.WantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
data "example" "example" {
|
||||
lifecycle {
|
||||
# This block intentionally left blank
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
resource "example" "example" {
|
||||
lifecycle {
|
||||
create_before_destroy = "ABSOLUTELY NOT"
|
||||
}
|
||||
}
|
1
configs/test-fixtures/invalid-files/unexpected-attr.tf
Normal file
1
configs/test-fixtures/invalid-files/unexpected-attr.tf
Normal file
@ -0,0 +1 @@
|
||||
foo = "bar"
|
2
configs/test-fixtures/invalid-files/unexpected-block.tf
Normal file
2
configs/test-fixtures/invalid-files/unexpected-block.tf
Normal file
@ -0,0 +1,2 @@
|
||||
varyable "whoops" {
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
variable "bad_type" {
|
||||
type = "string"
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
variable "bad_type" {
|
||||
type = notatype
|
||||
}
|
0
configs/test-fixtures/invalid-files/zerolen.tf.json
Normal file
0
configs/test-fixtures/invalid-files/zerolen.tf.json
Normal file
Loading…
Reference in New Issue
Block a user