Commit b3cf3d5a authored by Kerri Miller's avatar Kerri Miller

Replace delegated predicate methods with boolean-returning methods

Predicate methods should return a boolean value, however when we
delegate with allow_nil to properly handle non-extant relations, we
end up with true, false, and nil as possible responses. While we
generally are loose about nil being equivalent to false in Ruby, I
feel like a predicate method is a special signal that we very much
care about the boolean nature of its response.
parent 7dc02ae2
......@@ -197,10 +197,6 @@ module EE
delegate :ci_minutes_quota, to: :shared_runners_limit_namespace
delegate :last_update_succeeded?, :last_update_failed?,
:ever_updated_successfully?, :hard_failed?,
to: :import_state, prefix: :mirror, allow_nil: true
delegate :merge_pipelines_enabled, :merge_pipelines_enabled=, :merge_pipelines_enabled?, :merge_pipelines_were_disabled?, to: :ci_cd_settings
delegate :merge_trains_enabled, :merge_trains_enabled=, :merge_trains_enabled?, to: :ci_cd_settings
......@@ -256,6 +252,22 @@ module EE
end
end
def mirror_last_update_succeeded?
!!import_state&.last_update_succeeded?
end
def mirror_last_update_failed?
!!import_state&.last_update_failed?
end
def mirror_ever_updated_successfully?
!!import_state&.ever_updated_successfully?
end
def mirror_hard_failed?
!!import_state&.hard_failed?
end
class_methods do
extend ::Gitlab::Utils::Override
......
......@@ -125,6 +125,46 @@ RSpec.describe Project do
end
end
context 'import_state dependant predicate method' do
shared_examples 'returns expected values' do
context 'when project lacks a import_state relation' do
it 'returns false' do
expect(project.send("mirror_#{method}")).to be_falsey
end
end
context 'when project has a import_state relation' do
before do
create(:import_state, project: project)
end
it 'accesses the value from the import_state' do
expect(project.import_state).to receive(method)
project.send("mirror_#{method}")
end
end
end
describe '#mirror_last_update_succeeded?' do
it_behaves_like 'returns expected values' do
let(:method) { "last_update_succeeded?" }
end
end
describe '#mirror_last_update_failed?' do
it_behaves_like 'returns expected values' do
let(:method) { "last_update_failed?" }
end
end
describe '#mirror_ever_updated_successfully?' do
it_behaves_like 'returns expected values' do
let(:method) { "ever_updated_successfully?" }
end
end
end
describe 'approval_rules association' do
let_it_be(:rule, reload: true) { create(:approval_project_rule) }
let(:project) { rule.project }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment