From 8bb03fc09f0a0655ed4c06c80c3fd5a36c0ef537 Mon Sep 17 00:00:00 2001 From: Hamza El-Saawy Date: Tue, 29 Mar 2022 12:33:24 -0400 Subject: [PATCH] GUID tests to use t.Run Switched to subtests using `t.Run()` for GUID testing, rather than using log statements to demarcate different tests. Added `.String` to GUID `Variant` and `Version` using `golang.org/x/tools/cmd/stringer`. Added `tools.go` to version `stringer` in go.mod and allow `go generate ./...` to be run without needing a `go get` call. Based off of [go wiki](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module) recommendation. Signed-off-by: Hamza El-Saawy --- go.mod | 5 +-- go.sum | 28 +++++++++++++++-- pkg/guid/guid.go | 10 ++++-- pkg/guid/guid_test.go | 63 +++++++++++++++++++++----------------- pkg/guid/variant_string.go | 27 ++++++++++++++++ tools.go | 5 +++ 6 files changed, 103 insertions(+), 35 deletions(-) create mode 100644 pkg/guid/variant_string.go create mode 100644 tools.go diff --git a/go.mod b/go.mod index 715f4b85..8cf6b938 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.17 require ( github.com/sirupsen/logrus v1.7.0 - golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f + golang.org/x/tools v0.1.12 ) -require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect +require golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect diff --git a/go.sum b/go.sum index 7ec6d7ce..3583d96f 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,30 @@ github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/pkg/guid/guid.go b/pkg/guid/guid.go index 7e8e60b1..48ce4e92 100644 --- a/pkg/guid/guid.go +++ b/pkg/guid/guid.go @@ -14,17 +14,19 @@ import ( "strconv" ) +//go:generate go run golang.org/x/tools/cmd/stringer -type=Variant -trimprefix=Variant -linecomment + // Variant specifies which GUID variant (or "type") of the GUID. It determines // how the entirety of the rest of the GUID is interpreted. type Variant uint8 -// The variants specified by RFC 4122. +// The variants specified by RFC 4122 section 4.1.1. const ( // VariantUnknown specifies a GUID variant which does not conform to one of // the variant encodings specified in RFC 4122. VariantUnknown Variant = iota VariantNCS - VariantRFC4122 + VariantRFC4122 // RFC 4122 VariantMicrosoft VariantFuture ) @@ -34,6 +36,10 @@ const ( // hash of an input string. type Version uint8 +func (v Version) String() string { + return strconv.FormatUint(uint64(v), 10) +} + var _ = (encoding.TextMarshaler)(GUID{}) var _ = (encoding.TextUnmarshaler)(&GUID{}) diff --git a/pkg/guid/guid_test.go b/pkg/guid/guid_test.go index d7e21241..91ac0e68 100644 --- a/pkg/guid/guid_test.go +++ b/pkg/guid/guid_test.go @@ -46,27 +46,29 @@ func Test_Variant(t *testing.T) { {mustFromString(t, "f5cbc1a9-4cba-45a0-ffdd-b6761fc7dcc0"), VariantFuture}, } for _, tc := range testCases { - actualVariant := tc.g.Variant() - if actualVariant != tc.v { - t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g) - } + t.Run(tc.v.String()+"/"+tc.g.String(), func(t *testing.T) { + actualVariant := tc.g.Variant() + if actualVariant != tc.v { + t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g) + } + }) } } func Test_SetVariant(t *testing.T) { - testCases := []Variant{ - VariantNCS, - VariantRFC4122, - VariantMicrosoft, - VariantFuture, - } g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0") - for _, tc := range testCases { - t.Logf("Test case: %d", tc) - g.setVariant(tc) - if g.Variant() != tc { - t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", tc, g.Variant()) + for i := 0; i < len(_Variant_index)-1; i++ { + v := Variant(i) + if v == VariantUnknown { + // Unknown is not a valid variant + continue } + t.Run(v.String(), func(t *testing.T) { + g.setVariant(v) + if g.Variant() != v { + t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", v, g.Variant()) + } + }) } } @@ -83,22 +85,25 @@ func Test_Version(t *testing.T) { {mustFromString(t, "f5cbc1a9-4cba-55a0-0fdd-b6761fc7dcc0"), 5}, } for _, tc := range testCases { - actualVersion := tc.g.Version() - if actualVersion != tc.v { - t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g) - } + t.Run(tc.v.String()+"-"+tc.g.String(), func(t *testing.T) { + actualVersion := tc.g.Version() + if actualVersion != tc.v { + t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g) + } + }) } } func Test_SetVersion(t *testing.T) { g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0") for tc := 0; tc < 16; tc++ { - t.Logf("Test case: %d", tc) v := Version(tc) - g.setVersion(v) - if g.Version() != v { - t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version()) - } + t.Run(v.String(), func(t *testing.T) { + g.setVersion(v) + if g.Version() != v { + t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version()) + } + }) } } @@ -160,10 +165,12 @@ func Test_V5KnownValues(t *testing.T) { }, } for _, tc := range testCases { - g := mustNewV5(t, tc.ns, []byte(tc.name)) - if g != tc.g { - t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g) - } + t.Run(tc.name, func(t *testing.T) { + g := mustNewV5(t, tc.ns, []byte(tc.name)) + if g != tc.g { + t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g) + } + }) } } diff --git a/pkg/guid/variant_string.go b/pkg/guid/variant_string.go new file mode 100644 index 00000000..4076d313 --- /dev/null +++ b/pkg/guid/variant_string.go @@ -0,0 +1,27 @@ +// Code generated by "stringer -type=Variant -trimprefix=Variant -linecomment"; DO NOT EDIT. + +package guid + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[VariantUnknown-0] + _ = x[VariantNCS-1] + _ = x[VariantRFC4122-2] + _ = x[VariantMicrosoft-3] + _ = x[VariantFuture-4] +} + +const _Variant_name = "UnknownNCSRFC 4122MicrosoftFuture" + +var _Variant_index = [...]uint8{0, 7, 10, 18, 27, 33} + +func (i Variant) String() string { + if i >= Variant(len(_Variant_index)-1) { + return "Variant(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _Variant_name[_Variant_index[i]:_Variant_index[i+1]] +} diff --git a/tools.go b/tools.go new file mode 100644 index 00000000..2aa04584 --- /dev/null +++ b/tools.go @@ -0,0 +1,5 @@ +//go:build tools + +package winio + +import _ "golang.org/x/tools/cmd/stringer"