Skip to content

Allow version-tagged filepath in tests#165

Closed
thsnr wants to merge 1 commit into
pkg:masterfrom
thsnr:go-mod-tests
Closed

Allow version-tagged filepath in tests#165
thsnr wants to merge 1 commit into
pkg:masterfrom
thsnr:go-mod-tests

Conversation

@thsnr
Copy link
Copy Markdown

@thsnr thsnr commented Aug 17, 2018

Go 1.11 will introduce support for versioned modules. When downloading module dependencies, go get will add a version tag to the directory where the package is checked out (e.g., github.com/pkg/errors@v0.8.0).

It is also recommended as a best practice to run go test all before release, which will run tests for the package being released and all dependencies to check compatibility. Currently this fails because the tests are not happy to be checked out in a version-tagged directory.

$ cat go.mod 
module github.com/thsnr/gomod

require github.com/pkg/errors v0.0.0-00000000000000-816c9085562cd7ee03e7f8188a1cfd942858cded

$ go1.11rc1 test all
?   	github.com/thsnr/gomod	[no test files]
...
--- FAIL: TestFrameFormat (0.00s)
    format_test.go:379: test 2: line 2: fmt.Sprintf("%+s", err):
         got: "github.com/pkg/errors.init\n\t/home/tiit/go/pkg/mod/github.com/pkg/errors@v0.0.0-00000000000000-816c9085562cd7ee03e7f8188a1cfd942858cded/stack_test.go"
        want: "github.com/pkg/errors.init\n\t.+/github.com/pkg/errors/stack_test.go"
    format_test.go:379: test 12: line 2: fmt.Sprintf("%+v", err):
         got: "github.com/pkg/errors.init\n\t/home/tiit/go/pkg/mod/github.com/pkg/errors@v0.0.0-00000000000000-816c9085562cd7ee03e7f8188a1cfd942858cded/stack_test.go:9"
        want: "github.com/pkg/errors.init\n\t.+/github.com/pkg/errors/stack_test.go:9"
...

This pull request simply changes the tests to allow a version tag. I purposefully left the version regular expression vague (@v[^/]+) to keep it short – these tests should not concern themselves with correctness of the tag – and inlined it to avoid changing any line numbers.

This kind of tag is added when the package is checked out by
module-aware Go.
@davecheney
Copy link
Copy Markdown
Member

davecheney commented Aug 18, 2018 via email

@ChrisHines
Copy link
Copy Markdown
Contributor

ChrisHines commented Aug 18, 2018

FWIW, I was just playing with adding module support to github.com/go-stack/stack and encountered the same problem with the tests in that package. In particular, if I work in module mode outside GOPATH with go1.11rc1 then the logic to trim the compile time GOPATH from source file paths no longer works because there is no compile time GOPATH any more. That breaks the %+s and %+v formats.

I don't have a great solution yet.

@davecheney
Copy link
Copy Markdown
Member

Thanks Chris, I had a suspicion that this was a symptom of a larger problem.

@thsnr I am going to close this PR as I don't think it is the right approach. I would appreciate it if someone would open an issue about the path trimming not working outside GOPATH.

Thank you.

@thsnr
Copy link
Copy Markdown
Author

thsnr commented Aug 18, 2018

Ah yes, I forgot to mention that the tests fail only when operating in "module-aware mode" as opposed to "GOPATH mode".

@davecheney I do not think I fully understand the GOPATH trimming problem. While yes, github.com/go-stack/stack does trim the GOPATH from filenames before printing, I see no such behavior here in github.com/pkg/errors (although promised in https://github.com/pkg/errors/blob/master/stack.go#L49).

I even ran a small test-case:

package main

import (
        "fmt"

        "github.com/pkg/errors"
)

type stackTracer interface {
        StackTrace() errors.StackTrace
}

func main() {
        err := errors.New("stack")
        fmt.Printf("%+s\n\n", err.(stackTracer).StackTrace()[0])
        fmt.Printf("%+v\n", errors.New("stack"))
}

Result in "module-aware mode":

main.main
        /tmp/gomod/main.go

stack
main.main
        /tmp/gomod/main.go:16
runtime.main
        /home/tiit/sdk/go1.11rc1/src/runtime/proc.go:201
runtime.goexit
        /home/tiit/sdk/go1.11rc1/src/runtime/asm_amd64.s:1333

Result in "GOPATH mode":

main.main
        /tmp/gopath/src/github.com/thsnr/stack/main.go

stack
main.main
        /tmp/gopath/src/github.com/thsnr/stack/main.go:16
runtime.main
        /home/tiit/sdk/go1.11rc1/src/runtime/proc.go:201
runtime.goexit
        /home/tiit/sdk/go1.11rc1/src/runtime/asm_amd64.s:1333

(Output when using Go 1.10 was same as GOPATH mode, just with a different GOROOT.)

No GOPATH trimming is happening, it simply uses the output of Func.FileLine.

If I could understand the situation better, I would be happy to open a new issue.

@davecheney
Copy link
Copy Markdown
Member

@thsnr sorry I forgot to close the pull request. I am doing so now.

Please lets continue this question on an issue. We don't do design and bug traige in pull requests. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants