2018-02-07 01:28:40 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2021-06-30 19:25:50 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-02-07 01:28:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestParseLoadConfigDirSuccess is a simple test that just verifies that
|
2019-06-30 09:38:36 +02:00
|
|
|
// a number of test configuration directories (in testdata/valid-modules)
|
2018-02-07 01:28:40 +01:00
|
|
|
// can be parsed without raising any diagnostics.
|
|
|
|
//
|
2019-06-30 09:38:36 +02:00
|
|
|
// It also re-tests the individual files in testdata/valid-files as if
|
2018-02-07 01:28:40 +01:00
|
|
|
// 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) {
|
2019-06-30 09:38:36 +02:00
|
|
|
dirs, err := ioutil.ReadDir("testdata/valid-modules")
|
2018-02-07 01:28:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range dirs {
|
|
|
|
name := info.Name()
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
parser := NewParser(nil)
|
2019-06-30 09:38:36 +02:00
|
|
|
path := filepath.Join("testdata/valid-modules", name)
|
2018-02-07 01:28:40 +01:00
|
|
|
|
2018-05-01 21:06:14 +02:00
|
|
|
mod, diags := parser.LoadConfigDir(path)
|
2021-06-30 19:25:50 +02:00
|
|
|
if len(diags) != 0 && len(mod.ActiveExperiments) != 0 {
|
|
|
|
// As a special case to reduce churn while we're working
|
|
|
|
// through experimental features, we'll ignore the warning
|
|
|
|
// that an experimental feature is active if the module
|
|
|
|
// intentionally opted in to that feature.
|
|
|
|
// If you want to explicitly test for the feature warning
|
|
|
|
// to be generated, consider using testdata/warning-files
|
|
|
|
// instead.
|
|
|
|
filterDiags := make(hcl.Diagnostics, 0, len(diags))
|
|
|
|
for _, diag := range diags {
|
|
|
|
if diag.Severity != hcl.DiagWarning {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
match := false
|
|
|
|
for exp := range mod.ActiveExperiments {
|
|
|
|
allowedSummary := fmt.Sprintf("Experimental feature %q is active", exp.Keyword())
|
|
|
|
if diag.Summary == allowedSummary {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
filterDiags = append(filterDiags, diag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
diags = filterDiags
|
|
|
|
}
|
2018-02-07 01:28:40 +01:00
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics")
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 21:06:14 +02:00
|
|
|
|
|
|
|
if mod.SourceDir != path {
|
|
|
|
t.Errorf("wrong SourceDir value %q; want %s", mod.SourceDir, path)
|
|
|
|
}
|
2018-02-07 01:28:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:38:36 +02:00
|
|
|
// The individual files in testdata/valid-files should also work
|
2018-02-07 01:28:40 +01:00
|
|
|
// when loaded as modules.
|
2019-06-30 09:38:36 +02:00
|
|
|
files, err := ioutil.ReadDir("testdata/valid-files")
|
2018-02-07 01:28:40 +01:00
|
|
|
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) {
|
2019-06-30 09:38:36 +02:00
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata/valid-files", name))
|
2018-02-07 01:28:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := testParser(map[string]string{
|
|
|
|
"mod/" + name: string(src),
|
|
|
|
})
|
|
|
|
|
|
|
|
_, diags := parser.LoadConfigDir("mod")
|
2018-02-15 19:17:36 +01:00
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Errorf("unexpected error diagnostics")
|
2018-02-07 01:28:40 +01:00
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestParseLoadConfigDirFailure is a simple test that just verifies that
|
2019-06-30 09:38:36 +02:00
|
|
|
// a number of test configuration directories (in testdata/invalid-modules)
|
2018-02-07 01:28:40 +01:00
|
|
|
// produce diagnostics when parsed.
|
|
|
|
//
|
2019-06-30 09:38:36 +02:00
|
|
|
// It also re-tests the individual files in testdata/invalid-files as if
|
2018-02-07 01:28:40 +01:00
|
|
|
// 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) {
|
2019-06-30 09:38:36 +02:00
|
|
|
dirs, err := ioutil.ReadDir("testdata/invalid-modules")
|
2018-02-07 01:28:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range dirs {
|
|
|
|
name := info.Name()
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
parser := NewParser(nil)
|
2019-06-30 09:38:36 +02:00
|
|
|
path := filepath.Join("testdata/invalid-modules", name)
|
2018-02-07 01:28:40 +01:00
|
|
|
|
|
|
|
_, diags := parser.LoadConfigDir(path)
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
t.Errorf("no errors; want at least one")
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:38:36 +02:00
|
|
|
// The individual files in testdata/valid-files should also work
|
2018-02-07 01:28:40 +01:00
|
|
|
// when loaded as modules.
|
2019-06-30 09:38:36 +02:00
|
|
|
files, err := ioutil.ReadDir("testdata/invalid-files")
|
2018-02-07 01:28:40 +01:00
|
|
|
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) {
|
2019-06-30 09:38:36 +02:00
|
|
|
src, err := ioutil.ReadFile(filepath.Join("testdata/invalid-files", name))
|
2018-02-07 01:28:40 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-07-18 19:07:10 +02:00
|
|
|
|
|
|
|
func TestIsEmptyDir(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(filepath.Join("testdata", "valid-files"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if val {
|
|
|
|
t.Fatal("should not be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsEmptyDir_noExist(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(filepath.Join("testdata", "nopenopenope"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !val {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsEmptyDir_noConfigs(t *testing.T) {
|
|
|
|
val, err := IsEmptyDir(filepath.Join("testdata", "dir-empty"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !val {
|
|
|
|
t.Fatal("should be empty")
|
|
|
|
}
|
|
|
|
}
|