Commit 995d2d13 authored by Shinya Maeda's avatar Shinya Maeda

Simplify the spec

parent f262b8f3
...@@ -2,106 +2,93 @@ ...@@ -2,106 +2,93 @@
require 'spec_helper' require 'spec_helper'
describe Ci::ProcessBuildService, '#execute' do describe Ci::ProcessBuildService, '#execute' do
VALID_STATUSES_WHEN_ON_SUCCESS = %w[success skipped].freeze
VALID_STATUSES_WHEN_ON_FAILURE = %w[failed].freeze
VALID_STATUSES_WHEN_ALWAYS = %w[success failed skipped].freeze
VALID_STATUSES_WHEN_MANUAL = %w[success skipped].freeze
VALID_STATUSES_WHEN_DELAYED = %w[success skipped].freeze
SKIPPABLE_STATUES = %w[created pending].freeze
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project) }
subject { described_class.new(project, user).execute(build, status_for_prior_stages) } subject { described_class.new(project, user).execute(build, current_status) }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
end end
shared_examples_for 'Change the build status' do |when_option: nil, current_statuses: nil, from_statuses:, to_status:, factory_options: nil| context 'when build has on_success option' do
current_statuses.each do |current_status| let(:build) { create(:ci_build, :created, when: :on_success, user: user, project: project) }
context "when status for prior stages is #{current_status}" do
let(:status_for_prior_stages) { current_status }
from_statuses.each do |status| context 'when current status is success' do
context "when build status is #{status}" do let(:current_status) { 'success' }
let(:build) { create(:ci_build, status.to_sym, *factory_options, when: when_option, user: user, project: project) }
it 'changes the build status' do it 'changes the build status' do
expect { subject }.to change { build.status }.to(to_status) expect { subject }.to change { build.status }.to('pending')
end end
end end
end
(HasStatus::AVAILABLE_STATUSES - from_statuses).each do |status| context 'when current status is failed' do
context "when build status is #{status}" do let(:current_status) { 'failed' }
let(:build) { create(:ci_build, status.to_sym, *factory_options, when: when_option, user: user, project: project) }
it 'does not change the build status' do it 'does not change the build status' do
expect { subject }.not_to change { build.status } expect { subject }.to change { build.status }.to('skipped')
end
end
end
end end
end end
end end
context 'when build has on_success option' do
it_behaves_like 'Change the build status',
when_option: :on_success,
current_statuses: VALID_STATUSES_WHEN_ON_SUCCESS,
from_statuses: %w[created skipped manual scheduled],
to_status: 'pending'
it_behaves_like 'Change the build status',
when_option: :on_success,
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ON_SUCCESS),
from_statuses: SKIPPABLE_STATUES,
to_status: 'skipped'
end
context 'when build has on_failure option' do context 'when build has on_failure option' do
it_behaves_like 'Change the build status', let(:build) { create(:ci_build, :created, when: :on_failure, user: user, project: project) }
when_option: :on_failure,
current_statuses: VALID_STATUSES_WHEN_ON_FAILURE, context 'when current status is success' do
from_statuses: %w[created skipped manual scheduled], let(:current_status) { 'success' }
to_status: 'pending'
it 'changes the build status' do
it_behaves_like 'Change the build status', expect { subject }.to change { build.status }.to('skipped')
when_option: :on_failure, end
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ON_FAILURE), end
from_statuses: SKIPPABLE_STATUES,
to_status: 'skipped' context 'when current status is failed' do
let(:current_status) { 'failed' }
it 'does not change the build status' do
expect { subject }.to change { build.status }.to('pending')
end
end
end end
context 'when build has always option' do context 'when build has always option' do
it_behaves_like 'Change the build status', let(:build) { create(:ci_build, :created, when: :always, user: user, project: project) }
when_option: :always,
current_statuses: VALID_STATUSES_WHEN_ALWAYS, context 'when current status is success' do
from_statuses: %w[created skipped manual scheduled], let(:current_status) { 'success' }
to_status: 'pending'
it 'changes the build status' do
it_behaves_like 'Change the build status', expect { subject }.to change { build.status }.to('pending')
when_option: :always, end
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ALWAYS), end
from_statuses: SKIPPABLE_STATUES,
to_status: 'skipped' context 'when current status is failed' do
let(:current_status) { 'failed' }
it 'does not change the build status' do
expect { subject }.to change { build.status }.to('pending')
end
end
end end
context 'when build has manual option' do context 'when build has manual option' do
it_behaves_like 'Change the build status', let(:build) { create(:ci_build, :created, :actionable, user: user, project: project) }
when_option: :manual,
current_statuses: VALID_STATUSES_WHEN_MANUAL, context 'when current status is success' do
from_statuses: %w[created], let(:current_status) { 'success' }
to_status: 'manual',
factory_options: %i[actionable] it 'changes the build status' do
expect { subject }.to change { build.status }.to('manual')
it_behaves_like 'Change the build status', end
when_option: :manual, end
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ON_SUCCESS),
from_statuses: SKIPPABLE_STATUES, context 'when current status is failed' do
to_status: 'skipped', let(:current_status) { 'failed' }
factory_options: %i[actionable]
it 'does not change the build status' do
expect { subject }.to change { build.status }.to('skipped')
end
end
end end
context 'when build has delayed option' do context 'when build has delayed option' do
...@@ -109,44 +96,50 @@ describe Ci::ProcessBuildService, '#execute' do ...@@ -109,44 +96,50 @@ describe Ci::ProcessBuildService, '#execute' do
allow(Ci::BuildScheduleWorker).to receive(:perform_at) { } allow(Ci::BuildScheduleWorker).to receive(:perform_at) { }
end end
let(:build) { create(:ci_build, :created, :schedulable, user: user, project: project) }
context 'when ci_enable_scheduled_build is enabled' do context 'when ci_enable_scheduled_build is enabled' do
before do before do
stub_feature_flags(ci_enable_scheduled_build: true) stub_feature_flags(ci_enable_scheduled_build: true)
end end
it_behaves_like 'Change the build status', context 'when current status is success' do
when_option: :delayed, let(:current_status) { 'success' }
current_statuses: VALID_STATUSES_WHEN_DELAYED,
from_statuses: %w[created], it 'changes the build status' do
to_status: 'scheduled', expect { subject }.to change { build.status }.to('scheduled')
factory_options: %i[schedulable] end
end
it_behaves_like 'Change the build status',
when_option: :delayed, context 'when current status is failed' do
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ON_SUCCESS), let(:current_status) { 'failed' }
from_statuses: SKIPPABLE_STATUES,
to_status: 'skipped', it 'does not change the build status' do
factory_options: %i[schedulable] expect { subject }.to change { build.status }.to('skipped')
end
end
end end
context 'when ci_enable_scheduled_build is enabled' do context 'when ci_enable_scheduled_build is disabled' do
before do before do
stub_feature_flags(ci_enable_scheduled_build: false) stub_feature_flags(ci_enable_scheduled_build: false)
end end
it_behaves_like 'Change the build status', context 'when current status is success' do
when_option: :delayed, let(:current_status) { 'success' }
current_statuses: VALID_STATUSES_WHEN_DELAYED,
from_statuses: %w[created], it 'changes the build status' do
to_status: 'manual', expect { subject }.to change { build.status }.to('manual')
factory_options: %i[schedulable] end
end
it_behaves_like 'Change the build status',
when_option: :delayed, context 'when current status is failed' do
current_statuses: (HasStatus::AVAILABLE_STATUSES - VALID_STATUSES_WHEN_ON_SUCCESS), let(:current_status) { 'failed' }
from_statuses: SKIPPABLE_STATUES,
to_status: 'skipped', it 'does not change the build status' do
factory_options: %i[schedulable] expect { subject }.to change { build.status }.to('skipped')
end
end
end end
end end
end end
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