opentofu/internal/configs/parser_values_test.go
Martin Atkins 31349a9c3a Move configs/ to internal/configs/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

114 lines
2.3 KiB
Go

package configs
import (
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestParserLoadValuesFile(t *testing.T) {
tests := map[string]struct {
Source string
Want map[string]cty.Value
DiagCount int
}{
"empty.tfvars": {
"",
map[string]cty.Value{},
0,
},
"empty.json": {
"{}",
map[string]cty.Value{},
0,
},
"zerolen.json": {
"",
map[string]cty.Value{},
2, // syntax error and missing root object
},
"one-number.tfvars": {
"foo = 1\n",
map[string]cty.Value{
"foo": cty.NumberIntVal(1),
},
0,
},
"one-number.tfvars.json": {
`{"foo": 1}`,
map[string]cty.Value{
"foo": cty.NumberIntVal(1),
},
0,
},
"two-bools.tfvars": {
"foo = true\nbar = false\n",
map[string]cty.Value{
"foo": cty.True,
"bar": cty.False,
},
0,
},
"two-bools.tfvars.json": {
`{"foo": true, "bar": false}`,
map[string]cty.Value{
"foo": cty.True,
"bar": cty.False,
},
0,
},
"invalid-syntax.tfvars": {
"foo bar baz\n",
map[string]cty.Value{},
2, // invalid block definition, and unexpected foo block (the latter due to parser recovery behavior)
},
"block.tfvars": {
"foo = true\ninvalid {\n}\n",
map[string]cty.Value{
"foo": cty.True,
},
1, // blocks are not allowed
},
"variables.tfvars": {
"baz = true\nfoo = var.baz\n",
map[string]cty.Value{
"baz": cty.True,
"foo": cty.DynamicVal,
},
1, // variables cannot be referenced here
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
p := testParser(map[string]string{
name: test.Source,
})
got, diags := p.LoadValuesFile(name)
if len(diags) != test.DiagCount {
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.DiagCount)
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
if len(got) != len(test.Want) {
t.Errorf("wrong number of result keys %d; want %d", len(got), len(test.Want))
}
for name, gotVal := range got {
wantVal := test.Want[name]
if wantVal == cty.NilVal {
t.Errorf("unexpected result key %q", name)
continue
}
if !gotVal.RawEquals(wantVal) {
t.Errorf("wrong value for %q\ngot: %#v\nwant: %#v", name, gotVal, wantVal)
continue
}
}
})
}
}