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.
This commit is contained in:
Carlos Martín Nieto
2016-11-12 13:36:44 +01:00
parent 0d6356d9d8
commit 327856e31c
4 changed files with 39 additions and 15 deletions

View File

@@ -4,7 +4,8 @@ import (
"go/ast"
"go/parser"
"go/token"
"os"
"path"
"runtime"
"strconv"
. "github.com/onsi/ginkgo"
@@ -16,7 +17,11 @@ var _ = Describe("error codes", func() {
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!
filename := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/qerr/error_codes.go"
_, 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