Skip to content

Commit

Permalink
Merge pull request #207 from r7kamura/status
Browse files Browse the repository at this point in the history
Add `RSpecRailsStatusCodeCheckBySubject` cop
  • Loading branch information
r7kamura authored May 28, 2024
2 parents 03ed2b9 + a0f539f commit 23a10cd
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ RSpec/FilePath:
RSpecMatcherConsistentParentheses: rspec_matcher_consistent_parentheses
RSpecMemoizedHelperBlockDelimiter: rspec_memoized_helper_block_delimiter
RSpecRailsHaveHttpStatus: rspec_rails_have_http_status
RSpecRailsStatusCodeCheckBySubject: rspec_rails_status_code_check_by_subject

RSpec/MultipleExpectations:
Enabled: false
Expand All @@ -54,6 +55,7 @@ RSpec/SpecFilePathFormat:
RSpecMatcherConsistentParentheses: rspec_matcher_consistent_parentheses
RSpecMemoizedHelperBlockDelimiter: rspec_memoized_helper_block_delimiter
RSpecRailsHaveHttpStatus: rspec_rails_have_http_status
RSpecRailsStatusCodeCheckBySubject: rspec_rails_status_code_check_by_subject

Sevencop/AutoloadOrdered:
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Note that all cops are `Enabled: false` by default.
- [Sevencop/RSpecMatcherConsistentParentheses](lib/rubocop/cop/sevencop/rspec_matcher_consistent_parentheses.rb)
- [Sevencop/RSpecMemoizedHelperBlockDelimiter](lib/rubocop/cop/sevencop/rspec_memoized_helper_block_delimiter.rb)
- [Sevencop/RSpecRailsHaveHttpStatus](lib/rubocop/cop/sevencop/rspec_rails_have_http_status.rb)
- [Sevencop/RSpecRailsStatusCodeCheckBySubject](lib/rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject.rb)

## Notes

Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,11 @@ Sevencop/RSpecRailsHaveHttpStatus:
Include:
- "**/spec/controllers/**/*.rb"
- "**/spec/requests/**/*.rb"

Sevencop/RSpecRailsStatusCodeCheckBySubject:
Description: |
Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.
Enabled: false
Include:
- "**/spec/controllers/**/*.rb"
- "**/spec/requests/**/*.rb"
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Sevencop
# Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.
#
# @example
# # bad
# is_expected.to eq(200)
#
# # good
# expect(response).to have_http_status(200)
#
# # bad
# is_expected.to eq(:ok)
#
# # good
# expect(response).to have_http_status(:ok)
class RSpecRailsStatusCodeCheckBySubject < Base
extend AutoCorrector

MSG = 'Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.'

RESTRICT_ON_SEND = %i[
to
].freeze

# @param [RuboCop::AST::SendNode] node
# @return [void]
def on_send(node)
return unless is_expected_to_eq_code?(node)

add_offense(node) do |corrector|
corrector.insert_before(node, "subject\n")
corrector.replace(node.receiver, 'expect(response)')
corrector.replace(node.first_argument.location.selector, 'have_http_status')
end
end

private

# @!method is_expected_to_eq_code?(node)
# @param [RuboCop::AST::SendNode] node
# @return [Boolean]
def_node_matcher :is_expected_to_eq_code?, <<~PATTERN
(send (send nil? :is_expected) :to (send nil? :eq {int sym}))
PATTERN
end
end
end
end
1 change: 1 addition & 0 deletions lib/sevencop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
require_relative 'rubocop/cop/sevencop/rspec_matcher_consistent_parentheses'
require_relative 'rubocop/cop/sevencop/rspec_memoized_helper_block_delimiter'
require_relative 'rubocop/cop/sevencop/rspec_rails_have_http_status'
require_relative 'rubocop/cop/sevencop/rspec_rails_status_code_check_by_subject'
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RuboCop::Cop::Sevencop::RSpecRailsStatusCodeCheckBySubject, :config do
context 'when `is_expected.to redirect_to(root_path)` is used' do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
is_expected.to redirect_to(root_path)
RUBY
end
end

context 'when `is_expected.to eq(200)` is used' do
it 'registers offense' do
expect_offense(<<~RUBY)
is_expected.to eq(200)
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.
RUBY

expect_correction(<<~RUBY)
subject
expect(response).to have_http_status(200)
RUBY
end
end

context 'when `is_expected.to eq(:ok)` is used' do
it 'registers offense' do
expect_offense(<<~RUBY)
is_expected.to eq(:ok)
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(response).to have_http_status(code)` instead of `is_expected.to eq(code)`.
RUBY

expect_correction(<<~RUBY)
subject
expect(response).to have_http_status(:ok)
RUBY
end
end
end

0 comments on commit 23a10cd

Please sign in to comment.