debian-mirror-gitlab/spec/services/merge_requests/conflicts/resolve_service_spec.rb

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

257 lines
7.7 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe MergeRequests::Conflicts::ResolveService do
2018-03-17 18:26:18 +05:30
include ProjectForksHelper
2016-09-29 09:46:39 +05:30
let(:user) { create(:user) }
2018-03-17 18:26:18 +05:30
let(:project) { create(:project, :public, :repository) }
2016-09-29 09:46:39 +05:30
2018-03-17 18:26:18 +05:30
let(:forked_project) do
fork_project_with_submodules(project, user)
2016-09-29 09:46:39 +05:30
end
let(:merge_request) do
create(:merge_request,
source_branch: 'conflict-resolvable', source_project: project,
target_branch: 'conflict-start')
end
let(:merge_request_from_fork) do
create(:merge_request,
2018-03-17 18:26:18 +05:30
source_branch: 'conflict-resolvable-fork', source_project: forked_project,
2016-09-29 09:46:39 +05:30
target_branch: 'conflict-start', target_project: project)
end
describe '#execute' do
2017-08-17 22:00:37 +05:30
let(:service) { described_class.new(merge_request) }
2017-09-10 17:25:29 +05:30
def blob_content(project, ref, path)
project.repository.blob_at(ref, path).data
end
2016-11-03 12:29:30 +05:30
context 'with section params' do
2016-09-29 09:46:39 +05:30
let(:params) do
{
2016-11-03 12:29:30 +05:30
files: [
{
old_path: 'files/ruby/popen.rb',
new_path: 'files/ruby/popen.rb',
sections: {
'2f6fcd96b88b36ce98c38da085c795a27d92a3dd_14_14' => 'head'
}
}, {
old_path: 'files/ruby/regex.rb',
new_path: 'files/ruby/regex.rb',
sections: {
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_9_9' => 'head',
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_21_21' => 'origin',
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_49_49' => 'origin'
}
}
],
2016-09-29 09:46:39 +05:30
commit_message: 'This is a commit message!'
}
end
context 'when the source and target project are the same' do
before do
2017-08-17 22:00:37 +05:30
service.execute(user, params)
2016-09-29 09:46:39 +05:30
end
it 'creates a commit with the message' do
expect(merge_request.source_branch_head.message).to eq(params[:commit_message])
end
it 'creates a commit with the correct parents' do
2017-09-10 17:25:29 +05:30
expect(merge_request.source_branch_head.parents.map(&:id))
.to eq(%w(1450cd639e0bc6721eb02800169e464f212cde06
824be604a34828eb682305f0d963056cfac87b2d))
end
end
context 'when some files have trailing newlines' do
let!(:source_head) do
branch = 'conflict-resolvable'
path = 'files/ruby/popen.rb'
popen_content = blob_content(project, branch, path)
project.repository.update_file(
user,
path,
popen_content.chomp("\n"),
message: 'Remove trailing newline from popen.rb',
branch_name: branch
)
end
before do
service.execute(user, params)
end
it 'preserves trailing newlines from our side of the conflicts' do
head_sha = merge_request.source_branch_head.sha
popen_content = blob_content(project, head_sha, 'files/ruby/popen.rb')
regex_content = blob_content(project, head_sha, 'files/ruby/regex.rb')
expect(popen_content).not_to end_with("\n")
expect(regex_content).to end_with("\n")
2016-09-29 09:46:39 +05:30
end
end
context 'when the source project is a fork and does not contain the HEAD of the target branch' do
let!(:target_head) do
2017-08-17 22:00:37 +05:30
project.repository.create_file(
user,
'new-file-in-target',
'',
message: 'Add new file in target',
branch_name: 'conflict-start')
2016-09-29 09:46:39 +05:30
end
2018-03-17 18:26:18 +05:30
subject do
2017-08-17 22:00:37 +05:30
described_class.new(merge_request_from_fork).execute(user, params)
end
2016-09-29 09:46:39 +05:30
it 'creates a commit with the message' do
2018-03-17 18:26:18 +05:30
subject
2017-08-17 22:00:37 +05:30
2016-09-29 09:46:39 +05:30
expect(merge_request_from_fork.source_branch_head.message).to eq(params[:commit_message])
end
it 'creates a commit with the correct parents' do
2018-03-17 18:26:18 +05:30
subject
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(merge_request_from_fork.source_branch_head.parents.map(&:id))
.to eq(['404fa3fc7c2c9b5dacff102f353bdf55b1be2813', target_head])
2016-09-29 09:46:39 +05:30
end
end
end
2016-11-03 12:29:30 +05:30
context 'with content and sections params' do
let(:popen_content) { "class Popen\nend" }
let(:params) do
{
files: [
{
old_path: 'files/ruby/popen.rb',
new_path: 'files/ruby/popen.rb',
content: popen_content
}, {
old_path: 'files/ruby/regex.rb',
new_path: 'files/ruby/regex.rb',
sections: {
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_9_9' => 'head',
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_21_21' => 'origin',
'6eb14e00385d2fb284765eb1cd8d420d33d63fc9_49_49' => 'origin'
}
}
],
commit_message: 'This is a commit message!'
}
end
before do
2017-08-17 22:00:37 +05:30
service.execute(user, params)
2016-11-03 12:29:30 +05:30
end
it 'creates a commit with the message' do
expect(merge_request.source_branch_head.message).to eq(params[:commit_message])
end
it 'creates a commit with the correct parents' do
2017-09-10 17:25:29 +05:30
expect(merge_request.source_branch_head.parents.map(&:id))
.to eq(%w(1450cd639e0bc6721eb02800169e464f212cde06
824be604a34828eb682305f0d963056cfac87b2d))
2016-11-03 12:29:30 +05:30
end
it 'sets the content to the content given' do
2017-09-10 17:25:29 +05:30
blob = blob_content(
merge_request.source_project,
merge_request.source_branch_head.sha,
'files/ruby/popen.rb'
)
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
expect(blob).to eq(popen_content)
2016-11-03 12:29:30 +05:30
end
end
context 'when a resolution section is missing' do
let(:invalid_params) do
{
files: [
{
old_path: 'files/ruby/popen.rb',
new_path: 'files/ruby/popen.rb',
content: ''
}, {
old_path: 'files/ruby/regex.rb',
new_path: 'files/ruby/regex.rb',
sections: { '6eb14e00385d2fb284765eb1cd8d420d33d63fc9_9_9' => 'head' }
}
],
commit_message: 'This is a commit message!'
}
end
2018-03-17 18:26:18 +05:30
it 'raises a ResolutionError error' do
2017-09-10 17:25:29 +05:30
expect { service.execute(user, invalid_params) }
2018-03-17 18:26:18 +05:30
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
2016-11-03 12:29:30 +05:30
end
end
context 'when the content of a file is unchanged' do
2018-03-17 18:26:18 +05:30
let(:resolver) do
MergeRequests::Conflicts::ListService.new(merge_request).conflicts.resolver
end
2020-10-24 23:57:45 +05:30
2018-03-17 18:26:18 +05:30
let(:regex_conflict) do
resolver.conflict_for_path(resolver.conflicts, 'files/ruby/regex.rb', 'files/ruby/regex.rb')
end
2017-08-17 22:00:37 +05:30
2016-11-03 12:29:30 +05:30
let(:invalid_params) do
{
files: [
{
old_path: 'files/ruby/popen.rb',
new_path: 'files/ruby/popen.rb',
content: ''
}, {
old_path: 'files/ruby/regex.rb',
new_path: 'files/ruby/regex.rb',
2018-03-17 18:26:18 +05:30
content: regex_conflict.content
2016-11-03 12:29:30 +05:30
}
],
commit_message: 'This is a commit message!'
}
end
2018-03-17 18:26:18 +05:30
it 'raises a ResolutionError error' do
2017-09-10 17:25:29 +05:30
expect { service.execute(user, invalid_params) }
2018-03-17 18:26:18 +05:30
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
2016-09-29 09:46:39 +05:30
end
end
2016-11-03 12:29:30 +05:30
context 'when a file is missing' do
let(:invalid_params) do
{
files: [
{
old_path: 'files/ruby/popen.rb',
new_path: 'files/ruby/popen.rb',
content: ''
}
],
commit_message: 'This is a commit message!'
}
end
2018-03-17 18:26:18 +05:30
it 'raises a ResolutionError error' do
2017-09-10 17:25:29 +05:30
expect { service.execute(user, invalid_params) }
2018-03-17 18:26:18 +05:30
.to raise_error(Gitlab::Git::Conflict::Resolver::ResolutionError)
2016-11-03 12:29:30 +05:30
end
end
2016-09-29 09:46:39 +05:30
end
end