Repository: openconfig/ygot
Pull request #857 (2023-06-02 20:16:26)

Remove p_test variable dependencies on package p

coveralls: 
[![Coverage Status](https://coveralls.io/builds/60498411/badge)](https://coveralls.io/builds/60498411)

coverage: 89.838%. remained the same when pulling **a8429667edf8fd806ff25b3a51ccabdf787c19c2 on remove-hfam** into **8ff873b90cb0ac5014e25198965a5e7c6211daec on master**.


Patch:
diff --git a/go.mod b/go.mod
index 0db2fae..f7db183 100644
--- a/go.mod
+++ b/go.mod
@@ -7,6 +7,7 @@ require (
 	github.com/golang/glog v1.1.0
 	github.com/golang/protobuf v1.5.3
 	github.com/google/go-cmp v0.5.9
+	github.com/kr/pretty v0.3.0
 	github.com/kylelemons/godebug v1.1.0
 	github.com/openconfig/gnmi v0.9.1
 	github.com/openconfig/goyang v1.4.0
@@ -22,9 +23,11 @@ require (
 	github.com/fsnotify/fsnotify v1.6.0 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
+	github.com/kr/text v0.2.0 // indirect
 	github.com/magiconair/properties v1.8.7 // indirect
 	github.com/mitchellh/mapstructure v1.5.0 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
+	github.com/rogpeppe/go-internal v1.6.1 // indirect
 	github.com/spf13/afero v1.9.3 // indirect
 	github.com/spf13/cast v1.5.0 // indirect
 	github.com/spf13/jwalterweatherman v1.1.0 // indirect
diff --git a/go.sum b/go.sum
index 1c4b0e9..56003dd 100644
--- a/go.sum
+++ b/go.sum
@@ -48,6 +48,7 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
 github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
 github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
 github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -144,9 +145,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
 github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
+github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
 github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@@ -178,6 +181,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
 github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
+github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
 github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=


Test patch:
diff --git a/ygot/struct_validation_map_exported_test.go b/ygot/struct_validation_map_exported_test.go
index cb2e91b..e77898a 100644
--- a/ygot/struct_validation_map_exported_test.go
+++ b/ygot/struct_validation_map_exported_test.go
@@ -28,6 +28,12 @@ import (
 	"github.com/openconfig/ygot/ygot"
 )
 
+const (
+	// TestRoot is the path to the directory within which the test runs, appended
+	// to any filename that is to be loaded.
+	TestRoot string = ""
+)
+
 // errToString returns an error as a string.
 func errToString(err error) string {
 	if err == nil {
@@ -180,7 +186,7 @@ func TestEmitJSON(t *testing.T) {
 				FieldTwo: ygot.Uint32(42),
 			},
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson_1.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_1.json-txt"),
 	}, {
 		name: "simple schema JSON output with safe HTML",
 		inStruct: &ctestschema.MapStructTestOne{
@@ -192,7 +198,7 @@ func TestEmitJSON(t *testing.T) {
 		inConfig: &ygot.EmitJSONConfig{
 			EscapeHTML: true,
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson_1_html_safe.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_1_html_safe.json-txt"),
 	}, {
 		name: "schema with a list JSON output",
 		inStruct: &mapStructTestFour{
@@ -202,7 +208,7 @@ func TestEmitJSON(t *testing.T) {
 				},
 			},
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson_2.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_2.json-txt"),
 	}, {
 		name: "simple schema IETF JSON output",
 		inStruct: &ctestschema.MapStructTestOne{
@@ -219,7 +225,7 @@ func TestEmitJSON(t *testing.T) {
 			},
 			Indent: "  ",
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson1_ietf.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson1_ietf.json-txt"),
 	}, {
 		name: "schema with list and enum IETF JSON",
 		inStruct: &mapStructTestFour{
@@ -240,13 +246,13 @@ func TestEmitJSON(t *testing.T) {
 			},
 			Indent: "  ",
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson2_ietf.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson2_ietf.json-txt"),
 	}, {
 		name: "schema with container around a ordered list JSON output",
 		inStruct: &ctestschema.MapStructTestOne{
 			OrderedList: ctestschema.GetOrderedMapLonger(t),
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata/emitjson_orderedmap_container_internal.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata/emitjson_orderedmap_container_internal.json-txt"),
 	}, {
 		name:     "invalid struct contents",
 		inStruct: &mapStructInvalid{Name: ygot.String("aardvark")},
@@ -257,7 +263,7 @@ func TestEmitJSON(t *testing.T) {
 		inConfig: &ygot.EmitJSONConfig{
 			SkipValidation: true,
 		},
-		wantJSONPath: filepath.Join(ygot.TestRoot, "testdata", "invalid-struct.json-txt"),
+		wantJSONPath: filepath.Join(TestRoot, "testdata", "invalid-struct.json-txt"),
 	}, {
 		name:     "invalid internal JSON",
 		inStruct: &mapStructNoPaths{Name: ygot.String("honey badger")},
diff --git a/ygot/struct_validation_map_test.go b/ygot/struct_validation_map_test.go
index 03c986d..75509f4 100644
--- a/ygot/struct_validation_map_test.go
+++ b/ygot/struct_validation_map_test.go
@@ -29,12 +29,6 @@ import (
 	"github.com/openconfig/ygot/testutil"
 )
 
-const (
-	// TestRoot is the path to the directory within which the test runs, appended
-	// to any filename that is to be loaded.
-	TestRoot string = ""
-)
-
 var (
 	testBinary1 = testutil.Binary("abc")
 	testBinary2 = testutil.Binary("def")
diff --git a/ytypes/list_exported_test.go b/ytypes/list_exported_test.go
index bcd2a6b..ce736cc 100644
--- a/ytypes/list_exported_test.go
+++ b/ytypes/list_exported_test.go
@@ -19,7 +19,7 @@ import (
 	"testing"
 
 	"github.com/google/go-cmp/cmp"
-	"github.com/h-fam/errdiff"
+	"github.com/openconfig/gnmi/errdiff"
 	"github.com/openconfig/goyang/pkg/yang"
 	"github.com/openconfig/ygot/integration_tests/schemaops/ctestschema"
 	"github.com/openconfig/ygot/ygot"
@@ -264,36 +264,7 @@ func TestUnmarshalKeyedList(t *testing.T) {
 	}
 }
 
-func TestUnmarshalSingleListElement(t *testing.T) {
-	listSchema := &yang.Entry{
-		Name:     "struct-list",
-		Kind:     yang.DirectoryEntry,
-		ListAttr: yang.NewDefaultListAttr(),
-		Dir: map[string]*yang.Entry{
-			"leaf-field": {
-				Kind: yang.LeafEntry,
-				Name: "leaf-field",
-				Type: &yang.YangType{Kind: yang.Yint32},
-			},
-			"enum-leaf-field": {
-				Kind: yang.LeafEntry,
-				Name: "enum-leaf-field",
-				Type: &yang.YangType{Kind: yang.Yenum},
-			},
-			"leaf2-field": {
-				Kind: yang.LeafEntry,
-				Name: "leaf2-field",
-				Type: &yang.YangType{Kind: yang.Yint64},
-			},
-		},
-	}
-
-	type ListElemStruct struct {
-		LeafName  *int32          `path:"leaf-field"`
-		EnumLeaf  ytypes.EnumType `path:"enum-leaf-field"`
-		Leaf2Name *int64          `path:"leaf2-field"`
-	}
-
+func TestUnmarshalSingleListElementOrderedMap(t *testing.T) {
 	tests := []struct {
 		desc    string
 		schema  *yang.Entry
@@ -302,26 +273,6 @@ func TestUnmarshalSingleListElement(t *testing.T) {
 		want    any
 		wantErr string
 	}{
-		{
-			desc:   "success",
-			schema: listSchema,
-			json:   `{ "leaf-field" : 42, "enum-leaf-field" : "E_VALUE_FORTY_TWO"}`,
-			parent: &ListElemStruct{
-				Leaf2Name: ygot.Int64(42),
-			},
-			want: &ListElemStruct{
-				LeafName:  ygot.Int32(42),
-				Leaf2Name: ygot.Int64(42),
-				EnumLeaf:  42,
-			},
-		},
-		{
-			desc:    "bad field",
-			schema:  listSchema,
-			json:    `{ "leaf-field" : 42, "bad-field" : "E_VALUE_FORTY_TWO"}`,
-			parent:  &ListElemStruct{},
-			wantErr: `parent container struct-list (type *ytypes_test.ListElemStruct): JSON contains unexpected field bad-field`,
-		},
 		{
 			desc:   "success with ordered map -- this should be the same as a regular map object",
 			json:   `{ "key" : "foo", "config": { "value" : "foo-val"} }`,
diff --git a/ytypes/list_test.go b/ytypes/list_test.go
index 40b2c1c..97a471b 100644
--- a/ytypes/list_test.go
+++ b/ytypes/list_test.go
@@ -524,6 +524,87 @@ func TestUnmarshalUnkeyedList(t *testing.T) {
 	}
 }
 
+func TestUnmarshalSingleListElement(t *testing.T) {
+	listSchema := &yang.Entry{
+		Name:     "struct-list",
+		Kind:     yang.DirectoryEntry,
+		ListAttr: yang.NewDefaultListAttr(),
+		Dir: map[string]*yang.Entry{
+			"leaf-field": {
+				Kind: yang.LeafEntry,
+				Name: "leaf-field",
+				Type: &yang.YangType{Kind: yang.Yint32},
+			},
+			"enum-leaf-field": {
+				Kind: yang.LeafEntry,
+				Name: "enum-leaf-field",
+				Type: &yang.YangType{Kind: yang.Yenum},
+			},
+			"leaf2-field": {
+				Kind: yang.LeafEntry,
+				Name: "leaf2-field",
+				Type: &yang.YangType{Kind: yang.Yint64},
+			},
+		},
+	}
+
+	type ListElemStruct struct {
+		LeafName  *int32   `path:"leaf-field"`
+		EnumLeaf  EnumType `path:"enum-leaf-field"`
+		Leaf2Name *int64   `path:"leaf2-field"`
+	}
+
+	tests := []struct {
+		desc    string
+		schema  *yang.Entry
+		json    string
+		parent  any
+		want    any
+		wantErr string
+	}{
+		{
+			desc:   "success",
+			schema: listSchema,
+			json:   `{ "leaf-field" : 42, "enum-leaf-field" : "E_VALUE_FORTY_TWO"}`,
+			parent: &ListElemStruct{
+				Leaf2Name: ygot.Int64(42),
+			},
+			want: &ListElemStruct{
+				LeafName:  ygot.Int32(42),
+				Leaf2Name: ygot.Int64(42),
+				EnumLeaf:  42,
+			},
+		},
+		{
+			desc:    "bad field",
+			schema:  listSchema,
+			json:    `{ "leaf-field" : 42, "bad-field" : "E_VALUE_FORTY_TWO"}`,
+			parent:  &ListElemStruct{},
+			wantErr: `parent container struct-list (type *ytypes.ListElemStruct): JSON contains unexpected field bad-field`,
+		},
+	}
+
+	var jsonTree interface{}
+	for _, tt := range tests {
+		t.Run(tt.desc, func(t *testing.T) {
+			if err := json.Unmarshal([]byte(tt.json), &jsonTree); err != nil {
+				t.Fatalf("%s : %s", tt.desc, err)
+			}
+
+			err := Unmarshal(tt.schema, tt.parent, jsonTree)
+			if diff := errdiff.Text(err, tt.wantErr); diff != "" {
+				t.Fatalf("%s: Unmarshal error not expected:\n%s", tt.desc, diff)
+			}
+			if err == nil {
+				got, want := tt.parent, tt.want
+				if diff := cmp.Diff(want, got); diff != "" {
+					t.Errorf("%s: Unmarshal (-want, +got):\n%s", tt.desc, diff)
+				}
+			}
+		})
+	}
+}
+
 func TestUnmarshalStructKeyedList(t *testing.T) {
 	containerWithLeafListSchema := &yang.Entry{
 		Name: "container",