-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
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
Custom Cops for brew audit
#1873
Conversation
Why did the build fail? |
|
777988c
to
b18ffcf
Compare
@MikeMcQuaid Now only |
@GauthamGoli don't worry about it. There's a test suite bug that causes that test to intermittently fail. #1879 fixes it. |
It's merged so rerunning these tests. |
@MikeMcQuaid What should I do for codecov check failures? |
@GauthamGoli You can ignore them for now 👍 |
method, _args, body = *node | ||
_keyword, method_name = *method | ||
|
||
return unless method_name.equal?(:bottle) && revision?(body) |
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.
Make this multiple return
s.
lambda do |corrector| | ||
# Check for revision | ||
_method, _args, body = *node | ||
if revision?(body) |
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.
next unless revision?(body)
def revision?(body) | ||
body.children.each do |method_call_node| | ||
_receiver, method_name, _args = *method_call_node | ||
if method_name == :revision |
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.
return true if method_name == :revision
new_source = "" | ||
node.source.each_line do |line| | ||
if line =~ /\A\s*revision/ | ||
line = line.sub("revision", "rebuild") |
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.
line.sub!("revision", "rebuild")
def replace_revision(corrector, node) | ||
new_source = "" | ||
node.source.each_line do |line| | ||
if line =~ /\A\s*revision/ |
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.
Maybe next unless line =~ /\A\s*revision/
?
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.
next
cannot be used I think because on line 42 , the current line
is being appended to new_source
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.
Yeh, sorry, 👍
b18ffcf
to
7b6b2e7
Compare
new_source << line | ||
line.sub!("revision", "rebuild") | ||
end | ||
new_source << line |
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.
Looks like something weird's happened to your indentation here.
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.
Ah! Mix up of spaces and tabs due to opening in different editors. Fixing it.
7b6b2e7
to
7b68491
Compare
Tested locally. The user experience here is amazingly good. @reitermarkus @jawshooah you may want to consider this approach instead of a separate gem; if they have editor Rubocop integration with this one they'll just start seeing these warnings immediately with zero configuration. |
@MikeMcQuaid you just inspired me to set up Rubocop in my editor. This is amazing! Nice work @GauthamGoli! 👏 |
One thing: when I test this with a bottle block, my editor highlights |
method, _args, body = *node | ||
_keyword, method_name = *method | ||
|
||
return unless method_name.equal?(:bottle) |
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.
I think we should prefer #==
over #equal?
.
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.
Thanks. Pushed the updated code.
7b68491
to
d009e97
Compare
@alyssais Are you using Emacs? |
end | ||
|
||
def revision?(body) | ||
body.children.each do |method_call_node| |
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.
Can this be body.children.any?
instead?
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.
Thanks. Pushed the updated code.
d009e97
to
69bb6bb
Compare
@GauthamGoli I'm using Sublime Text 3 with SublimeLinter-rubocop. |
@alyssais Able to reproduce this. I think its because, I used |
@GauthamGoli fair enough. Let's forget about it for now, then, it's not a big deal. :) |
@alyssais I found a fix for this. Will update the code and push it in sometime! |
bf74c61
to
4321b6b
Compare
4321b6b
to
9f83f91
Compare
@alyssais Now the linter should highlight |
def check_revision?(body) | ||
body.children.each do |method_call_node| | ||
_receiver, method_name, _args = *method_call_node | ||
if method_name == :revision |
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.
Could do next unless method_name == :revision
to avoid indentation and end
Looks much better/simpler, thanks! |
9f83f91
to
89d5a1f
Compare
docs/brew.1.html
Outdated
run before submitting a new formula.</p> | ||
|
||
<p>If no <var>formulae</var> are provided, all of them are checked.</p> | ||
|
||
<p>If <code>--strict</code> is passed, additional checks are run, including RuboCop | ||
style checks.</p> | ||
style checks and custom cop checks.</p> |
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.
This is a bit redundant, since custom checks are still RuboCop style checks.
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.
Yeah, you're right. Fixed it.
89d5a1f
to
1f5cf4f
Compare
Library/.rubocop.yml
Outdated
@@ -6,6 +6,12 @@ AllCops: | |||
- '**/Casks/**/*' | |||
- '**/vendor/**/*' | |||
|
|||
require: | |||
- ./Homebrew/rubocops/bottle_block_cop.rb |
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.
Let's maybe require a since Homebrew/rubocops.rb
which in turn includes all our custom cops (just this one for now but we'll add more).
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.
Also could just do on a single line like require: ./Homebrew/rubocops.rb
perhaps?
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.
Yes, I was thinking of creating another another .yml
file(which will have all custom cops) andrequire
it inside .rubocop.yml
as the custom cops grow in number. Your idea of require
on rubocops.rb
is seems much better, but then we'd have to place all the cops in a single huge rubocops.rb
rather than having individual files. If thats not a problem, I'll go ahead and change it.
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.
but then we'd have to place all the cops in a single huge
rubocops.rb
Not really, we just require all of the individual cops inside rubocops/
with this file, so we don't have to do that inside the YAML file.
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.
Okay, cool. On my way to make the above changes.
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.
Facing an error when implementing the above: in `<module:Cop>':superclass must be a Class (Module given) (TypeError)
, trying to find a work around.
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.
@GauthamGoli feel free to post your code if you need a hand working it out! :)
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.
@alyssais I created Library/Homebrew/rubocops.rb
, and it has a single line Dir[File.dirname(__FILE__) + "/rubocops/*.rb"].each {|file| require file }
Line 10 in Library/.rubocop.yml
is now - ./Homebrew/rubocops.rb
Now just run brew audit --strict <someformula>
to see the error.
Further debugging shows the error is thrown at line 72 in /Homebrew/cmd/style.rb
and I think the issue is being unable to require
bottle_block_cop.rb
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.
@GauthamGoli it's failing because of this error:
Error: obsolete parameter IndentWhenRelativeTo (for Style/CaseIndentation) found in /Users/alyssa/code/brew/Library/.rubocop.yml
`IndentWhenRelativeTo` has been renamed to `EnforcedStyle`
obsolete parameter AlignWith (for Lint/EndAlignment) found in /Users/alyssa/code/brew/Library/.rubocop.yml
`AlignWith` has been renamed to `EnforcedStyleAlignWith`
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.
Let's just hard-code all the requires for now; dynamic requiring is a bit overkill for now.
@@ -0,0 +1,36 @@ | |||
module RuboCop | |||
module Cop | |||
module CustomCops |
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.
Is it possible to call this module Homebrew
?
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.
module Homebrew
has been used in Library/Homebrew/cmd/
and Library/Hombrew/dev-cmd/
, so I thought calling it as Homebrew
might cause some sort of confusion/clash ( I am not sure) and named it as CustomCops
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.
@GauthamGoli, this isn't really a problem as it's in another namespace. The only thing to look out for would be to reference the top-level module Homebrew
with ::Homebrew
inside Rubocop::Cop
, but I think we don't even need it in this context.
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.
this isn't really a problem as it's in another namespace
Agreed here.
3f225b7
to
8df4a0a
Compare
8df4a0a
to
5dc358c
Compare
Great work here @GauthamGoli 👏! |
brew tests
with your changes locally?This PR implements Custom Cops for a simple rule in
brew audit
command as discussed in #569More rules can be added/migrated as cops, and RuboCop can be leveraged to check for style violations instead of regular expressions as being done currently when
brew audit
is ran.Using
--strict
would check for CustomCop/style violations.The newly added
--fix
option would automatically correct the source code if any CustomCop/style violations are found.