summaryrefslogtreecommitdiff
path: root/spec/models
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2016-03-18 12:28:43 +0000
committerDouwe Maan <douwe@gitlab.com>2016-03-18 12:28:43 +0000
commitdadd28e317ace1e3d3a2a02926eb352832b97f08 (patch)
treeeaa4513cbb34185b1ff53a47a38ea91476f23ec3 /spec/models
parentf4b6a89252e4f3c12b1c091543eb811f405bc129 (diff)
parent18295585d9d2a7177f52fc451db7b0d542cc49b5 (diff)
downloadgitlab-ce-dadd28e317ace1e3d3a2a02926eb352832b97f08.tar.gz
Merge branch 'fix-mr-source-sha' into 'master'
Fix MergeRequest#source_sha when there is no diff Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/14170 ### Overview This MR fixes an unhandled Exception when visiting the page of an open Merge Request without diff. ### Description `MergeRequest#source_sha` is expected to return the sha of the source branch last commit. But when an open Merge Request has no diff (e.g. all commits have already been merged to the target branch), `merge_request.source_sha` incorrectly returns `nil`. This was without consequences before – but since !2217 was merged (a few days ago), it makes `Gitlab::Git::Commit.between` raise an "Unexpected nil argument" exception. This can be reproduced when visiting the http://localhost:3000/gitlab-org/gitlab-test/merge_requests/2 page on a fresh local Gitlab setup. This MR fixes the crash, by making sure that `source_sha` returns a correct result even when there is no diff available. I also added tests. @DouweM I believe you wrote most of this code in the first place ; does this looks correct to you, or is there a better way to resolve this issue maybe? See merge request !3135
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/merge_request_spec.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 633a16b59c2..654c71b6825 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -86,6 +86,31 @@ describe MergeRequest, models: true do
end
end
+ describe '#source_sha' do
+ let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) }
+
+ context 'with diffs' do
+ subject { create(:merge_request, :with_diffs) }
+ it 'returns the sha of the source branch last commit' do
+ expect(subject.source_sha).to eq(last_branch_commit.sha)
+ end
+ end
+
+ context 'without diffs' do
+ subject { create(:merge_request, :without_diffs) }
+ it 'returns the sha of the source branch last commit' do
+ expect(subject.source_sha).to eq(last_branch_commit.sha)
+ end
+ end
+
+ context 'when the merge request is being created' do
+ subject { build(:merge_request, source_branch: nil, compare_commits: []) }
+ it 'returns nil' do
+ expect(subject.source_sha).to be_nil
+ end
+ end
+ end
+
describe '#to_reference' do
it 'returns a String reference to the object' do
expect(subject.to_reference).to eq "!#{subject.iid}"