derision-test / go-mockgen

MIT License
58 stars 8 forks source link

Optimistically continue on typecheck errors #48

Closed camdencheek closed 1 year ago

camdencheek commented 1 year ago

A frequent practical issue I run into when making changes is that I'll make a change to an interface, make a change to the implementor of an interface, but then go-mockgen does not successfully generate the new mock for the interface because the old mock interface does not successfully typecheck against code that uses it.

This PR makes a small change to the logic to skip typechecking errors on package load and just log a warning instead. go-mockgen does not need the package to typecheck successfully to do its thing, it only really needs the package to parse. However, it doesn't really matter if there is a struct that doesn't correctly implement the interface.

I couldn't figure out a way to add integration tests for this because, by nature of the change, the code that would be tested doesn't build, so any test files I would commit would fail to build. Here's how I manually tested it though:

Initialize a module:

go mod init github.com/camdencheek/test-mockgen

Create a file with an interface:

package main

type Doer interface {
    Do(string)
}

Generate the mock:

go-mockgen -f -p main --import-path github.com/camdencheek/test-mockgen ./ -d ./

Modify the interface so that the old mock doesn't typecheck:

package main

type Doer interface {
    Do(int)
}

Regenerate the mock, which now completes successfully even though the package fails to typecheck:

go-mockgen -f -p main --import-path github.com/camdencheek/test-mockgen ./ -d ./
sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication