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

tests: fix code style issues #587

Merged
merged 1 commit into from Aug 6, 2016

Conversation

eirinikos
Copy link
Contributor

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew tests with your changes locally?

This change fixes many of the existing bugs reported by brew style for Homebrew/test.

33 bugs remain; many of them are one of the following:

  • Method definitions must not be nested. Use lambda instead.
    • these method definitions all (or mostly) seem to be enclosed within formula do blocks; does it make sense to convert these to lambdas? (I haven't yet figured out a way to do so that passes brew tests.)
  • Use snake_case for method names.
    • examples of these methods include test_check_DYLD_vars and test_create_DATA; it seems like these acronyms are best kept as-is.
  • Prefer to_s over string interpolation.
    • these bugs are easy to fix, but many instances of string interpolation seem to be tolerated by brew style (e.g., in test_integration_cmds; why is that?

I'm sensing that Rubocop's opinions are meant to be guidelines (rather than gospel), but maybe I'm wrong!

🎈

@@ -154,7 +154,7 @@ def test_git_variant

assert_equal "e1893a6bd191ba895c71b652ff8376a6114c7fa7", @tap.git_head
assert_equal "e189", @tap.git_short_head
assert_match %r{years ago}, @tap.git_last_commit
assert_match /years ago/, @tap.git_last_commit
Copy link
Member

Choose a reason for hiding this comment

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

Could this just be "years ago" & avoid // entirely given we're not actually using a regex here or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me!

@UniqMartin UniqMartin added the gsoc-outreachy Google Summer of Code or Outreachy label Jul 27, 2016
@UniqMartin
Copy link
Contributor

This change fixes many of the existing bugs reported by brew style for Homebrew/test.

Sorry if I'm being a bit pedantic here, but “bugs” feels like to strong of a word. There's nothing inherently wrong or broken about the code, it's just that it doesn't fully conform to certain code style guidelines.

Aside from this there's also a minor issue with the commit subject (and PR title). The way I tend to read them is <area of changed code>: <what was changed> and in this spirit a more fitting subject would be tests: fix code style issues or something similar.

  • Method definitions must not be nested. Use lambda instead.
    • these method definitions all (or mostly) seem to be enclosed within formula do blocks; does it make sense to convert these to lambdas? (I haven't yet figured out a way to do so that passes brew tests.)

This is one of those cases where refactoring is unlikely to make sense and RuboCop suggests a solution that doesn't apply because it fails to understand the full context of the surrounding code. Depending on whether this affects individual lines or short blocks of code, the best solution is to suppress those complaints by temporarily disabling one or more cops, as detailed in the RuboCop documentation.

  • Use snake_case for method names.
    • examples of these methods include test_check_DYLD_vars and test_create_DATA; it seems like these acronyms are best kept as-is.

True. Silencing the relevant cop is the best way to address this and to drop the violation count.

  • Prefer to_s over string interpolation.
    • these bugs are easy to fix, but many instances of string interpolation seem to be tolerated by brew style (e.g., in test_integration_cmds; why is that?

This mostly sounds like a misunderstanding to me, i.e. in which contexts this suggestion applies. "#{something}" is an unnecessary use of string interpolation because there's only the variable something and no surrounding text or other variables, thus something (if it is a string) or something.to_s (if it can be converted to a string) is more appropriate. "#{something}\n" instead is a perfectly fine use of string interpolation.

I'm sensing that Rubocop's opinions are meant to be guidelines (rather than gospel), but maybe I'm wrong!

Indeed, they are guidelines and where possible, we have tweaked the settings because our preferred style doesn't fully align with what RuboCop considers to be the best default. It can sometimes make sense to tweak a rule instead of “fixing” our code, though most suggestions and defaults are good (and just show how old and diverse some of our code base is).

@@ -142,7 +142,7 @@ def test_check_user_path_bin
def test_check_user_path_sbin
sbin = HOMEBREW_PREFIX/"sbin"
ENV["PATH"] = "#{HOMEBREW_PREFIX}/bin#{File::PATH_SEPARATOR}" +
ENV["PATH"].gsub(%r{(?:^|#{File::PATH_SEPARATOR})#{sbin}}, "")
ENV["PATH"].gsub(/(?:^|File::PATH_SEPARATOR.to_s)sbin.to_s/, "")
Copy link
Contributor

Choose a reason for hiding this comment

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

The substitution of #{File::PATH_SEPARATOR} with File::PATH_SEPARATOR.to_s is wrong and the same applies to the #{sbin} and sbin.to_s pair. A relatively easy way to check this is to enter both regular expressions into brew irb (though we'll need to also provide sbin):

$ brew irb
==> Interactive Homebrew Shell
Example commands available with: brew irb --examples
irb(main):001:0> sbin = HOMEBREW_PREFIX/"sbin"
=> #<Pathname:/opt/homebrew/sbin>
irb(main):002:0> %r{(?:^|#{File::PATH_SEPARATOR})#{sbin}}
=> /(?:^|:)\/opt\/homebrew\/sbin/
irb(main):003:0> /(?:^|File::PATH_SEPARATOR.to_s)sbin.to_s/
=> /(?:^|File::PATH_SEPARATOR.to_s)sbin.to_s/

The correct substitution should be:

/(?:^|#{Regexp.escape(File::PATH_SEPARATOR)})#{Regexp.escape(sbin)}/

(Regexp.escape makes additionally sure to properly handle any characters that we want to be interpreted literally, but that have special meaning when used inside a regular expression.)

@eirinikos eirinikos closed this Jul 27, 2016
@eirinikos eirinikos deleted the fix-style-bugs-homebrew-test branch July 27, 2016 19:56
@eirinikos eirinikos restored the fix-style-bugs-homebrew-test branch July 27, 2016 19:57
@eirinikos eirinikos changed the title style: fix style bugs in Homebrew/test tests: fix code style issues Jul 27, 2016
@eirinikos
Copy link
Contributor Author

eirinikos commented Jul 27, 2016

Sorry -- I accidentally closed this PR when I decided to rename my local and remote branches (without realizing the effect this would have on the PR). 😳 Working on addressing the feedback...

@eirinikos eirinikos reopened this Jul 27, 2016
@eirinikos eirinikos force-pushed the fix-style-bugs-homebrew-test branch from 5f6df5d to 556af40 Compare July 28, 2016 19:03
@eirinikos
Copy link
Contributor Author

eirinikos commented Jul 28, 2016

Sorry if I'm being a bit pedantic here, but “bugs” feels like to strong of a word. There's nothing inherently wrong or broken about the code, it's just that it doesn't fully conform to certain code style guidelines.

Aside from this there's also a minor issue with the commit subject (and PR title). The way I tend to read them is <area of changed code>: <what was changed> and in this spirit a more fitting subject would be tests: fix code style issues or something similar.

No worries, that makes sense to me. I thought I remembered hearing these issues referred to as "bugs" some time ago, but I see that that isn't the most accurate way to refer to them. I've updated the commit and PR title, though the branch still carries the old name. 😬

Thanks for pointing me to the Rubocop documentation on temporarily disabling cops. I've done this for the Method definitions must not be nested and Use snake_case for method names warnings. I followed the example of some existing calls to rubocop:disable and included a code comment explaining the purpose of disabling Rubocop (one comment per type of cop, per file).

I also removed the string interpolation in test_formulary and test_tap since it was unnecessary in both instances (I hadn't actually examined these offenses before).

It's probably obvious that I approached this task with haste and impatience, so I've learned my lesson. Thank you @UniqMartin and @DomT4 for taking a closer look here! 😁

9 Rubocop offenses remain in Homebrew/test; I hope it's OK for these to stay on the back-burner for now:

== test_dependency_collector.rb ==
C: 10: 40: Avoid the use of the case equality operator ===.
== test_ENV.rb ==
W: 31:  5: Do not suppress exceptions.
W: 31:  5: Avoid rescuing the Exception class. Perhaps you meant to rescue StandardError?
== test_formula.rb ==
W:511: 23: Comparison of something with itself detected.
C:523: 29: Do not use semicolons to terminate expressions.
== test_patching.rb ==
C:126:  9: Avoid multi-line chains of blocks.
C:140:  9: Avoid multi-line chains of blocks.
C:239:  9: Avoid multi-line chains of blocks.
== testing_env.rb ==
C: 45:  7: Replace class var @@log with a class instance var.

@@ -193,7 +193,7 @@ def test_cellar_formula
end

def test_env
assert_match %r{CMAKE_PREFIX_PATH="#{HOMEBREW_PREFIX}[:"]},
assert_match /CMAKE_PREFIX_PATH="#{Regexp.escape(HOMEBREW_PREFIX)}[:"]/,
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Got it. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...though now Rubocop is saying Don't use parentheses around a literal... 😑

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, needs to be this line and the next

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean like this?

assert_match(/CMAKE_PREFIX_PATH="#{Regexp.escape(HOMEBREW_PREFIX)}[:"]/,
             cmd("--env"))

Copy link
Member

Choose a reason for hiding this comment

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

Yep!

@MikeMcQuaid
Copy link
Member

@eirinikos Nice work! Added a few comments and then we can 🚢 this.

@eirinikos eirinikos force-pushed the fix-style-bugs-homebrew-test branch from 556af40 to 914893c Compare August 2, 2016 17:46
@eirinikos
Copy link
Contributor Author

@MikeMcQuaid Thanks!


# `formula do` uses nested method definitions
Style/NestedMethodDefinition:
Enabled: false
Copy link
Member

Choose a reason for hiding this comment

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

Add a newline at the end of this file (ideally configure your editor to do this automagically!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! 👍

@eirinikos eirinikos force-pushed the fix-style-bugs-homebrew-test branch from 914893c to 2a67d70 Compare August 4, 2016 19:16
@UniqMartin UniqMartin merged commit 8ec5925 into Homebrew:master Aug 6, 2016
@UniqMartin
Copy link
Contributor

Merged in 8ec5925. Thank you for another great contribution to Homebrew, @eirinikos! 🎉

@MikeMcQuaid
Copy link
Member

Nice work @eirinikos 👏

@Homebrew Homebrew locked and limited conversation to collaborators May 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
gsoc-outreachy Google Summer of Code or Outreachy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants