Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Allow version-tagged filepath in tests #165

Closed
wants to merge 1 commit into from
Closed

Conversation

thsnr
Copy link

@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
Member

davecheney commented Aug 18, 2018 via email

@ChrisHines
Copy link
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
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
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
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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants