golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.3k stars 610 forks source link

Mocking net/http RoundTripper with mockgen outputs an error #339

Closed preslavmihaylov closed 4 years ago

preslavmihaylov commented 4 years ago

How to reproduce:

  1. Go to any project with go modules
  2. Run mockgen -destination=mockhttp.go -package=mockhttp net/http RoundTripper

Detailed steps:

# reinstall latest mockgen version
rm -rf $GOPATH/bin/mockgen
go install github.com/golang/mock/mockgen

# make new go modules project
cd playground
go mod init github.com/preslavmihaylov/playground

# run mockgen
mockgen -destination=mockhttp.go -package=mockhttp net/http RoundTripper

Result:

 build command-line-arguments: cannot load github.com/golang/mock/mockgen/model: open /usr/local/Cellar/go/1.13/libexec/src/github.com/golang/mock/mockgen/model: no such file or directory

The output file is still generated and looks correct, though.

mockhttp.go

``` // Code generated by MockGen. DO NOT EDIT. // Source: net/http (interfaces: RoundTripper) // Package mockhttp is a generated GoMock package. package mockhttp import ( gomock "github.com/golang/mock/gomock" http "net/http" reflect "reflect" ) // MockRoundTripper is a mock of RoundTripper interface type MockRoundTripper struct { ctrl *gomock.Controller recorder *MockRoundTripperMockRecorder } // MockRoundTripperMockRecorder is the mock recorder for MockRoundTripper type MockRoundTripperMockRecorder struct { mock *MockRoundTripper } // NewMockRoundTripper creates a new mock instance func NewMockRoundTripper(ctrl *gomock.Controller) *MockRoundTripper { mock := &MockRoundTripper{ctrl: ctrl} mock.recorder = &MockRoundTripperMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use func (m *MockRoundTripper) EXPECT() *MockRoundTripperMockRecorder { return m.recorder } // RoundTrip mocks base method func (m *MockRoundTripper) RoundTrip(arg0 *http.Request) (*http.Response, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "RoundTrip", arg0) ret0, _ := ret[0].(*http.Response) ret1, _ := ret[1].(error) return ret0, ret1 } // RoundTrip indicates an expected call of RoundTrip func (mr *MockRoundTripperMockRecorder) RoundTrip(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RoundTrip", reflect.TypeOf((*MockRoundTripper)(nil).RoundTrip), arg0) } ```

If the executed command is:

GO111MODULE=off mockgen -destination=mockhttp.go -package=mockhttp net/http RoundTripper

There is no error and the output file is the same

Reproduced with latest released v1.3.1

codyoss commented 4 years ago

@preslavmihaylov I just tried to reproduce this in https://github.com/codyoss/bar No error for me. Can you provide more details?

preslavmihaylov commented 4 years ago
# reinstall latest mockgen version
rm -rf $GOPATH/bin/mockgen
go install github.com/golang/mock/mockgen

# make new go modules project
cd playground
go mod init github.com/preslavmihaylov/playground

# run mockgen
mockgen -destination=mockhttp.go -package=mockhttp net/http RoundTripper
codyoss commented 4 years ago

@preslavmihaylov I also tried your steps and no error for me. Just worked. Can you give me your go version and go env

preslavmihaylov commented 4 years ago

go version:

go version go1.13 darwin/amd64

go env:

go env output ``` GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/pmihaylov/Library/Caches/go-build" GOENV="/Users/pmihaylov/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/pmihaylov/programming/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/Cellar/go/1.13/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.13/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/pmihaylov/programming/playground/go/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9v/vvj1r6_54pb855bvmtqv3wn80000gn/T/go-build171341806=/tmp/go-build -gno-record-gcc-switches -fno-common" ```

Unfortunately, mockgen doesn't output any version information, but I ran the commands listed in the issue description.

codyoss commented 4 years ago

I am sorry I am still on able to reproduce this. I will leave it open though to see if anyone else can. Only thing I can think of is if the version of mockgen is actually older and not actually located in $GOPATH/bin/mockgen. If you do a which mockgen is it actually found at $GOPATH/bin/mockgen? You could also try got get -u to make sure you are pulling in the latest. I wonder if install will just re-pull the old installed version in module land. The module wiki suggests a global mod to keep track of installed tools and versions.

preslavmihaylov commented 4 years ago

To make sure I'm using latest version I tried git cloning `https://github.com/golang/mock', and manually building mockgen from there:

go build ./mockgen/mockgen.go ./mockgen/parse.go ./mockgen/reflect.go

It still outputs the same error.

In the error, there is a line open /usr/local/Cellar/go/1.13/libexec....

Perhaps this has something to do with go being installed via brew or Mac installer?

codyoss commented 4 years ago

Maybe, my installation is from golang.org/dl. If I clone master, use the same build command, it works for me.

codyoss commented 4 years ago

I was able to reproduce this after I installed go via brew... will try to look in further later. I assume so env var needs to be properly set to make this all happy, but I am not sure.

codyoss commented 4 years ago

My best advice here is to install Go from https://golang.org/dl/ instead on Homebrew. This seems like an issue related to how Homebrew links some dirs on install or improperly setting environment variables. After I reinstalled from the link above everything just works. Closing as I don't think this is a problem with the actual project.