Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move generated ExpecterTest to mocks directory #454

Merged
merged 1 commit into from May 24, 2022

Conversation

Gevrai
Copy link

@Gevrai Gevrai commented Apr 20, 2022

Description

Fixes the location of the --expecter tests mocks, as requested in #396 (comment)

Type of change

Test files moved to correct location, new behaviour change for users.

Version of Golang used when building/testing:

  • 1.11
  • 1.12
  • 1.13
  • 1.14
  • 1.15
  • 1.16
  • 1.17
  • 1.18

How Has This Been Tested?

Tested with go test ./... on full project

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@codecov-commenter
Copy link

codecov-commenter commented Apr 20, 2022

Codecov Report

Merging #454 (265dd0d) into master (aad3571) will decrease coverage by 24.94%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           master     #454       +/-   ##
===========================================
- Coverage   70.76%   45.81%   -24.95%     
===========================================
  Files           7       40       +33     
  Lines        1293     1997      +704     
===========================================
  Hits          915      915               
- Misses        325     1029      +704     
  Partials       53       53               
Impacted Files Coverage Δ
mocks/pkg/fixtures/ExpecterTest.go 84.00% <100.00%> (ø)
mocks/pkg/fixtures/requester_unexported.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/RequesterSlice.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/KeyManager.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/AsyncProducer.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/Example.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/MapFunc.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/RequesterArray.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/ConsulLock.go 0.00% <0.00%> (ø)
mocks/pkg/fixtures/MapToInterface.go 0.00% <0.00%> (ø)
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update aad3571...265dd0d. Read the comment docs.

@Gevrai
Copy link
Author

Gevrai commented Apr 20, 2022

@LandonTClipp As far as I can see the command make mocks doesn't generate all the right ones in the mocks file, is that expected? I didn't add this one for the expecter because it seemed outdated, but I might be wrong.

@@ -1,18 +1,26 @@
// Code generated by mockery. DO NOT EDIT.
// Code generated by mockery v1.0.0. DO NOT EDIT.
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like you used the wrong version to generate the mocks...strange.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, could you verify what mockery you're using to build the mocks? These version strings should be disabled so this is telling me that your mockery probably wasn't reading the config properly. (maybe we should re-introduce the version numbers?)

Copy link
Author

Choose a reason for hiding this comment

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

I hadn't used the Makefile, fixed now

@grongor
Copy link
Contributor

grongor commented Apr 20, 2022

@Gevrai I think the mocks are generated correctly (but I might be wrong, please post details if you are sure). The issue here is that you combine the generated mocks and the fixtures in an unusual way for this repo IMO. All generator tests have their expected output in the generator test itself. I think the best way forward is to just update the expecter test to behave the same as other tests, and I'll look into it later and try to move these fixed strings to separate files.

The problem with what you did (I think) is that you say that you expect the generated code to be equal to the mock file, but the mock file itself is generated by the generator, so this test doesn't really validate anything. I might be wrong though...

@Gevrai
Copy link
Author

Gevrai commented Apr 20, 2022

@Gevrai I think the mocks are generated correctly (but I might be wrong, please post details if you are sure). The issue here is that you combine the generated mocks and the fixtures in an unusual way for this repo IMO. All generator tests have their expected output in the generator test itself.

Oh, my bad I didn't think those were autogenerated anymore, when doing make clean mocks locally it doesn't really seem to be working as expected, some are modified/deleted. I half expected the script to not be used 😅

I saw that there were some other tests reading from expected file (TestGeneratorVariadicArgs and TestGeneratorVariadicArgsAsOneArg) and went that route because it made the most sense to me. I don't exactly remember why I had put it in a new mocks directory initially (it's been more than 6 months now..), maybe because I saw the other files were generated, I'm not sure.

I think the best way forward is to just update the expecter test to behave the same as other tests, and I'll look into it later and try to move these fixed strings to separate files.

I would kind of want to keep the unit tests for the autogenerated expecter (file now at mocks/pkg/fixtures/ExpecterTest_test.go). I can't add those tests if I have them inlined, unless maybe next to the autogenerated version?

I would be strongly in favor having the fixed string in separate files, maybe even some kind of setup were we can have a full test defined in a file as

// Interface definition to test
// Generator Config
// Expected output

The problem with what you did (I think) is that you say that you expect the generated code to be equal to the mock file, but the mock file itself is generated by the generator, so this test doesn't really validate anything. I might be wrong though...

You are right, for the future at least. I didn't run regenerate the directory on my side so for now so it is indeed validating that the output is valid, but that would be overwritten as soon as the directory is regenerated...

@@ -240,7 +240,7 @@ func NewRequester(t testing.TB) *Requester {
}

func (s *GeneratorSuite) TestGeneratorExpecterComplete() {
expectedBytes, err := ioutil.ReadFile(getFixturePath("mocks", "expecter.go"))
expectedBytes, err := ioutil.ReadFile(getMocksPath("ExpecterTest.go"))
Copy link
Contributor

Choose a reason for hiding this comment

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

The thing I agree with @grongor here is that it's odd to be asserting the output of mockery with the output of mockery (you see what I mean?). You are generating ExepcterTest.go in the mocks package, then you generate it again in this test and assert that it's the same. It's not necessarily wrong to do it this way, but none of the other tests do it this way (see TestGeneratorFunction below).

Although, I can see an argument to be made that because the Expecter interface is so verbose in the generated code, that cluttering the tests with expected code would be too much. But I think that for this case, we really should have the expected code as a raw string in the test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should stick with how the tests are done for now (strings directly in the test), but definitely look for improvements later on. As you mentioned, asserting mockery with mockeries output isn't correct, but we definitely can improve the readability of the tests, maybe even the way @Gevrai described. But it definitely is out of the scope of this, and it's a bigger decision that should be done correctly (so that we don't face it again in a year, hopefully :D ).

Copy link
Contributor

Choose a reason for hiding this comment

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

I do really like the idea of keeping unit tests for the generated mocks (mocks/pkg/fixtures/ExpecterTest_test.go), that can probably stay. I think we should have more of these unit tests on the generated mocks as another layer.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Gevrai let me know if you could make the change (expected output as a string literal in the test). We can get it merged once that's settled. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Hey sorry! Things got in the way a bit, I'll have it done this evening.

Just to make sure

  • Keep test in mocks/pkg/fixtures/ExpecterTest_test.go
  • Inline generated expecter check instead of reading from file

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries thanks again for the contributions. Yeah those two points should be it! (and the merge conflicts that just popped up)

Copy link
Author

Choose a reason for hiding this comment

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

Done, should be good now.

FYI I've changed the name of the interface (ExpecterTest -> Expecter) for coherence with others, and added the expecter to be generated by Makefile so that the unit tests have something to run on!

@Gevrai Gevrai force-pushed the gejo-move-expecter-test branch 2 times, most recently from fe03e28 to f9cad3c Compare April 26, 2022 03:36
@Gevrai
Copy link
Author

Gevrai commented May 24, 2022

branch rebased

@LandonTClipp
Copy link
Contributor

Thanks @Gevrai you read my mind.

@LandonTClipp LandonTClipp merged commit 9dba1fd into vektra:master May 24, 2022
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.

None yet

4 participants