mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
36fb5b52e7
In our new loader we are changing certain values in configuration to be naked keywords or references rather than quoted strings as before. Since many of these have been shown in books, tutorials, and our own documentation we will make the old forms generate deprecation warnings rather than errors so that newcomers starting from older documentation can be eased into the new syntax, rather than getting blocked. This will also avoid creating a hard compatibility wall for reusable modules that are already published, allowing them to still be used in spite of these warnings and then fixed when the maintainer is able.
137 lines
3.6 KiB
Go
137 lines
3.6 KiB
Go
package configs
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestParseLoadConfigDirSuccess is a simple test that just verifies that
|
|
// a number of test configuration directories (in test-fixtures/valid-modules)
|
|
// can be parsed without raising any diagnostics.
|
|
//
|
|
// It also re-tests the individual files in test-fixtures/valid-files as if
|
|
// they were single-file modules, to ensure that they can be bundled into
|
|
// modules correctly.
|
|
//
|
|
// This test does not verify that reading these modules produces the correct
|
|
// module element contents. More detailed assertions may be made on some subset
|
|
// of these configuration files in other tests.
|
|
func TestParserLoadConfigDirSuccess(t *testing.T) {
|
|
dirs, err := ioutil.ReadDir("test-fixtures/valid-modules")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, info := range dirs {
|
|
name := info.Name()
|
|
t.Run(name, func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
path := filepath.Join("test-fixtures/valid-modules", name)
|
|
|
|
_, diags := parser.LoadConfigDir(path)
|
|
if len(diags) != 0 {
|
|
t.Errorf("unexpected diagnostics")
|
|
for _, diag := range diags {
|
|
t.Logf("- %s", diag)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// The individual files in test-fixtures/valid-files should also work
|
|
// when loaded as modules.
|
|
files, err := ioutil.ReadDir("test-fixtures/valid-files")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, info := range files {
|
|
name := info.Name()
|
|
t.Run(fmt.Sprintf("%s as module", name), func(t *testing.T) {
|
|
src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
parser := testParser(map[string]string{
|
|
"mod/" + name: string(src),
|
|
})
|
|
|
|
_, diags := parser.LoadConfigDir("mod")
|
|
if diags.HasErrors() {
|
|
t.Errorf("unexpected error diagnostics")
|
|
for _, diag := range diags {
|
|
t.Logf("- %s", diag)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
// TestParseLoadConfigDirFailure is a simple test that just verifies that
|
|
// a number of test configuration directories (in test-fixtures/invalid-modules)
|
|
// produce diagnostics when parsed.
|
|
//
|
|
// It also re-tests the individual files in test-fixtures/invalid-files as if
|
|
// they were single-file modules, to ensure that their errors are still
|
|
// detected when loading as part of a module.
|
|
//
|
|
// This test does not verify that reading these modules produces any
|
|
// diagnostics in particular. More detailed assertions may be made on some subset
|
|
// of these configuration files in other tests.
|
|
func TestParserLoadConfigDirFailure(t *testing.T) {
|
|
dirs, err := ioutil.ReadDir("test-fixtures/invalid-modules")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, info := range dirs {
|
|
name := info.Name()
|
|
t.Run(name, func(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
path := filepath.Join("test-fixtures/invalid-modules", name)
|
|
|
|
_, diags := parser.LoadConfigDir(path)
|
|
if !diags.HasErrors() {
|
|
t.Errorf("no errors; want at least one")
|
|
for _, diag := range diags {
|
|
t.Logf("- %s", diag)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// The individual files in test-fixtures/valid-files should also work
|
|
// when loaded as modules.
|
|
files, err := ioutil.ReadDir("test-fixtures/invalid-files")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, info := range files {
|
|
name := info.Name()
|
|
t.Run(fmt.Sprintf("%s as module", 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{
|
|
"mod/" + name: string(src),
|
|
})
|
|
|
|
_, diags := parser.LoadConfigDir("mod")
|
|
if !diags.HasErrors() {
|
|
t.Errorf("no errors; want at least one")
|
|
for _, diag := range diags {
|
|
t.Logf("- %s", diag)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|