configs: valid-modules test ignores experimental features warning

A common source of churn when we're running experiments is that a module
that would otherwise be valid ends up generating a warning merely because
the experiment is active. That means we end up needing to shuffle the
test files around if the feature ultimately graduates to stable.

To reduce that churn in simple cases, we'll make an exception to disregard
the "Experiment is active" warning for any experiment that a module has
intentionally opted into, because those warnings are always expected and
not a cause for concern.

It's still possible to test those warnings explicitly using the
testdata/warning-files directory, if needed.
This commit is contained in:
Martin Atkins 2021-06-30 10:25:50 -07:00
parent 708003b035
commit d92b5e5f5e

View File

@ -5,6 +5,8 @@ import (
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/hashicorp/hcl/v2"
) )
// TestParseLoadConfigDirSuccess is a simple test that just verifies that // TestParseLoadConfigDirSuccess is a simple test that just verifies that
@ -31,6 +33,33 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
path := filepath.Join("testdata/valid-modules", name) path := filepath.Join("testdata/valid-modules", name)
mod, diags := parser.LoadConfigDir(path) mod, diags := parser.LoadConfigDir(path)
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
}
if len(diags) != 0 { if len(diags) != 0 {
t.Errorf("unexpected diagnostics") t.Errorf("unexpected diagnostics")
for _, diag := range diags { for _, diag := range diags {