move test case iteration to caller

To make the test case logic more obvious
This commit is contained in:
Daniel Nephin 2020-12-21 18:38:10 -05:00
parent f86ceea803
commit bd728df345
2 changed files with 90 additions and 104 deletions

View File

@ -42,13 +42,18 @@ type testCase struct {
patch func(rt *RuntimeConfig) // expected
err string // expectedErr
warns []string // expectedWarnings
opts LoadOpts
json []string
hcl []string
}
func (tc testCase) source(format string) []string {
if format == "hcl" {
return tc.hcl
}
return tc.json
}
// TestConfigFlagsAndEdgecases tests the command line flags and
// edgecases for the config parsing. It provides a test structure which
// checks for warnings on deprecated fields and flags. These tests
@ -4846,63 +4851,41 @@ func TestLoad_IntegrationWithFlags(t *testing.T) {
},
}
testConfig(t, tests, dataDir)
for _, tc := range tests {
if len(tc.json) == 0 && len(tc.hcl) == 0 {
testConfig(t, tc, "", dataDir)
continue
}
func testConfig(t *testing.T, tests []testCase, dataDir string) {
for _, tt := range tests {
for pass, format := range []string{"json", "hcl"} {
for _, format := range []string{"json", "hcl"} {
testConfig(t, tc, format, dataDir)
}
}
}
func testConfig(t *testing.T, tc testCase, format string, dataDir string) {
t.Run(fmt.Sprintf("%v_%v", tc.desc, format), func(t *testing.T) {
// clean data dir before every test
os.RemoveAll(dataDir)
os.MkdirAll(dataDir, 0755)
// when we test only flags then there are no JSON or HCL
// sources and we need to make only one pass over the
// tests.
flagsOnly := len(tt.json) == 0 && len(tt.hcl) == 0
if flagsOnly && pass > 0 {
continue
if tc.pre != nil {
tc.pre()
}
// json and hcl sources need to be in sync
// to make sure we're generating the same config
if len(tt.json) != len(tt.hcl) {
t.Fatal(tt.desc, ": JSON and HCL test case out of sync")
}
// select the source
srcs := tt.json
if format == "hcl" {
srcs = tt.hcl
}
// build the description
var desc []string
if !flagsOnly {
desc = append(desc, format)
}
if tt.desc != "" {
desc = append(desc, tt.desc)
}
t.Run(strings.Join(desc, ":"), func(t *testing.T) {
opts := tt.opts
opts := tc.opts
fs := flag.NewFlagSet("", flag.ContinueOnError)
AddFlags(fs, &opts)
require.NoError(t, fs.Parse(tt.args))
require.NoError(t, fs.Parse(tc.args))
require.Len(t, fs.Args(), 0)
if tt.pre != nil {
tt.pre()
}
// Then create a builder with the flags.
patchLoadOptsShims(&opts)
b, err := newBuilder(opts)
require.NoError(t, err)
// read the source fragments
for i, data := range srcs {
for i, data := range tc.source(format) {
b.Sources = append(b.Sources, FileSource{
Name: fmt.Sprintf("src-%d.%s", i, format),
Format: format,
@ -4910,19 +4893,20 @@ func testConfig(t *testing.T, tests []testCase, dataDir string) {
})
}
// build/merge the config fragments
actual, err := b.BuildAndValidate()
switch {
case err == nil && tt.err != "":
t.Fatalf("got nil want error to contain %q", tt.err)
case err != nil && tt.err == "":
case err == nil && tc.err != "":
t.Fatalf("got nil want error to contain %q", tc.err)
case err != nil && tc.err == "":
t.Fatalf("got error %s want nil", err)
case err != nil && tt.err != "" && !strings.Contains(err.Error(), tt.err):
t.Fatalf("error %q does not contain %q", err.Error(), tt.err)
case err != nil && tc.err != "" && !strings.Contains(err.Error(), tc.err):
t.Fatalf("error %q does not contain %q", err.Error(), tc.err)
}
if tt.err != "" {
if tc.err != "" {
return
}
require.Equal(t, tt.warns, b.Warnings, "warnings")
require.Equal(t, tc.warns, b.Warnings, "warnings")
// build a default configuration, then patch the fields we expect to change
// and compare it with the generated configuration. Since the expected
@ -4934,8 +4918,8 @@ func testConfig(t *testing.T, tests []testCase, dataDir string) {
expected, err := x.Build()
require.NoError(t, err)
if tt.patch != nil {
tt.patch(&expected)
if tc.patch != nil {
tc.patch(&expected)
}
// both DataDir fields should always be the same, so test for the
@ -4947,8 +4931,6 @@ func testConfig(t *testing.T, tests []testCase, dataDir string) {
assertDeepEqual(t, expected, actual, cmpopts.EquateEmpty())
})
}
}
}
func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) {
t.Helper()

View File

@ -50,5 +50,9 @@ func TestSegments(t *testing.T) {
},
}
testConfig(t, tests, dataDir)
for _, tt := range tests {
for _, format := range []string{"json", "hcl"} {
testConfig(t, tt, format, dataDir)
}
}
}