diff options
Diffstat (limited to 'spec/initializers')
-rw-r--r-- | spec/initializers/00_connection_logger_spec.rb | 39 | ||||
-rw-r--r-- | spec/initializers/0_log_deprecations_spec.rb | 18 | ||||
-rw-r--r-- | spec/initializers/100_patch_omniauth_oauth2_spec.rb | 2 | ||||
-rw-r--r-- | spec/initializers/1_acts_as_taggable_spec.rb | 64 | ||||
-rw-r--r-- | spec/initializers/enumerator_next_patch_spec.rb | 167 | ||||
-rw-r--r-- | spec/initializers/set_active_support_hash_digest_class_spec.rb | 9 |
6 files changed, 245 insertions, 54 deletions
diff --git a/spec/initializers/00_connection_logger_spec.rb b/spec/initializers/00_connection_logger_spec.rb deleted file mode 100644 index 8b288b463c4..00000000000 --- a/spec/initializers/00_connection_logger_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter do # rubocop:disable RSpec/FilePath - before do - allow(PG).to receive(:connect) - end - - let(:conn_params) { PG::Connection.conndefaults_hash } - - context 'when warn_on_new_connection is enabled' do - before do - described_class.warn_on_new_connection = true - end - - it 'warns on new connection' do - expect(ActiveSupport::Deprecation) - .to receive(:warn).with(/Database connection should not be called during initializers/, anything) - - expect(PG).to receive(:connect).with(conn_params) - - described_class.new_client(conn_params) - end - end - - context 'when warn_on_new_connection is disabled' do - before do - described_class.warn_on_new_connection = false - end - - it 'does not warn on new connection' do - expect(ActiveSupport::Deprecation).not_to receive(:warn) - expect(PG).to receive(:connect).with(conn_params) - - described_class.new_client(conn_params) - end - end -end diff --git a/spec/initializers/0_log_deprecations_spec.rb b/spec/initializers/0_log_deprecations_spec.rb index 35bceb2f132..f5065126eaf 100644 --- a/spec/initializers/0_log_deprecations_spec.rb +++ b/spec/initializers/0_log_deprecations_spec.rb @@ -3,6 +3,10 @@ require 'spec_helper' RSpec.describe '0_log_deprecations' do + def setup_other_deprecations + Warning.process(__FILE__) { :default } + end + def load_initializer load Rails.root.join('config/initializers/0_log_deprecations.rb') end @@ -11,16 +15,20 @@ RSpec.describe '0_log_deprecations' do before do stub_env('GITLAB_LOG_DEPRECATIONS', env_var) + setup_other_deprecations load_initializer end after do - # reset state changed by initializer - Warning.clear ActiveSupport::Notifications.unsubscribe('deprecation.rails') end - context 'for Ruby deprecations' do + around do |example| + # reset state changed by initializer + Warning.clear(&example) + end + + describe 'Ruby deprecations' do context 'when catching deprecations through Kernel#warn' do it 'also logs them to deprecation logger' do expect(Gitlab::DeprecationJsonLogger).to receive(:info).with( @@ -32,7 +40,7 @@ RSpec.describe '0_log_deprecations' do end end - context 'for other messages from Kernel#warn' do + describe 'other messages from Kernel#warn' do it 'does not log them to deprecation logger' do expect(Gitlab::DeprecationJsonLogger).not_to receive(:info) @@ -51,7 +59,7 @@ RSpec.describe '0_log_deprecations' do end end - context 'for Rails deprecations' do + describe 'Rails deprecations' do it 'logs them to deprecation logger' do expect(Gitlab::DeprecationJsonLogger).to receive(:info).with( message: match(/^DEPRECATION WARNING: ABC will be removed/), diff --git a/spec/initializers/100_patch_omniauth_oauth2_spec.rb b/spec/initializers/100_patch_omniauth_oauth2_spec.rb index c30a1cdeafa..36a14816b7e 100644 --- a/spec/initializers/100_patch_omniauth_oauth2_spec.rb +++ b/spec/initializers/100_patch_omniauth_oauth2_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe 'OmniAuth::Strategies::OAuth2' do it 'verifies the gem version' do current_version = OmniAuth::OAuth2::VERSION - expected_version = '1.7.2' + expected_version = '1.7.3' expect(current_version).to eq(expected_version), <<~EOF New version #{current_version} of the `omniauth-oauth2` gem detected! diff --git a/spec/initializers/1_acts_as_taggable_spec.rb b/spec/initializers/1_acts_as_taggable_spec.rb new file mode 100644 index 00000000000..f9ccc9718d5 --- /dev/null +++ b/spec/initializers/1_acts_as_taggable_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'ActsAsTaggableOn::Tag' do + describe '.find_or_create_all_with_like_by_name' do + let(:tags) { %w[tag] } + + subject(:find_or_create) { ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tags) } + + it 'creates a tag' do + expect { find_or_create }.to change(ActsAsTaggableOn::Tag, :count).by(1) + end + + it 'returns the Tag record' do + results = find_or_create + + expect(results.size).to eq(1) + expect(results.first).to be_an_instance_of(ActsAsTaggableOn::Tag) + expect(results.first.name).to eq('tag') + end + + context 'some tags already existing' do + let(:tags) { %w[tag preexisting_tag tag2] } + + before_all do + ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag') + end + + it 'creates only the missing tag' do + expect(ActsAsTaggableOn::Tag).to receive(:insert_all) + .with([{ name: 'tag' }, { name: 'tag2' }], unique_by: :name) + .and_call_original + + expect { find_or_create }.to change(ActsAsTaggableOn::Tag, :count).by(2) + end + + it 'returns the Tag records' do + results = find_or_create + + expect(results.map(&:name)).to match_array(tags) + end + end + + context 'all tags already existing' do + let(:tags) { %w[preexisting_tag preexisting_tag2] } + + before_all do + ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag') + ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag2') + end + + it 'does not create new tags' do + expect { find_or_create }.not_to change(ActsAsTaggableOn::Tag, :count) + end + + it 'returns the Tag records' do + results = find_or_create + + expect(results.map(&:name)).to match_array(tags) + end + end + end +end diff --git a/spec/initializers/enumerator_next_patch_spec.rb b/spec/initializers/enumerator_next_patch_spec.rb new file mode 100644 index 00000000000..99e73af5e86 --- /dev/null +++ b/spec/initializers/enumerator_next_patch_spec.rb @@ -0,0 +1,167 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Enumerator#next patch fix' do + describe 'Enumerator' do + RSpec::Matchers.define :contain_unique_method_calls_in_order do |expected| + attr_reader :actual + + match do |actual| + @actual_err = actual + regexps = expected.map { |method_name| { name: method_name, regexp: make_regexp(method_name) } } + @actual = actual.backtrace.filter_map do |line| + regexp = regexps.find { |r| r[:regexp].match? line } + + regexp[:name] if regexp + end + + expected == @actual + end + + diffable + + failure_message do + "#{super()}\n\nFull error backtrace:\n #{@actual_err.backtrace.join("\n ")}" + end + + private + + def make_regexp(method_name) + Regexp.new("/spec/initializers/enumerator_next_patch_spec\\.rb:[0-9]+:in `#{method_name}'$") + end + end + + def have_been_raised_by_next_and_not_fixed_up + contain_unique_method_calls_in_order %w(call_enum_method) + end + + def have_been_raised_by_enum_object_and_fixed_up + contain_unique_method_calls_in_order %w(make_error call_enum_method) + end + + def have_been_raised_by_nested_next_and_fixed_up + contain_unique_method_calls_in_order %w(call_nested_next call_enum_method) + end + + methods = [ + { + name: 'next', + expected_value: 'Test value' + }, + { + name: 'next_values', + expected_value: ['Test value'] + }, + { + name: 'peek', + expected_value: 'Test value' + }, + { + name: 'peek_values', + expected_value: ['Test value'] + } + ] + + methods.each do |method| + describe "##{method[:name]}" do + def call_enum_method + enumerator.send(method_name) + end + + let(:method_name) { method[:name] } + + subject { call_enum_method } + + describe 'normal yield' do + let(:enumerator) { Enumerator.new { |yielder| yielder << 'Test value' } } + + it 'returns yielded value' do + is_expected.to eq(method[:expected_value]) + end + end + + describe 'end of iteration' do + let(:enumerator) { Enumerator.new { |_| } } + + it 'does not fix up StopIteration' do + expect { subject }.to raise_error do |err| + expect(err).to be_a(StopIteration) + expect(err).to have_been_raised_by_next_and_not_fixed_up + end + end + + context 'nested enum object' do + def call_nested_next + nested_enumerator.next + end + + let(:nested_enumerator) { Enumerator.new { |_| } } + let(:enumerator) { Enumerator.new { |yielder| yielder << call_nested_next } } + + it 'fixes up StopIteration thrown by another instance of #next' do + expect { subject }.to raise_error do |err| + expect(err).to be_a(StopIteration) + expect(err).to have_been_raised_by_nested_next_and_fixed_up + end + end + end + end + + describe 'arguments error' do + def call_enum_method + enumerator.send(method_name, 'extra argument') + end + + let(:enumerator) { Enumerator.new { |_| } } + + it 'does not fix up ArgumentError' do + expect { subject }.to raise_error do |err| + expect(err).to be_a(ArgumentError) + expect(err).to have_been_raised_by_next_and_not_fixed_up + end + end + end + + describe 'error' do + let(:enumerator) { Enumerator.new { |_| raise error } } + let(:error) { make_error } + + it 'fixes up StopIteration' do + def make_error + StopIteration.new.tap { |err| err.set_backtrace(caller) } + end + + expect { subject }.to raise_error do |err| + expect(err).to be(error) + expect(err).to have_been_raised_by_enum_object_and_fixed_up + end + end + + it 'fixes up ArgumentError' do + def make_error + ArgumentError.new.tap { |err| err.set_backtrace(caller) } + end + + expect { subject }.to raise_error do |err| + expect(err).to be(error) + expect(err).to have_been_raised_by_enum_object_and_fixed_up + end + end + + it 'adds backtrace from other errors' do + def make_error + StandardError.new('This is a test').tap { |err| err.set_backtrace(caller) } + end + + expect { subject }.to raise_error do |err| + expect(err).to be(error) + expect(err).to have_been_raised_by_enum_object_and_fixed_up + expect(err.message).to eq('This is a test') + end + end + end + end + end + end +end diff --git a/spec/initializers/set_active_support_hash_digest_class_spec.rb b/spec/initializers/set_active_support_hash_digest_class_spec.rb deleted file mode 100644 index 256e8a1f218..00000000000 --- a/spec/initializers/set_active_support_hash_digest_class_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe 'setting ActiveSupport::Digest.hash_digest_class' do - it 'sets overrides config.active_support.hash_digest_class' do - expect(ActiveSupport::Digest.hash_digest_class).to eq(Gitlab::HashDigest::Facade) - end -end |