From a287f028d1eb0d0a4da5bd6594990f4ac761362e Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 7 Jul 2018 12:20:23 -0400 Subject: [PATCH] lang: Detect sub tests with the same name This detects identically named tests and fails the test in such a scenario to prevent confusion. --- lang/interpolate_test.go | 19 +++++++++++++++++++ lang/interpret_test.go | 13 +++++++++++++ lang/lang_test.go | 7 +++++++ lang/lexparse_test.go | 9 ++++++++- lang/unification_test.go | 7 +++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lang/interpolate_test.go b/lang/interpolate_test.go index 9dc0cbe7..2a5587b3 100644 --- a/lang/interpolate_test.go +++ b/lang/interpolate_test.go @@ -26,6 +26,7 @@ import ( "testing" "github.com/purpleidea/mgmt/lang/interfaces" + "github.com/purpleidea/mgmt/util" "github.com/davecgh/go-spew/spew" "github.com/kylelemons/godebug/pretty" @@ -127,7 +128,13 @@ func TestInterpolate0(t *testing.T) { }) } + names := []string{} for index, tc := range testCases { // run all the tests + if util.StrInList(tc.name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, tc.name) + continue + } + names = append(names, tc.name) t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) { code, fail, exp := tc.code, tc.fail, tc.ast @@ -366,7 +373,13 @@ func TestInterpolateBasicStmt(t *testing.T) { }) } + names := []string{} for index, tc := range testCases { // run all the tests + if util.StrInList(tc.name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, tc.name) + continue + } + names = append(names, tc.name) t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) { ast, fail, exp := tc.ast, tc.fail, tc.exp @@ -681,7 +694,13 @@ func TestInterpolateBasicExpr(t *testing.T) { // }) //} + names := []string{} for index, tc := range testCases { // run all the tests + if util.StrInList(tc.name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, tc.name) + continue + } + names = append(names, tc.name) t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) { ast, fail, exp := tc.ast, tc.fail, tc.exp diff --git a/lang/interpret_test.go b/lang/interpret_test.go index c21bb0f3..a5d4b8a5 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -29,6 +29,7 @@ import ( "github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/unification" "github.com/purpleidea/mgmt/pgraph" + "github.com/purpleidea/mgmt/util" ) func vertexAstCmpFn(v1, v2 pgraph.Vertex) (bool, error) { @@ -365,12 +366,18 @@ func TestAstFunc0(t *testing.T) { }) } + names := []string{} for index, test := range values { // run all the tests name, code, fail, scope, exp := test.name, test.code, test.fail, test.scope, test.graph if name == "" { name = "" } + if util.StrInList(name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, name) + continue + } + names = append(names, name) //if index != 3 { // hack to run a subset (useful for debugging) //if test.name != "simple operators" { @@ -591,12 +598,18 @@ func TestAstInterpret0(t *testing.T) { }) } + names := []string{} for index, test := range values { // run all the tests name, code, fail, exp := test.name, test.code, test.fail, test.graph if name == "" { name = "" } + if util.StrInList(name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, name) + continue + } + names = append(names, name) //if index != 3 { // hack to run a subset (useful for debugging) //if test.name != "nil" { diff --git a/lang/lang_test.go b/lang/lang_test.go index fcd31442..49ddf6f0 100644 --- a/lang/lang_test.go +++ b/lang/lang_test.go @@ -28,6 +28,7 @@ import ( "github.com/purpleidea/mgmt/engine/resources" _ "github.com/purpleidea/mgmt/lang/funcs/facts/core" // load facts "github.com/purpleidea/mgmt/pgraph" + "github.com/purpleidea/mgmt/util" multierr "github.com/hashicorp/go-multierror" errwrap "github.com/pkg/errors" @@ -850,12 +851,18 @@ func TestInterpretMany(t *testing.T) { }) } + names := []string{} for index, test := range values { // run all the tests name, code, fail, exp := test.name, test.code, test.fail, test.graph if name == "" { name = "" } + if util.StrInList(name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, name) + continue + } + names = append(names, name) //if index != 3 { // hack to run a subset (useful for debugging) //if test.name != "nil" { diff --git a/lang/lexparse_test.go b/lang/lexparse_test.go index 3aae144e..7ff9fafe 100644 --- a/lang/lexparse_test.go +++ b/lang/lexparse_test.go @@ -26,6 +26,7 @@ import ( "github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/types" + "github.com/purpleidea/mgmt/util" "github.com/davecgh/go-spew/spew" ) @@ -115,7 +116,7 @@ func TestLexParse0(t *testing.T) { } { values = append(values, test{ - name: "one res", + name: "one res with param", code: ` test "t1" { int16 => 01134, # some comment @@ -1412,12 +1413,18 @@ func TestLexParse0(t *testing.T) { }) } + names := []string{} for index, test := range values { // run all the tests name, code, fail, exp := test.name, test.code, test.fail, test.exp if name == "" { name = "" } + if util.StrInList(name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, name) + continue + } + names = append(names, name) //if index != 3 { // hack to run a subset (useful for debugging) //if (index != 20 && index != 21) { diff --git a/lang/unification_test.go b/lang/unification_test.go index 6381319f..935d02f1 100644 --- a/lang/unification_test.go +++ b/lang/unification_test.go @@ -26,6 +26,7 @@ import ( "github.com/purpleidea/mgmt/lang/interfaces" "github.com/purpleidea/mgmt/lang/types" "github.com/purpleidea/mgmt/lang/unification" + "github.com/purpleidea/mgmt/util" ) func TestUnification1(t *testing.T) { @@ -511,7 +512,13 @@ func TestUnification1(t *testing.T) { }) } + names := []string{} for index, test := range values { // run all the tests + if util.StrInList(test.name, names) { + t.Errorf("test #%d: duplicate sub test name of: %s", index, test.name) + continue + } + names = append(names, test.name) t.Run(fmt.Sprintf("test #%d (%s)", index, test.name), func(t *testing.T) { ast, fail, expect := test.ast, test.fail, test.expect