Commit 1e43b047 authored by drew cimino's avatar drew cimino

Update Ci::Build#merge_request spec contexts

The current implementation of Ci::Build#merge_request isn't up to
date with how merge request pipelines currently work. We'll now use
available factories to create contexts, some of which fail because
they weren't tested before.
parent 66f4082d
...@@ -1818,64 +1818,65 @@ describe Ci::Build do ...@@ -1818,64 +1818,65 @@ describe Ci::Build do
end end
describe '#merge_request' do describe '#merge_request' do
def create_mr(build, pipeline, factory: :merge_request, created_at: Time.now) subject { pipeline.builds.take.merge_request }
create(factory, source_project: pipeline.project,
target_project: pipeline.project,
source_branch: build.ref,
created_at: created_at)
end
context 'when a MR has a reference to the pipeline' do context 'on a branch pipeline' do
before do let!(:pipeline) { create(:ci_pipeline, :with_job, project: project, ref: 'fix') }
@merge_request = create_mr(build, pipeline, factory: :merge_request)
commits = [double(id: pipeline.sha)] context 'with no merge request' do
allow(@merge_request).to receive(:commits).and_return(commits) it { is_expected.to be_nil }
allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request])
end end
it 'returns the single associated MR' do context 'with an open merge request from the same ref name' do
expect(build.merge_request.id).to eq(@merge_request.id) let!(:merge_request) { create(:merge_request, source_project: project, source_branch: 'fix') }
end
end
context 'when there is not a MR referencing the pipeline' do # If no diff exists, the pipeline commit was not part of the merge
it 'returns nil' do # request and may have simply incidentally used the same ref name.
expect(build.merge_request).to be_nil context 'without a merge request diff containing the pipeline commit' do
end it { is_expected.to be_nil }
end end
context 'when more than one MR have a reference to the pipeline' do # If the merge request was truly opened from the branch that the
before do # pipeline ran on, that head sha will be present in a diff.
@merge_request = create_mr(build, pipeline, factory: :merge_request) context 'with a merge request diff containing the pipeline commit' do
@merge_request.close! let!(:mr_diff) { create(:merge_request_diff, merge_request: merge_request) }
@merge_request2 = create_mr(build, pipeline, factory: :merge_request) let!(:mr_diff_commit) { create(:merge_request_diff_commit, sha: build.sha, merge_request_diff: mr_diff) }
commits = [double(id: pipeline.sha)] it { is_expected.to eq(merge_request) }
allow(@merge_request).to receive(:commits).and_return(commits) end
allow(@merge_request2).to receive(:commits).and_return(commits)
allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request, @merge_request2])
end end
it 'returns the first MR' do context 'with multiple open merge requests' do
expect(build.merge_request.id).to eq(@merge_request.id) let!(:merge_request) { create(:merge_request, source_project: project, source_branch: 'fix') }
let!(:mr_diff) { create(:merge_request_diff, merge_request: merge_request) }
let!(:mr_diff_commit) { create(:merge_request_diff_commit, sha: build.sha, merge_request_diff: mr_diff) }
let!(:new_merge_request) { create(:merge_request, source_project: project, source_branch: 'fix', target_branch: 'staging') }
let!(:new_mr_diff) { create(:merge_request_diff, merge_request: new_merge_request) }
let!(:new_mr_diff_commit) { create(:merge_request_diff_commit, sha: build.sha, merge_request_diff: new_mr_diff) }
it 'returns the first merge request' do
expect(subject).to eq(merge_request)
end
end end
end end
context 'when a Build is created after the MR' do context 'on a detached merged request pipeline' do
before do let(:pipeline) { create(:ci_pipeline, :detached_merge_request_pipeline, :with_job) }
@merge_request = create_mr(build, pipeline, factory: :merge_request_with_diffs)
pipeline2 = create(:ci_pipeline, project: project)
@build2 = create(:ci_build, pipeline: pipeline2)
allow(@merge_request).to receive(:commit_shas) it { is_expected.to eq(pipeline.merge_request) }
.and_return([pipeline.sha, pipeline2.sha]) end
allow(MergeRequest).to receive_message_chain(:includes, :where, :reorder).and_return([@merge_request])
end
it 'returns the current MR' do context 'on a legacy detached merged request pipeline' do
expect(@build2.merge_request.id).to eq(@merge_request.id) let(:pipeline) { create(:ci_pipeline, :legacy_detached_merge_request_pipeline, :with_job) }
end
it { is_expected.to eq(pipeline.merge_request) }
end
context 'on a pipeline for merged results' do
let(:pipeline) { create(:ci_pipeline, :merged_result_pipeline, :with_job) }
it { is_expected.to eq(pipeline.merge_request) }
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