qerr: move away from Ginkgo (#4649)

This commit is contained in:
Marten Seemann
2024-09-11 15:50:06 +08:00
committed by GitHub
parent 72be861aa2
commit a21f6c1c41
3 changed files with 145 additions and 164 deletions

View File

@@ -7,46 +7,41 @@ import (
"path"
"runtime"
"strconv"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/require"
)
var _ = Describe("error codes", func() {
// If this test breaks, you should run `go generate ./...`
It("has a string representation for every error code", func() {
// We parse the error code file, extract all constants, and verify that
// each of them has a string version. Go FTW!
_, thisfile, _, ok := runtime.Caller(0)
if !ok {
panic("Failed to get current frame")
}
filename := path.Join(path.Dir(thisfile), "error_codes.go")
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
Expect(err).NotTo(HaveOccurred())
constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
for _, c := range constSpecs {
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.ParseInt(valString, 0, 64)
Expect(err).NotTo(HaveOccurred())
Expect(TransportErrorCode(val).String()).ToNot(Equal("unknown error code"))
}
})
func TestTransportErrorStringer(t *testing.T) {
_, thisfile, _, ok := runtime.Caller(0)
require.True(t, ok, "Failed to get current frame")
It("has a string representation for unknown error codes", func() {
Expect(TransportErrorCode(0x1337).String()).To(Equal("unknown error code: 0x1337"))
})
filename := path.Join(path.Dir(thisfile), "error_codes.go")
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
require.NoError(t, err)
It("says if an error is a crypto error", func() {
for i := 0; i < 0x100; i++ {
Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse())
}
for i := 0x100; i < 0x200; i++ {
Expect(TransportErrorCode(i).IsCryptoError()).To(BeTrue())
}
for i := 0x200; i < 0x300; i++ {
Expect(TransportErrorCode(i).IsCryptoError()).To(BeFalse())
}
})
})
constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
require.Greater(t, len(constSpecs), 4, "Expected more than 4 constants")
for _, c := range constSpecs {
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.ParseInt(valString, 0, 64)
require.NoError(t, err)
require.NotEqual(t, "unknown error code", TransportErrorCode(val).String())
}
// test that there's a string representation for unknown error codes
require.Equal(t, "unknown error code: 0x1337", TransportErrorCode(0x1337).String())
}
func TestIsCryptoError(t *testing.T) {
for i := 0; i < 0x100; i++ {
require.False(t, TransportErrorCode(i).IsCryptoError())
}
for i := 0x100; i < 0x200; i++ {
require.True(t, TransportErrorCode(i).IsCryptoError())
}
for i := 0x200; i < 0x300; i++ {
require.False(t, TransportErrorCode(i).IsCryptoError())
}
}