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

Rubocop fixes for #653 #658

Merged
merged 23 commits into from
Feb 4, 2022
Merged

Conversation

Justin-W
Copy link
Contributor

This PR resolves many of the rubocop cop offenses (#653) which were previously-ignored due to the auto-generated config in .rubocop_todo.yml.

Almost all of the changes are "pure code refactoring", with zero impact to the behavior of the modified code (the notable exception being 1 unit test, which was found to be previously-broken, and is now fixed).

All of the fixed/excluded offenses were resolved incrementally (one cop at a time, in the order indicated below), by either auto-fixing the manually re-enabled offenses or else explicitly disabling/excluding (inline or in the .rubocop.yml, instead of in .rubocop_todo.yml) the previously-ignored offenses.

After each incremental change (whether auto-fixed by rubocop or manually fixed/resolved), the incremental code changes were re-reviewed (for both correctness and readability), and the unit test suite was re-executed (to verify no regressions were introduced). Where either the code review and/or unit tests indicated that the auto-fixed changes were unacceptable, the auto-fixed changes were discarded and manual fixes/resolution were applied instead.

Auto-fixes were performed via the rubocop -x, rubocop -a, or rubocop -A commands (in that order), depending on the auto-correct requirements for each individual cop.

Checklist:

Please check each of the boxes below for which you have completed the corresponding task:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • All unit tests pass locally (after my changes)
  • Rubocop reports zero offenses (after my changes)

Please include below the summary portions of the output from the following 2 scripts:

pushd src/bosh_azure_cpi
  ./bin/test-unit
  ./bin/rubocop_check
popd

NOTE: Please see how to setup dev environment and run unit tests in docs/development.md.

Unit Test output:

1013 examples, 0 failures
Coverage report generated for RSpec to /Users/justinw/go/src/github.com/cloudfoundry/bosh-azure-cpi-release/src/bosh_azure_cpi/coverage. 4234 / 4253 LOC (99.55%) covered.

Rubocop output:

187 files inspected, no offenses detected

Changelog

NOTE: This refactoring makes the modified code section's layout more consistent with the project-wide layout conventions.
SEE: https://rubystyle.guide/#one-class-per-file
NOTE: This file contains 9 "model" classes, of which only 1 is named "Config", and at least 4 of the 9 appear to be completely unrelated to the Config class. Also, several of the 9 classes have class-specific spec files (see: "src/bosh_azure_cpi/spec/unit/models/*_config_spec.rb"). So it seems like the code would be more maintainable if up to 8 of the 9 classes were split into separate files, with filenames corresponding to the class name and spec file name.
…sses over the default 100 lines.

NOTE: It seems preferable to explicitly disable this cop (inline) for these classes, rather than setting a higher project-wide threshold (in .rubocop_todo.yml) since the threshold doesn't make reveal which classes are over the default threshold.
…les.

NOTE: Since spec files tend to have long blocks. it seems preferable to explicitly disable this cop for those files, in order to be able to set a lower project-wide limit (which will apply to non-spec files).
NOTE: Although 'shared_stuff.rb' is not technically a spec file, it contains rspec code, and should therefore be subject to similar rubocop config as the '*_spec.rb' files (see the NOTE above).
NOTE: Each of the existing offenses is less readable if the offense if fixed. So this seems like a cop which reduces readability, and should therefore be disabled project-wide.
NOTE: The offenses were permanently excluded (in '.rubocop.yml' instead of '.rubocop_todo.yml'), since fixing them would be backwards-incompatible with Ruby < v2.7, and it seems like the file containing the offenses _might_ need to be backwards-compatible with earlier versions.
NOTE: This cop was permanently disabled (in '.rubocop.yml' instead of '.rubocop_todo.yml') for 1 particular file, for reasons detailed in the comments, since using inline rubocop comments to exclude the 6 specific offenses did not prevent `rubocop -A` from auto-fixing those offenses. This could unintentionally prevent other (future, hypothetical) offenses in that same file from getting detected/fixed, but that seems preferable these 6 offenses getting unintentionally auto-fixed.
Offenses:

lib/cloud/azure/restapi/azure_client.rb:156:1: C: [Corrected] Layout/IndentationStyle: Tab detected in indentation.
        while next_url != nil
^
lib/cloud/azure/restapi/azure_client.rb:156:2: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
        while next_url != nil ...
 ^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:156:8: C: [Corrected] Style/NonNilCheck: Prefer !next_url.nil? over next_url != nil.
        while next_url != nil
       ^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:156:10: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
         while !next_url.nil? ...
         ^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:156:10: C: [Corrected] Style/NegatedWhile: Favor until over while for negative conditions.
         while !next_url.nil? ...
         ^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:157:2: C: [Corrected] Layout/IndentationWidth: Use 2 (not 9) spaces for indentation.
          @logger.debug("Getting resources from nextLink #{next_url}")
 ^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:157:10: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation.
          @logger.debug("Getting resources from nextLink #{next_url}")
         ^
lib/cloud/azure/restapi/azure_client.rb:158:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                uri = URI(next_url)
                ^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:159:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                response = http_get(uri)
                ^^^^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:160:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                cloud_error("Got empty page from nextLink #{next_url}") if response.body.nil?
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:162:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                body = JSON.parse(response.body)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:163:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                result.deep_merge!(body)
                ^^^^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:164:17: C: [Corrected] Layout/IndentationConsistency: Inconsistent indentation detected.
                next_url = body['nextLink']
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/cloud/azure/restapi/azure_client.rb:165:9: W: [Corrected] Layout/EndAlignment: end at 165, 8 is not aligned with while at 156, 1.
        end
        ^^^
lib/cloud/azure/restapi/azure_client.rb:165:9: W: [Corrected] Layout/EndAlignment: end at 165, 8 is not aligned with while at 156, 9.
        end
        ^^^
lib/cloud/azure/restapi/azure_client.rb:165:10: W: [Corrected] Layout/EndAlignment: end at 165, 9 is not aligned with until at 156, 8.
         end
         ^^^

187 files inspected, 16 offenses detected, 16 offenses corrected
…y#653).

NOTE: The fixed offenses were auto-fixed (via `rubocop -x`) incrementally, by re-enabling and fixing the offenses of one cop at each step (in the order indicated below).
SQUASHED COMMIT HISTORY:
---
rubocop: Auto-fixed: Layout/TrailingEmptyLines.
rubocop: Auto-fixed: Layout/SpaceInsideHashLiteralBraces.
rubocop: Auto-fixed: Layout/SpaceInsideArrayLiteralBrackets.
rubocop: Auto-fixed: Layout/MultilineMethodCallIndentation.
NOTE: The default `aligned` style is the closest match to the existing code.
rubocop: Auto-fixed: Layout/LineEndStringConcatenationIndentation.
rubocop: Auto-fixed: Layout/FirstHashElementIndentation.
rubocop: Auto-fixed: Layout/FirstArgumentIndentation.
rubocop: Auto-fixed: Layout/ClosingParenthesisIndentation.
rubocop: Auto-fixed 2 cop offenses: Layout/ExtraSpacing, Layout/SpaceAroundOperators.
rubocop: Auto-fixed 2 cop offenses: Layout/EmptyLinesAroundBlockBody.
rubocop: Auto-fixed 1 cop offenses: Layout/EmptyLineBetweenDefs.
rubocop: Auto-fixed 11 cop offenses: Layout/EmptyLinesAroundAttributeAccessor.
NOTE: This spec previously passed (despite an incorrect expectation) due to incorrect syntax. The incorrect syntax was one of the (previously-ignored) 'Lint/AmbiguousBlockAssociation' rubocop offenses.
NOTE: The offense was a false-positive. In this particular case, we must violate the cop's rule. So the offense is now disabled via an inline rubocop comment.
NOTE: The 'Cop supports --auto-correct' auto-fixes resulted in incorrect fixes for these offenses, requiring manual fixing instead.
NOTE: The offenses were permanently excluded (in '.rubocop.yml' instead of '.rubocop_todo.yml'), since fixing them would be backwards-incompatible with Ruby < v2.4, and it seems like the file containing the offenses _might_ need to be backwards-compatible with earlier versions.
…y#653).

NOTE: The fixed offenses were auto-fixed (via `rubocop -a`) incrementally, by re-enabling and fixing the offenses of one cop at each step (in the order indicated below).
SQUASHED COMMIT HISTORY:
---
rubocop: Auto-fixed: Style/QuotedSymbols.
rubocop: Auto-fixed: RSpec/Yield.
rubocop: Auto-fixed: RSpec/ReceiveCounts.
rubocop: Auto-fixed: RSpec/NotToNot.
rubocop: Auto-fixed: RSpec/HookArgument.
rubocop: Auto-fixed: RSpec/ExcessiveDocstringSpacing.
rubocop: Auto-fixed: RSpec/EmptyLineAfterHook.
rubocop: Auto-fixed: RSpec/EmptyLineAfterFinalLet.
rubocop: Auto-fixed: RSpec/EmptyLineAfterExampleGroup.
rubocop: Auto-fixed: RSpec/ContextMethod.
rubocop: Auto-fixed: Lint/SymbolConversion.
rubocop: Auto-fixed: Style/StringLiterals.
rubocop: Auto-fixed: Style/SoleNestedConditional.
NOTE: A few of the auto-corrected changes could potentially be simplified further by refactoring them to use the Hash#dig method instead of chained Hash#fetch calls (i.e. chained square brackets).
rubocop: Auto-fixed: Style/SlicingWithRange.
rubocop: Auto-fixed: Style/RedundantRegexpEscape.
rubocop: Auto-fixed: Style/RedundantFileExtensionInRequire.
rubocop: Auto-fixed: Style/RedundantBegin.
rubocop: Auto-fixed: Style/RedundantAssignment.
rubocop: Auto-fixed: Style/PercentLiteralDelimiters.
rubocop: Auto-fixed: Style/NestedTernaryOperator.
rubocop: Auto-fixed: Style/IfInsideElse.
rubocop: Auto-fixed: Style/HashSyntax.
rubocop: Auto-fixed: Style/HashEachMethods.
rubocop: Auto-fixed: Style/HashConversion.
rubocop: Auto-fixed: Style/FrozenStringLiteralComment.
rubocop: Auto-fixed: Style/EmptyLiteral.
rubocop: Auto-fixed: Style/CommentAnnotation.
rubocop: Auto-fixed: Style/ClassEqualityComparison.
rubocop: Auto-fixed: Style/CaseLikeIf.
NOTE: The auto-fix also required that the Metrics/ModuleLength cop's Max (in .rubocop_todo.yml) be updated, to keep that cop from causing a new offense.
rubocop: Auto-fixed: Style/BisectedAttrAccessor.
NOTE: These are the remaining (non-excluded) offenses of this cop, and exclude the offenses which were manually excluded by an earlier commit.
rubocop: Auto-fixed: Lint/AmbiguousRegexpLiteral.
rubocop: Auto-fixed: Lint/AmbiguousOperatorPrecedence.
rubocop: Auto-fixed: Lint/AmbiguousOperator.
NOTE: This auto-fix required the use of the `rubocop --auto-correct-all` command.
   -A, --auto-correct-all           Auto-correct offenses (safe and unsafe)
NOTE: The 'Cop supports --auto-correct' auto-fixes resulted in harder-to-read code than before, warranting manual refactoring instead.
NOTE: The `Style/NumericPredicate` cop was reporting 9 auto-correctable offenses (for the modified lines). But auto-fixing the offenses caused failing specs, so those 9 cop offenses have been disabled inline.
…s added code comments).

NOTE: The offending method seems like a candidate for more/different refactoring beyond a fix for the `Style/ExplicitBlockArgument` offense.
@Justin-W
Copy link
Contributor Author

@klakin-pivotal Regarding the number of commits: I wasn't quite sure how much I should squash the commits in this PR.

On the one hand, each rubocop cop's offenses represent a distinct set of issues to be resolved (and the fixes typically involve similar code changes, making them easy to review toether).

On the other hand, all of the changes (whether auto-fixed or manual) have the same objectives (i.e. improve code correctness, maintainability, readability, etc.), but squashing together the fixes for multiple cops' offenses makes the code review more cognitively difficult.

Therefore, I've prepared 3 different branches as options for you:

This PR is currently based on branch 'B', for the reasons noted above (and since it seemed like the probably-best option based on your prior feedback for a different PR). However, if you would prefer more or fewer commits, I'd be happy to revise the PR to the HEAD of either of the other 2 branches.

@klakin-pivotal
Copy link
Contributor

So, I'm probably not the fellow you should be asking to review PRs that change rubocop stuff. Because it has never told me to change anything that has been actually useful, (and has -at times- demanded that I change many things that have made my programs substantially more difficult to read and understand) I hate it with a burning passion.

Anyway. A few things:

  1. If I were reviewing this PR, and if I didn't hate rubocop I would prefer that all the RuboCop changes be squashed into a single commit, with the reasoning behind any changes that you've made in the commit message. So, if you feel that the the 22-commit PR is a good way to break out the changes, I would prefer that the reasoning described in those 22 commit messages be put into the message of a single squash commit.
    1a) I also would not include the text from the SQUASHED COMMIT HISTORY: part onwards. In my opinion, the commit messages in this section don't contain enough information to be worth keeping around. Any information that they would contain should be described with some prose in the commit message.
  2. I notice that most of the Offense Count counts (in terms of number of lines that contain the phrase "Offense count: ") in src/bosh_azure_cpi/.rubocop_todo.yml increased. What's up with that? (Though, to be fair, some of the ones that decrased, decreased a ton.)
  3. I'll restate this, but more explicitly: I probably should not be reviewing this PR... I really, really, dislike rubocop, and wouldn't mind if it was suddenly erased from existence. ;)

So, yeah.

Someone who's not me should actually review this PR, and if you dislike what I've said here, please do ignore it!

(@Justin-W, can you request a review from someone, or do you need me to select someone? That is, do you see a thing in the upper-right-ish of the UI that says Reviewers ... Suggestions, and maybe has three folks in the list? The UI I'm talking about seems to be pretty close to what's described in this blog post: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review)

@Justin-W
Copy link
Contributor Author

Justin-W commented Feb 1, 2022

@klakin-pivotal Thanks for the response. Answers below.

So, I'm probably not the fellow you should be asking to review PRs that change rubocop stuff. Because it has never told me to change anything that has been actually useful, (and has -at times- demanded that I change many things that have made my programs substantially more difficult to read and understand) I hate it with a burning passion.

Lol. Ok, fair enough. And I've had some similar frustrations, but have found that it does occasionally expose real issues (like the test which was broken but incapable of failing, and which I've fixed in this PR). And such "hidden bugs" were the primary reason for even attempting this PR (with code style consistency across the project being secondary).

  1. If I were reviewing this PR, and if I didn't hate rubocop I would prefer that all the RuboCop changes be squashed into a single commit,
    ...
    Any information that they would contain should be described with some prose in the commit message.

I'm fine with that. But it sounds like you think maybe the "different reviewer" should give an opinion first? I really don't care either way, other than trying to minimize additional/wasted time spent on this PR.

  1. I notice that most of the Offense Count counts (in terms of number of lines that contain the phrase "Offense count: ") in src/bosh_azure_cpi/.rubocop_todo.yml increased. What's up with that? (Though, to be fair, some of the ones that decrased, decreased a ton.)

I checked, and it looks like the ones that increased slightly were mostly increased because the src/bosh_azure_cpi/.rubocop_todo.yml file wasn't updated by #651, and the new code added by that PR increased those offense counts because the new code matched the project's existing (rubocop-cop-offending) code style (and mainly in files which already contained similar offenses before #651). So those increased counts should be viewed as corrections to the pre-PR counts, not as new offenses from this PR.

...
Someone who's not me should actually review this PR, and if you dislike what I've said here, please do ignore it!

(@Justin-W, can you request a review from someone, or do you need me to select someone?

I do not have the permission to assign reviewers within this project, so you (or @rkoster) would need to make such assignments. (I have it on other orgs/projects, but not for this one.)

Also, I only made this whole set of "fixes" with the intent to improve the state of the project. So if there are serious concerns about whether it actually does so, I'm open to hearing that.

@rkoster rkoster requested review from a team, Benjamintf1 and KaiHofstetter and removed request for a team February 1, 2022 08:21
@rkoster rkoster requested review from a team, jpalermo and beyhan and removed request for Benjamintf1, KaiHofstetter and a team February 1, 2022 08:21
Copy link
Member

@beyhan beyhan left a comment

Choose a reason for hiding this comment

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

@Justin-W thank you for looking into this. The change looks good to me. I just have one comment about one refactoring (change) which I couldn't follow.

Copy link
Contributor

@rkoster rkoster left a comment

Choose a reason for hiding this comment

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

Wow, that was a lot of changes, to review. But apart from the things Beyhan already asked about, everything made sense.

@rkoster rkoster merged commit 4a066fa into cloudfoundry:master Feb 4, 2022
@rkoster
Copy link
Contributor

rkoster commented Feb 4, 2022

Thanks! @Justin-W

@Justin-W Justin-W deleted the rubocop-fixes-b branch February 9, 2022 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants