Fix Lint/ConstantDefinitionInBlock cop (#24763)

This commit is contained in:
Matt Jankowski 2023-05-03 04:32:30 -04:00 committed by GitHub
parent 8b636a29c6
commit 3df665fd23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 118 deletions

View file

@ -106,19 +106,6 @@ Lint/AmbiguousOperatorPrecedence:
Exclude: Exclude:
- 'config/initializers/rack_attack.rb' - 'config/initializers/rack_attack.rb'
# Configuration parameters: AllowedMethods.
# AllowedMethods: enums
Lint/ConstantDefinitionInBlock:
Exclude:
- 'spec/controllers/api/base_controller_spec.rb'
- 'spec/controllers/application_controller_spec.rb'
- 'spec/controllers/concerns/accountable_concern_spec.rb'
- 'spec/controllers/concerns/signature_verification_spec.rb'
- 'spec/lib/activitypub/adapter_spec.rb'
- 'spec/lib/connection_pool/shared_connection_pool_spec.rb'
- 'spec/lib/connection_pool/shared_timed_stack_spec.rb'
- 'spec/models/concerns/remotable_spec.rb'
# Configuration parameters: AllowComments, AllowEmptyLambdas. # Configuration parameters: AllowComments, AllowEmptyLambdas.
Lint/EmptyBlock: Lint/EmptyBlock:
Exclude: Exclude:

View file

@ -74,7 +74,11 @@ describe Api::BaseController do
end end
describe 'error handling' do describe 'error handling' do
ERRORS_WITH_CODES = { before do
routes.draw { get 'error' => 'api/base#error' }
end
{
ActiveRecord::RecordInvalid => 422, ActiveRecord::RecordInvalid => 422,
Mastodon::ValidationError => 422, Mastodon::ValidationError => 422,
ActiveRecord::RecordNotFound => 404, ActiveRecord::RecordNotFound => 404,
@ -82,13 +86,7 @@ describe Api::BaseController do
HTTP::Error => 503, HTTP::Error => 503,
OpenSSL::SSL::SSLError => 503, OpenSSL::SSL::SSLError => 503,
Mastodon::NotPermittedError => 403, Mastodon::NotPermittedError => 403,
} }.each do |error, code|
before do
routes.draw { get 'error' => 'api/base#error' }
end
ERRORS_WITH_CODES.each do |error, code|
it "Handles error class of #{error}" do it "Handles error class of #{error}" do
expect(FakeService).to receive(:new).and_raise(error) expect(FakeService).to receive(:new).and_raise(error)

View file

@ -223,14 +223,16 @@ describe ApplicationController, type: :controller do
end end
describe 'cache_collection' do describe 'cache_collection' do
class C < ApplicationController subject do
Class.new(ApplicationController) do
public :cache_collection public :cache_collection
end end
end
shared_examples 'receives :with_includes' do |fabricator, klass| shared_examples 'receives :with_includes' do |fabricator, klass|
it 'uses raw if it is not an ActiveRecord::Relation' do it 'uses raw if it is not an ActiveRecord::Relation' do
record = Fabricate(fabricator) record = Fabricate(fabricator)
expect(C.new.cache_collection([record], klass)).to eq [record] expect(subject.new.cache_collection([record], klass)).to eq [record]
end end
end end
@ -241,13 +243,13 @@ describe ApplicationController, type: :controller do
record = Fabricate(fabricator) record = Fabricate(fabricator)
relation = klass.none relation = klass.none
allow(relation).to receive(:cache_ids).and_return([record]) allow(relation).to receive(:cache_ids).and_return([record])
expect(C.new.cache_collection(relation, klass)).to eq [record] expect(subject.new.cache_collection(relation, klass)).to eq [record]
end end
end end
it 'returns raw unless class responds to :with_includes' do it 'returns raw unless class responds to :with_includes' do
raw = Object.new raw = Object.new
expect(C.new.cache_collection(raw, Object)).to eq raw expect(subject.new.cache_collection(raw, Object)).to eq raw
end end
context 'Status' do context 'Status' do

View file

@ -3,7 +3,8 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe AccountableConcern do RSpec.describe AccountableConcern do
class Hoge let(:hoge_class) do
Class.new do
include AccountableConcern include AccountableConcern
attr_reader :current_account attr_reader :current_account
@ -11,10 +12,11 @@ RSpec.describe AccountableConcern do
@current_account = current_account @current_account = current_account
end end
end end
end
let(:user) { Fabricate(:account) } let(:user) { Fabricate(:account) }
let(:target) { Fabricate(:account) } let(:target) { Fabricate(:account) }
let(:hoge) { Hoge.new(user) } let(:hoge) { hoge_class.new(user) }
describe '#log_action' do describe '#log_action' do
it 'creates Admin::ActionLog' do it 'creates Admin::ActionLog' do

View file

@ -3,7 +3,8 @@
require 'rails_helper' require 'rails_helper'
describe ApplicationController, type: :controller do describe ApplicationController, type: :controller do
class WrappedActor let(:wrapped_actor_class) do
Class.new do
attr_reader :wrapped_account attr_reader :wrapped_account
def initialize(wrapped_account) def initialize(wrapped_account)
@ -12,6 +13,7 @@ describe ApplicationController, type: :controller do
delegate :uri, :keypair, to: :wrapped_account delegate :uri, :keypair, to: :wrapped_account
end end
end
controller do controller do
include SignatureVerification include SignatureVerification
@ -93,7 +95,7 @@ describe ApplicationController, type: :controller do
end end
context 'with a valid actor that is not an Account' do context 'with a valid actor that is not an Account' do
let(:actor) { WrappedActor.new(author) } let(:actor) { wrapped_actor_class.new(author) }
before do before do
get :success get :success

View file

@ -3,20 +3,24 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe ActivityPub::Adapter do RSpec.describe ActivityPub::Adapter do
class TestObject < ActiveModelSerializers::Model before do
test_object_class = Class.new(ActiveModelSerializers::Model) do
attributes :foo attributes :foo
end end
stub_const('TestObject', test_object_class)
class TestWithBasicContextSerializer < ActivityPub::Serializer test_with_basic_context_serializer = Class.new(ActivityPub::Serializer) do
attributes :foo attributes :foo
end end
stub_const('TestWithBasicContextSerializer', test_with_basic_context_serializer)
class TestWithNamedContextSerializer < ActivityPub::Serializer test_with_named_context_serializer = Class.new(ActivityPub::Serializer) do
context :security context :security
attributes :foo attributes :foo
end end
stub_const('TestWithNamedContextSerializer', test_with_named_context_serializer)
class TestWithNestedNamedContextSerializer < ActivityPub::Serializer test_with_nested_named_context_serializer = Class.new(ActivityPub::Serializer) do
attributes :foo attributes :foo
has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer has_one :virtual_object, key: :baz, serializer: TestWithNamedContextSerializer
@ -25,13 +29,15 @@ RSpec.describe ActivityPub::Adapter do
object object
end end
end end
stub_const('TestWithNestedNamedContextSerializer', test_with_nested_named_context_serializer)
class TestWithContextExtensionSerializer < ActivityPub::Serializer test_with_context_extension_serializer = Class.new(ActivityPub::Serializer) do
context_extensions :sensitive context_extensions :sensitive
attributes :foo attributes :foo
end end
stub_const('TestWithContextExtensionSerializer', test_with_context_extension_serializer)
class TestWithNestedContextExtensionSerializer < ActivityPub::Serializer test_with_nested_context_extension_serializer = Class.new(ActivityPub::Serializer) do
context_extensions :manually_approves_followers context_extensions :manually_approves_followers
attributes :foo attributes :foo
@ -41,6 +47,8 @@ RSpec.describe ActivityPub::Adapter do
object object
end end
end end
stub_const('TestWithNestedContextExtensionSerializer', test_with_nested_context_extension_serializer)
end
describe '#serializable_hash' do describe '#serializable_hash' do
subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json } subject { ActiveModelSerializers::SerializableResource.new(TestObject.new(foo: 'bar'), serializer: serializer_class, adapter: described_class).as_json }

View file

@ -3,22 +3,24 @@
require 'rails_helper' require 'rails_helper'
describe ConnectionPool::SharedConnectionPool do describe ConnectionPool::SharedConnectionPool do
class MiniConnection subject { described_class.new(size: 5, timeout: 5) { |site| mini_connection_class.new(site) } }
let(:mini_connection_class) do
Class.new do
attr_reader :site attr_reader :site
def initialize(site) def initialize(site)
@site = site @site = site
end end
end end
end
subject { described_class.new(size: 5, timeout: 5) { |site| MiniConnection.new(site) } }
describe '#with' do describe '#with' do
it 'runs a block with a connection' do it 'runs a block with a connection' do
block_run = false block_run = false
subject.with('foo') do |connection| subject.with('foo') do |connection|
expect(connection).to be_a MiniConnection expect(connection).to be_a mini_connection_class
block_run = true block_run = true
end end

View file

@ -3,30 +3,32 @@
require 'rails_helper' require 'rails_helper'
describe ConnectionPool::SharedTimedStack do describe ConnectionPool::SharedTimedStack do
class MiniConnection subject { described_class.new(5) { |site| mini_connection_class.new(site) } }
let(:mini_connection_class) do
Class.new do
attr_reader :site attr_reader :site
def initialize(site) def initialize(site)
@site = site @site = site
end end
end end
end
subject { described_class.new(5) { |site| MiniConnection.new(site) } }
describe '#push' do describe '#push' do
it 'keeps the connection in the stack' do it 'keeps the connection in the stack' do
subject.push(MiniConnection.new('foo')) subject.push(mini_connection_class.new('foo'))
expect(subject.size).to eq 1 expect(subject.size).to eq 1
end end
end end
describe '#pop' do describe '#pop' do
it 'returns a connection' do it 'returns a connection' do
expect(subject.pop('foo')).to be_a MiniConnection expect(subject.pop('foo')).to be_a mini_connection_class
end end
it 'returns the same connection that was pushed in' do it 'returns the same connection that was pushed in' do
connection = MiniConnection.new('foo') connection = mini_connection_class.new('foo')
subject.push(connection) subject.push(connection)
expect(subject.pop('foo')).to be connection expect(subject.pop('foo')).to be connection
end end
@ -36,8 +38,8 @@ describe ConnectionPool::SharedTimedStack do
end end
it 'repurposes a connection for a different site when maximum amount is reached' do it 'repurposes a connection for a different site when maximum amount is reached' do
5.times { subject.push(MiniConnection.new('foo')) } 5.times { subject.push(mini_connection_class.new('foo')) }
expect(subject.pop('bar')).to be_a MiniConnection expect(subject.pop('bar')).to be_a mini_connection_class
end end
end end
@ -47,14 +49,14 @@ describe ConnectionPool::SharedTimedStack do
end end
it 'returns false when there are connections on the stack' do it 'returns false when there are connections on the stack' do
subject.push(MiniConnection.new('foo')) subject.push(mini_connection_class.new('foo'))
expect(subject.empty?).to be false expect(subject.empty?).to be false
end end
end end
describe '#size' do describe '#size' do
it 'returns the number of connections on the stack' do it 'returns the number of connections on the stack' do
2.times { subject.push(MiniConnection.new('foo')) } 2.times { subject.push(mini_connection_class.new('foo')) }
expect(subject.size).to eq 2 expect(subject.size).to eq 2
end end
end end

