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

Enable RSpec/LeakyConstantDeclaration rubocop #9348

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# https://github.com/bbatsov/rubocop/blob/master/config/disabled.yml
require:
- rubocop-rails
- rubocop-rspec
- rubocop-performance
- ./lib/linters/analytics_event_name_linter.rb
- ./lib/linters/localized_validation_message_linter.rb
Expand Down Expand Up @@ -997,6 +998,9 @@ Rails/WhereNot:
Rails/WhereNotWithMultipleConditions:
Enabled: true

RSpec/LeakyConstantDeclaration:
Enabled: true

Security/Eval:
Enabled: true

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ group :development, :test do
gem 'rubocop', '~> 1.55.1', require: false
gem 'rubocop-performance', '~> 1.18.0', require: false
gem 'rubocop-rails', '>= 2.5.2', require: false
gem 'rubocop-rspec', require: false
end

group :test do
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,21 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.19.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.24.0)
rubocop (~> 1.33)
rubocop-performance (1.18.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.20.2)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-rspec (2.24.1)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby-saml (1.13.0)
nokogiri (>= 1.10.5)
Expand Down Expand Up @@ -805,6 +813,7 @@ DEPENDENCIES
rubocop (~> 1.55.1)
rubocop-performance (~> 1.18.0)
rubocop-rails (>= 2.5.2)
rubocop-rspec
ruby-progressbar
ruby-saml
safe_target_blank (>= 1.0.2)
Expand Down
14 changes: 14 additions & 0 deletions spec/components/base_component_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'rails_helper'

RSpec.describe BaseComponent, type: :component do
# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleComponent < BaseComponent
Comment on lines +4 to 5
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to allow these instead of turn them into lets because I got errors like this

       undefined method `demodulize' for nil:NilClass

Other options

  • stub_const('ExampleComponent', ...)
  • making sure to define a self.name method on these anonymous classes

def call
''
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

let(:view_context) { vc_test_controller.view_context }

Expand All @@ -20,6 +22,7 @@ def call
end

context 'with sidecar script' do
# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleComponentWithScript < BaseComponent
def call
''
Expand All @@ -32,7 +35,9 @@ def self.sidecar_files(extensions)
files.presence || super(extensions)
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleComponentWithScriptRenderingOtherComponentWithScript < BaseComponent
def call
render(ExampleComponentWithScript.new)
Expand All @@ -46,7 +51,9 @@ def self.sidecar_files(extensions)
end
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

# rubocop:disable RSpec/LeakyConstantDeclaration
class NestedExampleComponentWithScript < ExampleComponentWithScript
def self.sidecar_files(extensions)
if extensions.include?('js')
Expand All @@ -56,6 +63,7 @@ def self.sidecar_files(extensions)
end
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

it 'adds script to class variable when rendered' do
expect(view_context).to receive(:enqueue_component_scripts).with(
Expand Down Expand Up @@ -98,6 +106,7 @@ def self.sidecar_files(extensions)
end

context 'with sidecar stylesheet' do
# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleComponentWithStylesheet < BaseComponent
def call
''
Expand All @@ -109,7 +118,9 @@ def self.sidecar_files(extensions)
files.presence || super(extensions)
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleComponentWithStylesheetRenderingOtherComponentWithStylesheet < BaseComponent
def call
render(ExampleComponentWithStylesheet.new)
Expand All @@ -123,7 +134,9 @@ def self.sidecar_files(extensions)
end
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

# rubocop:disable RSpec/LeakyConstantDeclaration
class NestedExampleComponentWithStylesheet < ExampleComponentWithStylesheet
def self.sidecar_files(extensions)
if extensions.include?('scss')
Expand All @@ -133,6 +146,7 @@ def self.sidecar_files(extensions)
end
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

it 'adds script to class variable when rendered' do
expect(view_context).to receive(:enqueue_component_stylesheets).with(
Expand Down
2 changes: 2 additions & 0 deletions spec/components/block_link_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
end

context 'with custom renderer' do
# rubocop:disable RSpec/LeakyConstantDeclaration
class ExampleBlockLinkCustomRendererComponent < BaseComponent
def initialize(href:, **)
@href = href
Expand All @@ -39,6 +40,7 @@ def call
content_tag(:button, "Example #{content.strip}", data: { href: @href })
end
end
# rubocop:enable RSpec/LeakyConstantDeclaration

it 'renders using the custom renderer' do
rendered = render_inline BlockLinkComponent.new(
Expand Down
11 changes: 3 additions & 8 deletions spec/controllers/concerns/idv/ab_test_analytics_concern_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
require 'rails_helper'

RSpec.describe 'AbTestAnalyticsConcern' do
module Idv
class StepController < ApplicationController
include AbTestAnalyticsConcern
end
end

RSpec.describe Idv::AbTestAnalyticsConcern do
let(:user) { create(:user) }
let(:idv_session) do
Idv::Session.new(user_session: subject.user_session, current_user: user, service_provider: nil)
end

describe '#ab_test_analytics_buckets' do
controller Idv::StepController do
controller(ApplicationController) do
include Idv::AbTestAnalyticsConcern
end

let(:acuant_sdk_args) { { as_bucket: :as_value } }
Expand Down
38 changes: 20 additions & 18 deletions spec/controllers/concerns/idv_step_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,43 @@
Idv::Session.new(user_session: subject.user_session, current_user: user, service_provider: nil)
end

module Idv
class StepController < ApplicationController
include IdvStepConcern
idv_step_controller_class = Class.new(ApplicationController) do
def self.name
'AnonymousController'
end

def show
render plain: 'Hello'
end
include IdvStepConcern

def show
render plain: 'Hello'
end
end

describe 'before_actions' do
it 'includes handle_fraud' do
expect(Idv::StepController).to have_actions(
expect(idv_step_controller_class).to have_actions(
:before,
:handle_fraud,
)
end

it 'includes check_for_mail_only_outage before_action' do
expect(Idv::StepController).to have_actions(
expect(idv_step_controller_class).to have_actions(
:before,
:check_for_mail_only_outage,
)
end
end

describe '#confirm_idv_needed' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_idv_needed
end

before(:each) do
sign_in(user)
routes.draw do
get 'show' => 'idv/step#show'
get 'show' => 'anonymous#show'
end
end

Expand Down Expand Up @@ -73,14 +75,14 @@ def show
end

describe '#confirm_address_step_complete' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_address_step_complete
end

before(:each) do
sign_in(user)
routes.draw do
get 'show' => 'idv/step#show'
get 'show' => 'anonymous#show'
end
end

Expand Down Expand Up @@ -131,14 +133,14 @@ def show
end

describe '#confirm_verify_info_step_complete' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_verify_info_step_complete
end

before(:each) do
sign_in(user)
routes.draw do
get 'show' => 'idv/step#show'
get 'show' => 'anonymous#show'
end
end

Expand Down Expand Up @@ -185,15 +187,15 @@ def show
end

describe '#confirm_no_pending_in_person_enrollment' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_no_pending_in_person_enrollment
end

before(:each) do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
routes.draw do
get 'show' => 'idv/step#show'
get 'show' => 'anonymous#show'
end
end

Expand Down Expand Up @@ -223,15 +225,15 @@ def show
end

describe '#confirm_no_pending_gpo_profile' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_no_pending_gpo_profile
end

before(:each) do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
routes.draw do
get 'show' => 'idv/step#show'
get 'show' => 'anonymous#show'
end
end

Expand Down
40 changes: 21 additions & 19 deletions spec/controllers/concerns/rate_limit_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@
RSpec.describe 'RateLimitConcern' do
let(:user) { create(:user, :fully_registered, email: 'old_email@example.com') }

module Idv
class StepController < ApplicationController
include RateLimitConcern
include IdvSession
idv_step_controller_class = Class.new(ApplicationController) do
def self.name
'AnonymousController'
end

def show
render plain: 'Hello'
end
include RateLimitConcern
include IdvSession

def update
render plain: 'Bye'
end
def show
render plain: 'Hello'
end

def update
render plain: 'Bye'
end
end

describe '#confirm_not_rate_limited' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_not_rate_limited
end

before(:each) do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
routes.draw do
get 'show' => 'idv/step#show'
put 'update' => 'idv/step#update'
get 'show' => 'anonymous#show'
put 'update' => 'anonymous#update'
end
end

Expand Down Expand Up @@ -100,16 +102,16 @@ def update
end

describe '#confirm_not_rate_limited_after_doc_auth' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_not_rate_limited_after_doc_auth
end

before(:each) do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
routes.draw do
get 'show' => 'idv/step#show'
put 'update' => 'idv/step#update'
get 'show' => 'anonymous#show'
put 'update' => 'anonymous#update'
end
end

Expand All @@ -132,16 +134,16 @@ def update
end

describe '#confirm_not_rate_limited_after_idv_resolution' do
controller Idv::StepController do
controller(idv_step_controller_class) do
before_action :confirm_not_rate_limited_after_idv_resolution
end

before(:each) do
sign_in(user)
allow(subject).to receive(:current_user).and_return(user)
routes.draw do
get 'show' => 'idv/step#show'
put 'update' => 'idv/step#update'
get 'show' => 'anonymous#show'
put 'update' => 'anonymous#update'
end
end

Expand Down
Loading