-
Notifications
You must be signed in to change notification settings - Fork 11
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
Use analysis.Analyzer #16
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Woah, very nice. You beat me to it. Thank you. Some questions below. I'm wondering how we can reduce the diff so that the tests stay mostly the same to ensure that this is only a refactor of code, and not a change in behavior and functionality. Thoughts?
) | ||
|
||
func TestCheckNoGlobals(t *testing.T) { | ||
cases := []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What causes us not to be able to use the existing tests? The behavior of the tool isn't changing, so it should be possible in theory to make minor alterations to the tests but their setup and expectations should still be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there's no longer any code to traverse directories, open files and do all the work before getting to the ast I didn't keep them. It felt bad to keep the code or write new code for this kind of job when it's handled by the analysis package already and won't be a part of the code.
To address what you mention in the comment - we could keep the testdata structure and move each folder into testdata/src
then add 10 different package tests. However, some of them will be irrelevant since their only purpose was to test the file and directory walking.
I'll take a pass and start looking into how I can minimise the diff.
Co-authored-by: Leigh McCulloch <leigh@mcchouse.com>
Good input on reducing diff and only doing one thing per PR! I dropped the I also tried to keep all the test files/packages. As mentioned above I can't keep them 1:1. First of all, we don't have any code walking directories finding files anymore. Secondly as mentioned in the test with a comment, I cannot get tests to pass by enabling packages including tests (with or without the flag). The last bit I can think of now is if we wan't to wrap the analyser in a constructor to follow the linters own principle of having no globals or if we should keep going with the common way of having the analyzer as a global variable. Please give this a new pass and let me know what you think! |
Oh crap... The issue with analysis is not limited to tests. This has to do with the analyzer itself! % gochecknoglobals ./...
/Users/simon.sawert/git/gochecknoglobals/check_no_globals.go:30:5: Analyzer is a global variable
/Users/simon.sawert/Library/Caches/go-build/c2/c22bf8402833e9a68e0989ace01544e619f369592b95532b3a35bf2f5d9fba5d-d:19:5: tests is a global variable
/Users/simon.sawert/Library/Caches/go-build/c2/c22bf8402833e9a68e0989ace01544e619f369592b95532b3a35bf2f5d9fba5d-d:25:5: benchmarks is a global variable
/Users/simon.sawert/Library/Caches/go-build/c2/c22bf8402833e9a68e0989ace01544e619f369592b95532b3a35bf2f5d9fba5d-d:29:5: examples is a global variable I'll dig into this to see how we can eliminate this! Feel free to give input if you have any suggestions. |
Hmm, I'd need to dig into the other issue you saw, probably later tonight. I'll come back to you. |
Thanks a bunch! I'll fix the other things! Currently the only idea I can think of would be to only parse files with a To get some more info on this I did a change in I'll commit and push a |
I had another look through the diff and this looks great! 🎉 I didn't know about the analysistest package and I love that it simplifies things. I need to test it out on a few projects before merging, but I'll need a few days to do that because of other commitments. I'll come back to you soon. |
This looks great. I've pushed two commits. The first reduces the diff a little more, but retaining the same logic and functionality. The main change in this is combining the The second moves the main back to the root and moves the logic to a sub-package. This keeps the install instructions the same and doesn't break existing installs. I think this also fits the type of program better too. It is primarily a binary, so that should be the first thing people see. I ran some tests and I noticed that there are four behavior that weren't evident from the tests:
All in all I think this is good to merge 🎉 except for point 2 and 4 above. I'd love your thoughts on them. [Edit: Added point 4 above.] |
Sorry for the late reply, been super busy today. Sounds good to put back as much as possible to I understand the reasoning of having main in the root. I'm personally not a fan of that layout but I guess it's good to do as few changes as possible for this first pass.
I think you're doing the right thing to minimise diff but I would also argue that if you are worried about breaking changes, just bump to a new version. You should be able to move forward freely with this linter and if it will become the source of |
Co-authored-by: Simon Sawert <simon@sawert.se>
I like the work around you added modifying os.Args. That's neat and very simple. It's much simpler than what I was thinking, basically rewriting the main function instead of using the singlechecker. This is better though. |
@bombsimon Thanks for making this change clean, straightforward, and easy to understand! And for working out the issues with running the tests. |
Thanks for quick turnarounds, good discussions and the merge! :) I think this will help for future changes and make it easy to maintain |
Done! |
I did a quick update for the linter to use Go's analyzer which has some great features. This linter no longer has to figure out paths to files, tests, filenames, lines etc. It's also super easy to maintain tests since you just add diagnostics to your test package and write a oneliner test using
analysistest
. I had one issue with testing test files as you can see which resulted in an upstream issue here. The test code generated will have global variables which will make the test not pass.The main reason however was to address golangci-lint#1393 and make it easier to keep golangci-lint in sync with this linter.
After merging this it will be super easy to just add this analyzer to
golangci-lint
like this.Please let me know if you think this is a good idea or if you want any changes to this approach!