View file

@ -3,7 +3,8 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Remotable do RSpec.describe Remotable do
class Foo let(:foo_class) do
Class.new do
def initialize def initialize
@attrs = {} @attrs = {}
end end
@ -28,23 +29,21 @@ RSpec.describe Remotable do
{ hoge: nil } { hoge: nil }
end end
end end
before do
class Foo
include Remotable
remotable_attachment :hoge, 1.kilobyte
end
end end
let(:attribute_name) { "#{hoge}_remote_url".to_sym } let(:attribute_name) { "#{hoge}_remote_url".to_sym }
let(:code) { 200 } let(:code) { 200 }
let(:file) { 'filename="foo.txt"' } let(:file) { 'filename="foo.txt"' }
let(:foo) { Foo.new } let(:foo) { foo_class.new }
let(:headers) { { 'content-disposition' => file } } let(:headers) { { 'content-disposition' => file } }
let(:hoge) { :hoge } let(:hoge) { :hoge }
let(:url) { 'https://google.com' } let(:url) { 'https://google.com' }
before do
foo_class.include described_class
foo_class.remotable_attachment :hoge, 1.kilobyte
end
it 'defines a method #hoge_remote_url=' do it 'defines a method #hoge_remote_url=' do
expect(foo).to respond_to(:hoge_remote_url=) expect(foo).to respond_to(:hoge_remote_url=)
end end