Closed jamie-digital closed 5 years ago
Thank you for raising this issue. Sadly it’s not feasible to fix this. If you want to run the tests of this project do so in the context of this repo.
I get the some error in GitHub actions CI. Have you found any solutions?
@zmrenwu what did you do? What did you see when you did it?
Everything works fine on local enviroment, but can not run go test in GitHub actions CI.
My GitHub action config:
name: Build and test Go
on: [ push, pull_request ]
jobs:
test:
strategy:
matrix:
go-version: [ 1.15.x ]
os: [ ubuntu-latest ]
mongodb-version: [4.2]
runs-on: ${{ matrix.os }}
steps:
- name: setup env
shell: bash
run: |
echo "::set-env name=GOPATH::${{ github.workspace }}/go"
echo "::add-path::${{ github.workspace }}/go/bin"
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.3.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Build
env:
GOPROXY: "https://proxy.golang.org"
run: go build .
- name: Test
run: go test ./...
Some GitHub action logs:
ok github.com/klauspost/compress/zstd 30.515s
ok github.com/klauspost/compress/zstd/internal/xxhash 0.016s
--- FAIL: TestFormatNew (0.00s)
format_test.go:38: test 3: line 3: fmt.Sprintf("%+v", err):
got: "error\ngithub.com/pkg/errors.TestFormatNew\n\t/home/runner/work/coinsmart-api/coinsmart-api/go/pkg/mod/github.com/pkg/errors@v0.9.1/format_test.go:26\ntesting.tRunner\n\t/opt/hostedtoolcache/go/1.15.0/x64/src/testing/testing.go:1108\nruntime.goexit\n\t/opt/hostedtoolcache/go/1.15.0/x64/src/runtime/asm_amd64.s:1374"
want: "error\ngithub.com/pkg/errors.TestFormatNew\n\t.+/github.com/pkg/errors/format_test.go:26"
--- FAIL: TestFormatErrorf (0.00s)
format_test.go:64: test 3: line 3: fmt.Sprintf("%+v", err):
got: "error\ngithub.com/pkg/errors.TestFormatErrorf\n\t/home/runner/work/coinsmart-api/coinsmart-api/go/pkg/mod/github.com/pkg/errors@v0.9.1/format_test.go:56\ntesting.tRunner\n\t/opt/hostedtoolcache/go/1.15.0/x64/src/testing/testing.go:1108\nruntime.goexit\n\t/opt/hostedtoolcache/go/1.15.0/x64/src/runtime/asm_amd64.s:1374"
want: "error\ngithub.com/pkg/errors.TestFormatErrorf\n\t.+/github.com/pkg/errors/format_test.go:56"
Thanks for helps!
Thanks. I'm not sure what your test runner is doing but the resolution of this issue was -- do not test modules, only test your code. Have you vendored or copied the code for pkg/errors into your codebase?
No, I don't directly import any code of pkg/errors. go test
command run all tests include direct and indirect modules. But I don't know how to just test my code. I am a go beginner. In python, only test project code is a default behavior. But I don't know how to active this behavior in golang.
I'm sorry I'm not sure how to help. go test should only test the code in your module, not the tests in another module as that could lead to arbitrary code execution.
@pkg/errors-maintainers do you have any suggestions.
I was able to recreate the error situation. The issue causing the trouble is here: ::set-env name=GOPATH::${{ github.workspace }}/go
This is setting your $GOPATH
equal to a subdirectory of the working directory. While go test ./...
knows to skip vendored packages, it does not know to skip the whole $GOPATH
if it is rooted under the current working directory.
Given:
-- main.go --
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
fmt.Printf("%+v\n", errors.New("oh noes!"))
}
-- go.mod --
module github.com/puellanivis/gopath-in-working-dir
go 1.14
require github.com/pkg/errors v0.9.1
Followed by:
gopath-in-working-dir$ export GOPATH="${PWD}/go"
gopath-in-working-dir$ go build # to setup the GOPATH and download the module
go: downloading github.com/pkg/errors v0.9.1
gopath-in-working-dir$ go test ./... # result is a dump of the same kind of test failures as above
The answer is: do not put your GOPATH
into a sub-directory of your project. Given echo "::set-env name=GOPATH::${{ github.workspace }}/go"
then actually checkout the source code into ${{ github.workspace }}/src
or some other directory parallel to your GOPATH.
Go version
go version go1.13 darwin/amd64
Module version
v0.8.1
What went wrong?
The following tests fail when the module is tested as a dependency:
TestStackTraceFormat
TestStackTrace
TestFrameFormat
TestFormatGeneric
TestFormatWithMessage
TestFormatWithStack
TestFormatWrapf
TestFormatWrap
TestFormatErrorf
TestFormatNew
Steps to reproduce
Output:
Cause
When Go modules' tests are run as a dependency (from outside their codebase), they execute in the module cache, which includes the module version in the filepath. This breaks several tests in this package, which expects the package filepath not to include a version.