debian-mirror-gitlab/spec/initializers/0_log_deprecations_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
3.6 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe '0_log_deprecations' do
2022-08-13 15:12:31 +05:30
def setup_other_deprecations
Warning.process(__FILE__) { :default }
end
2021-10-27 15:23:28 +05:30
def load_initializer
load Rails.root.join('config/initializers/0_log_deprecations.rb')
end
2022-08-27 11:52:29 +05:30
def with_deprecation_behavior
behavior = ActiveSupport::Deprecation.behavior
ActiveSupport::Deprecation.behavior = deprecation_behavior
yield
ensure
ActiveSupport::Deprecation.behavior = behavior
end
let(:deprecation_behavior) { :stderr }
2021-10-27 15:23:28 +05:30
let(:env_var) { '1' }
before do
stub_env('GITLAB_LOG_DEPRECATIONS', env_var)
2022-08-13 15:12:31 +05:30
setup_other_deprecations
2021-10-27 15:23:28 +05:30
load_initializer
end
after do
ActiveSupport::Notifications.unsubscribe('deprecation.rails')
end
2022-08-13 15:12:31 +05:30
around do |example|
2022-08-27 11:52:29 +05:30
with_deprecation_behavior do
# reset state changed by initializer
Warning.clear(&example)
end
2022-08-13 15:12:31 +05:30
end
describe 'Ruby deprecations' do
2022-08-27 11:52:29 +05:30
shared_examples 'deprecation logger' do
it 'logs them to deprecation logger once and to stderr' do
2021-10-27 15:23:28 +05:30
expect(Gitlab::DeprecationJsonLogger).to receive(:info).with(
message: 'ABC gem is deprecated',
source: 'ruby'
)
2022-08-27 11:52:29 +05:30
expect { subject }.to output.to_stderr
end
end
context 'when catching deprecations through Kernel#warn' do
subject { warn('ABC gem is deprecated') }
include_examples 'deprecation logger'
context 'with non-notify deprecation behavior' do
let(:deprecation_behavior) { :silence }
include_examples 'deprecation logger'
end
context 'with notify deprecation behavior' do
let(:deprecation_behavior) { :notify }
include_examples 'deprecation logger'
2021-10-27 15:23:28 +05:30
end
end
2022-08-13 15:12:31 +05:30
describe 'other messages from Kernel#warn' do
2021-10-27 15:23:28 +05:30
it 'does not log them to deprecation logger' do
expect(Gitlab::DeprecationJsonLogger).not_to receive(:info)
expect { warn('Sure is hot today') }.to output.to_stderr
end
end
context 'when disabled via environment' do
let(:env_var) { '0' }
it 'does not log them to deprecation logger' do
expect(Gitlab::DeprecationJsonLogger).not_to receive(:info)
expect { warn('ABC gem is deprecated') }.to output.to_stderr
end
end
end
2022-08-13 15:12:31 +05:30
describe 'Rails deprecations' do
2022-08-27 11:52:29 +05:30
subject { ActiveSupport::Deprecation.warn('ABC will be removed') }
shared_examples 'deprecation logger' do
it 'logs them to deprecation logger once' do
expect(Gitlab::DeprecationJsonLogger).to receive(:info).with(
message: match(/^DEPRECATION WARNING: ABC will be removed/),
source: 'rails'
)
subject
end
end
context 'with non-notify deprecation behavior' do
let(:deprecation_behavior) { :silence }
include_examples 'deprecation logger'
end
context 'with notify deprecation behavior' do
let(:deprecation_behavior) { :notify }
include_examples 'deprecation logger'
end
context 'when deprecations were silenced' do
around do |example|
silenced = ActiveSupport::Deprecation.silenced
ActiveSupport::Deprecation.silenced = true
example.run
ActiveSupport::Deprecation.silenced = silenced
end
2021-10-27 15:23:28 +05:30
2022-08-27 11:52:29 +05:30
include_examples 'deprecation logger'
2021-10-27 15:23:28 +05:30
end
context 'when disabled via environment' do
let(:env_var) { '0' }
it 'does not log them to deprecation logger' do
expect(Gitlab::DeprecationJsonLogger).not_to receive(:info)
expect { ActiveSupport::Deprecation.warn('ABC will be removed') }.to output.to_stderr
end
end
end
end