Rubocop-rspec: Rails/HttpStatus not working after requiring rubocop-rspec

Created on 14 May 2018  路  15Comments  路  Source: rubocop-hq/rubocop-rspec

After adding require: rubocop-rspec to the .rubocop.yml, the Rails/HttpStatus doesn't check the code, only the specs

first reported here: https://github.com/bbatsov/rubocop/issues/5738#issuecomment-388838142

rubocop-rspec 1.25.1

Most helpful comment

@pirj opened a PR with renaming (#834).

All 15 comments

I think there is a conflict of cops name between rubocop and rubocop-rspec as Rails/HttpStatus is defined in both gems with the same name and rubocop-rspec might override folders to inspect.

I'm not sure how we can fix that 馃

could we rename Rails/HttpStatus to RSpec/Rails/HttpStatus?

Since this is still a problem and it's hard to find out how to fix this maybe it would be better to rename RSpec/Rails/HttpStatus to RSpec/StatusHttp? Yes, it might be ugly but at least this won't have any conflicts...

Any news/plans on fixing this?

Right now I have to switch orders of requires to check both cops worked...

I have fixed this issue locally with following hacks:

# file: rubocop/fixes/http_status.rb
# frozen_string_literal: true

require "rubocop/cop/rails/http_status"
require "rubocop/cop/rspec/rails/http_status"

RuboCop::Cop::Cop.registry.instance_eval do
  [
    RuboCop::Cop::Rails::HttpStatus,
    RuboCop::Cop::RSpec::Rails::HttpStatus
  ].each do |cop|
    @registry.delete cop.badge
    @departments[cop.department].delete cop
    @cops_by_cop_name[cop.cop_name].delete cop
  end
end

module RuboCop
  module Cop
    module RSpec
      module Rails
        class HttpStatus < Cop
          def self.badge
            *departament, cop_name = name.split("::").last(3)
            Badge.new(departament.join("/"), cop_name)
          end
        end
      end
    end
  end
end

[RuboCop::Cop::Rails::HttpStatus, RuboCop::Cop::RSpec::Rails::HttpStatus]
  .each { |cop| RuboCop::Cop::Cop.registry.enlist cop }

# file: rubocop/fixes/http_status.yml

RSpec/Rails/HttpStatus:
  Description: Enforces use of symbolic or numeric value to describe HTTP status.
  Enabled: true
  EnforcedStyle: symbolic
  SupportedStyles:
  - numeric
  - symbolic
  StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Rails/HttpStatus

# file: .rubocop.yml

require:
  - ./rubocop/fixes/http_status.rb

inherit:
  - ./rubocop/fixes/http_status.yml

@ixti the code required to support sub-departments looks non-trivial, I'm not sure if it will have a warm welcome in RuboCop.

WDYT of renaming this cop to RSpecRails/HttpStatus? have_http_status is an RSpec-Rails' matcher anyway.
This naming seems to be more consistent with other departments (Capybara, FactoryBot).

@pirj Yeah, I thought that it won't be welcomed :D It was more like a "what about" example to have some input ideas on. And I totally love your renaming idea.

@pirj opened a PR with renaming (#834).

Did someone end up opening an issue to fix this on the RuboCop side (https://github.com/rubocop-hq/rubocop-rspec/pull/834#issuecomment-570497415)? I can't find one.

@haines Unfortunately not. Would you like to handle this?
References:
https://github.com/rubocop-hq/rubocop-rspec/pull/834#issuecomment-570497415
https://github.com/rubocop-hq/rubocop/issues/5251

Thanks @haines !

This should be fixed in #1019 that is part of the release 2.0.

Was this page helpful?
0 / 5 - 0 ratings