Files
quic-go/qerr/errorcodes_test.go
Carlos Martín Nieto 327856e31c Don't rely on GOPATH to load the certificates or error codes
GOPATH is a list of paths, similar to PATH. If someone does have a list
set, the tests will try to use the full list as a path prefix to load
the certificates, which won't work.

But even handling GOPATH as a list does not solve the issue of what
certificate or error_codes.go file to load, since the code might not be
under the first one it finds.

Instead, use the runtime functionality to get the filename of the path
of the project at compilation time and perform the lookups relative to
that, which guarantees that we're loading the files from the path of the
code that is running.
2016-12-02 13:52:12 +01:00

39 lines
1.1 KiB
Go

package qerr
import (
"go/ast"
"go/parser"
"go/token"
"path"
"runtime"
"strconv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
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[0].(*ast.GenDecl).Specs
Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
for _, c := range constSpecs {
name := c.(*ast.ValueSpec).Names[0].Name
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.Atoi(valString)
Expect(err).NotTo(HaveOccurred())
Expect(ErrorCode(val).String()).To(Equal(name))
}
Expect(ErrorCode(0).String()).To(Equal("ErrorCode(0)"))
})
})