golang / mock

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

aux_files shouldn't be necessary in mockgen #181

Open balshetzer opened 6 years ago

balshetzer commented 6 years ago

mockgen should try to load all the files in the package in source mode so the user shouldn't need to specify aux_file.

codyoss commented 4 years ago

I think this is close being done. The test in the package no longer needs aux_files at least. I need to do a little more research to know if we can deprecate this for sure.

codyoss commented 4 years ago

I would love to hear from users here. The tests we currently have in the library that involve aux_imports no longer need the flag. If you are currently using this flag and you still need it with the current release, v1.4.1, please either comment here or open an issue. I would like to deprecate this flag in a future release and make it a no-op.

Botono commented 3 years ago

@codyoss I am currently having trouble making mocks of the Prometheus client package. I am trying to make a mock of the gauge.go file. All the files are in vendor/github.com/prometheus/client_golang/prometheus/.

I am using Mockgen v1.4.4.

gauge.go:

package prometheus

type Gauge interface {
    Metric
    Collector
}

metric.go:

package prometheus

type Metric interface {
    Desc() *Desc
}

collector.go:

package prometheus

type Collector interface {
    Desc() *Desc
}

Failed commands:

mockgen --source=vendor/github.com/prometheus/client_golang/prometheus/gauge.go --destination=mypackage/cmd/mocks/gauge.go --package=mocks
2021/02/09 07:54:13 Loading input failed: vendor/github.com/prometheus/client_golang/prometheus/gauge.go:33:2: unknown embedded interface Metric

mockgen --source=vendor/github.com/prometheus/client_golang/prometheus/gauge.go --aux_files github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/metric.go --destination=mypackage/cmd/mocks/gauge.go --package=mocks
2021/02/09 07:54:28 Loading input failed: vendor/github.com/prometheus/client_golang/prometheus/gauge.go:33:2: unknown embedded interface Metric

Frustratingly, the second command previously gave a different failure message:

mockgen --source=vendor/github.com/prometheus/client_golang/prometheus/gauge.go --aux_files github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/metric.go --destination=mypackage/cmd/mocks/gauge.go --package=mocks
2021/02/09 07:27:39 Loading input failed: vendor/github.com/prometheus/client_golang/prometheus/gauge.go:34:2: unknown embedded interface Collector

And adding the collector.go file to the --aux_files option worked!

mockgen --source=vendor/github.com/prometheus/client_golang/prometheus/gauge.go --aux_files github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/metric.go,github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/collector.go --destination=mypackage/cmd/mocks/gauge.go --package=mocks

I cannot repeat this! After thinking I was successful, I was setting up Makefile targets to refresh these mocks. After deleting all the mocks and trying the commands again, I only ever get this message from the above command:

mockgen --source=vendor/github.com/prometheus/client_golang/prometheus/gauge.go --aux_files github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/metric.go,github.com/prometheus/client_golang/prometheus=vendor/github.com/prometheus/client_golang/prometheus/collector.go --destination=mypackage/cmd/mocks/gauge.go --package=mocks
2021/02/09 08:00:24 Loading input failed: vendor/github.com/prometheus/client_golang/prometheus/gauge.go:33:2: unknown embedded interface Metric
codyoss commented 2 years ago

@Botono I believe this case is now fixed, at least on HEAD. I believe all cases were aux_files was needed should now be gone. Unless I hear from users others I will deprecate this flag in the next release and make it a noop in the future.

intoxicated commented 1 year ago

I'm struggling to resolve this issue..

Two interface in two different file but in same package. If I attempt to generate mock file from one interface which composite of the other, I got unknown embedded interface error. Tried to play with aux_files but no luck (not sure what would be input for this package=source.go ? but its in same package)

I'm using 1.6.0 btw

mpyw commented 1 year ago

Exactly the same situation as @intoxicated ...

codyoss commented 1 year ago

@intoxicated @mpyw This is fixed on HEAD. Please try with v1.7.0-rc.1 and let me know if you still have issues

aldrin-coexservices commented 1 year ago

@codyoss i am trying to mock uptrace bun IDB interface but I am having error

//go:generate mockgen -source $GOBASE/vendor/github.com/uptrace/bun/query_base.go -aux_files github.com/uptrace/bun/schema=$GOBASE/vendor/github.com/uptrace/bun/schema/hook.go,github.com/uptrace/bun=$GOBASE/vendor/github.com/uptrace/bun/bun.go -destination=$GOBASE/gen/mock/uptracde/bun/query_base.go IDB

output:

----> Generating go stuff GOPATH=/Users/xxxx/go:/Users/xxxx/IdeaProjects/ms-auth-api/vendor:/Users/xxxx/IdeaProjects/ms-auth-api GOBIN=/Users/xxxx/IdeaProjects/ms-auth-api/bin go generate ./... 2023/02/10 08:13:11 Loading input failed: /Users/xxxx/IdeaProjects/ms-auth-api/vendor/github.com/uptrace/bun/query_base.go:75:2: unknown embedded interface ms-auth-api/vendor/github.com/uptrace/bun.Query src/test/pubsub_test/user_auth_upsert_handler_test.go:27: running "mockgen": exit status 1

I am thinking the issue is because of type alias on another interface : https://github.com/uptrace/bun/blob/33503abd50f1dc349179a4470617a962f90b6948/bun.go#L16

how to fix? I am already using 1.7.0-rc.1