Skip to content

Conversation

@kliukovkin
Copy link
Contributor

This is a proposal for -interfaces flag using source mode. Using this flag it is possible to list only required interfaces to be mocked in source mode. Currently, source mode mocks all interfaces which -source file contains.

Thanks @KastenMike for raising this!

Fixes golang/mock#660

@CLAassistant
Copy link

CLAassistant commented Aug 28, 2024

CLA assistant check
All committers have signed the CLA.

@kliukovkin
Copy link
Contributor Author

Hey @JacobOaks , can you please take a look?

@JacobOaks
Copy link
Contributor

Hey @kliukovkin! Thanks for the PR!

I am in favor of having this feature, just two questions/comments:

  • Instead of using an -interfaces flag, what do you think about having source mode read positional arguments (i.e., mockgen -source <path/to/file.go> InterfaceOne,InterfaceTwo) to align it with how reflect mode works? (If there are no positional arguments specified, we would parse all interfaces to keep backwards compatibility)
  • Instead of parsing and then dropping interfaces that aren't specified, can we simply not parse ones that aren't requested? This is similar to how the exclusion flag already works and would avoid some wasted computation.

- Instead of using an -interfaces flag, what do you think about having source mode read positional arguments (i.e., mockgen -source <path/to/file.go> InterfaceOne,InterfaceTwo) to align it with how reflect mode works? (If there are no positional arguments specified, we would parse all interfaces to keep backwards compatibility)
- Instead of parsing and then dropping interfaces that aren't specified, can we simply not parse ones that aren't requested? This is similar to how the exclusion flag already works and would avoid some wasted computation.
@kliukovkin
Copy link
Contributor Author

Hey @kliukovkin! Thanks for the PR!

I am in favor of having this feature, just two questions/comments:

  • Instead of using an -interfaces flag, what do you think about having source mode read positional arguments (i.e., mockgen -source <path/to/file.go> InterfaceOne,InterfaceTwo) to align it with how reflect mode works? (If there are no positional arguments specified, we would parse all interfaces to keep backwards compatibility)

  • Instead of parsing and then dropping interfaces that aren't specified, can we simply not parse ones that aren't requested? This is similar to how the exclusion flag already works and would avoid some wasted computation.

Hey @JacobOaks ! Done, can you please review and let me know if that looks good?

@kliukovkin
Copy link
Contributor Author

@JacobOaks hey Jacob! Gentle ping here.

Copy link
Contributor

@r-hang r-hang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. Looks good to me with some minor comments. Let's merge this when they are addressed.

wantErr: true,
},
{
name: "missing interface (Baz not found)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a test case for accidentally specified duplicate requested interfaces?

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := filterInterfaces(tt.args.all, tt.args.requested)
if (err != nil) != tt.wantErr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of wantErr being a bool, could we make this a string and compare the error contents?

mockgen/parse.go Outdated
Comment on lines 99 to 108

// If there are interfaces provided as positional arguments, filter them
if len(ifaces) > 0 {
if pkg.Interfaces, err = filterInterfaces(pkg.Interfaces, ifaces); err != nil {
log.Fatalf("Filtering interfaces failed: %v", err)
}
} else {
// No interfaces provided, process all interfaces for backward compatibility
log.Printf("No interfaces specified, processing all interfaces")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is still doing the thing @JacobOaks's comment advised against:

Instead of parsing and then dropping interfaces that aren't specified, can we simply not parse ones that aren't requested? This is similar to how the exclusion flag already works and would avoid some wasted computation.

@kliukovkin
Copy link
Contributor Author

Updated according to feedback: moved filtering inline into parseFile, removed filterInterfaces, updated tests. Thanks @r-hang @abhinav for reviewing!

Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sywhang This PR implements the changes you suggested in #281 (failing on missing expected interfaces).

@kliukovkin, thanks for updating. left some minor suggestions for improvements.
I see that the tests are failing, though.

@kliukovkin
Copy link
Contributor Author

Updated according to feedback.

  • accepted all your suggestions
  • removed the “missing interfaces” error check, because it was breaking integration tests - in practice, missing names should just be ignored

Thanks for the review @abhinav ! 🚀

@sywhang
Copy link
Contributor

sywhang commented Oct 3, 2025

@kliukovkin thanks for updating the PR - could you please check the CI test run results and update the checked in golden files for tests?

@kliukovkin
Copy link
Contributor Author

kliukovkin commented Oct 4, 2025

@kliukovkin thanks for updating the PR - could you please check the CI test run results and update the checked in golden files for tests?

@sywhang ran go generate ./... to address the CI failure and ran ./ci/test.sh, everything looks good now. Sorry for the earlier mismatch.

Copy link
Contributor

@sywhang sywhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sywhang sywhang merged commit 043dd32 into uber-go:main Oct 4, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce possibility to list required interfaces so mockgen won't generate all of them in source mode

7 participants