jmhodges / bazel_gomock

Code to create Go mocks for bazel targets using mockgen
MIT License
58 stars 30 forks source link

Add support for `aux_files` option #29

Closed josmad closed 4 years ago

josmad commented 4 years ago

Given files other.go and sample.go other.go

package mock_test

type Y interface{}

sample.go

package mock_test

type X interface {
  Y
}

type test interface {
  F(X)
}

The gomock stanza below fails with unknown embedded interface Y:

gomock(
    name = "private_mocks",
    out = "mocks.go",
    interfaces = [
        "test",
    ],
    library = ":go_default_library",
    package = "mock_test",
    source = "sample.go",
)

With this PR and modifying the stanza as below succeeds in generating the mock:

gomock(
    name = "private_mocks",
    out = "mocks.go",
    interfaces = [
        "test",
    ],
    library = ":go_default_library",
    package = "mock_test",
    source = "sample.go",
    aux_files = {
        "github.com/josmad/go_samples/mock_test": [
            "other.go",
        ],
    },
)
jmhodges commented 4 years ago

Hm, I'm looking at this again and is there no better way to get these file paths than assuming they're relative to the library's directory in the GOPATH?

josmad commented 4 years ago

I wanted to add an explicit dependency on a file within a go_library rule (or just the whole rule) to re-run mock generation if a listed aux_files changes. However, I was unsure how to do it (if it is even possible). If we can get the file paths of files in go_library rules it may be an option to change the attribute to reference go_library rules. Assuming that is possible I had two ideas: