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

Handle unknown HTTP status codes for RSpecRails::HttpStatus cop #51

Conversation

viralpraxis
Copy link
Contributor

@viralpraxis viralpraxis commented Oct 2, 2024

Consider the following snippet

it { expect(response).to have_http_status("some-string") }

RSpecRails::HttpStatus (configured with default symbolic style) reports this as an offense:

C: [Correctable] RSpecRails/HttpStatus: Prefer nil over "some-string" to describe HTTP status code. (https://www.rubydoc.info/gems/rubocop-rspec_rails/RuboCop/Cop/RSpecRails/HttpStatus)
    it { expect(response).to have_http_status("some-string") }

Autocorrection looks like have_http_status(nil) which leads to runtime error.

I expect the cop to ignore unexpected objects passed as http status code.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@viralpraxis viralpraxis requested a review from a team as a code owner October 2, 2024 16:02
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

I think we still need to register an offence, but with a different message, that doesn’t mention nil, and do not autocorrect.

How does this spec work before correction?

@viralpraxis
Copy link
Contributor Author

I think we still need to register an offence, but with a different message, that doesn’t mention nil, and do not autocorrect.

I agree, it seems to be even better.

How does this spec work before correction?

expect_offense(<<~RUBY)
  it { is_expected.to have_http_status("some-custom-string") }
                                        ^^^^^^^^^^^^^^^^^^^^ Prefer `nil` over `"some-custom-string"` to describe HTTP status code.
RUBY

@pirj
Copy link
Member

pirj commented Oct 2, 2024

I mean how does the have_http_status("some-custom-string") work, will it ever match?

@viralpraxis
Copy link
Contributor Author

Ah, I got you. That example is artificial. I have a project where have_http_status is extended/patched to work with some specifial values (don't ask why), so it works. I understand that rubocop-rspec_rails should expect that this matcher only works with symbols/strings/integers, but the error message (and, especially, autocorrection) still seems to be wrong

@pirj
Copy link
Member

pirj commented Oct 2, 2024

Understood. Would it fix your case to still raise an offence but don’t autocorrect unknown string arguments?

@viralpraxis
Copy link
Contributor Author

Yep. How should the message for this case look like?

@viralpraxis viralpraxis force-pushed the fix-http-status-cop-to-ignore-unexpected-status-codes branch 2 times, most recently from c5158ba to 8ea4ef0 Compare October 2, 2024 20:56
@viralpraxis viralpraxis changed the title Fix RSpecRails::HttpStatus cop to ignore unexpected objects Handle unknown HTTP status codes for RSpecRails::HttpStatus cop Oct 2, 2024
@viralpraxis
Copy link
Contributor Author

Actually the issue may happen with any EnforcedStyle configured; fixed it (with message Unknown status code. and autocorrection disabled).

@viralpraxis
Copy link
Contributor Author

Could you please take a look? @pirj

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Perfect, thank you!

@pirj pirj requested a review from a team October 8, 2024 15:29
Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

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

Could you please add your comments and Bad case about this case?

# Enforces use of symbolic or numeric value to describe HTTP status.
#
# This cop inspects only `have_http_status` calls.
# So, this cop does not check if a method starting with `be_*` is used
# when setting for `EnforcedStyle: symbolic` or
# `EnforcedStyle: numeric`.
#
# @example `EnforcedStyle: symbolic` (default)
# # bad
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status :forbidden }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: numeric`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status "forbidden" }
#
# # good
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status 403 }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: be_status`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status "forbidden" }
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to be_ok }
# it { is_expected.to be_not_found }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#

@viralpraxis viralpraxis force-pushed the fix-http-status-cop-to-ignore-unexpected-status-codes branch from 8ea4ef0 to e741eab Compare October 8, 2024 17:59
@viralpraxis
Copy link
Contributor Author

Could you please add your comments and Bad case about this case?

# Enforces use of symbolic or numeric value to describe HTTP status.
#
# This cop inspects only `have_http_status` calls.
# So, this cop does not check if a method starting with `be_*` is used
# when setting for `EnforcedStyle: symbolic` or
# `EnforcedStyle: numeric`.
#
# @example `EnforcedStyle: symbolic` (default)
# # bad
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status :forbidden }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: numeric`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status "forbidden" }
#
# # good
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status 403 }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#
# @example `EnforcedStyle: be_status`
# # bad
# it { is_expected.to have_http_status :ok }
# it { is_expected.to have_http_status :not_found }
# it { is_expected.to have_http_status "forbidden" }
# it { is_expected.to have_http_status 200 }
# it { is_expected.to have_http_status 404 }
# it { is_expected.to have_http_status "403" }
#
# # good
# it { is_expected.to be_ok }
# it { is_expected.to be_not_found }
# it { is_expected.to have_http_status :success }
# it { is_expected.to have_http_status :error }
#

Done.

@viralpraxis
Copy link
Contributor Author

@ydah Could you please take a look? 🙂 I updated cop's docs

Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

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

Looks good!

@ydah ydah merged commit 71d49a0 into rubocop:master Oct 14, 2024
25 checks passed
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.

3 participants