2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe DiffsEntity do
|
2021-01-25 16:09:03 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
let(:request) { EntityRequest.new(project: project, current_user: user) }
|
|
|
|
let(:merge_request_diffs) { merge_request.merge_request_diffs }
|
2021-07-21 17:10:10 -04:00
|
|
|
let(:allow_tree_conflicts) { false }
|
2020-11-13 13:09:11 -05:00
|
|
|
let(:options) do
|
2021-07-21 17:10:10 -04:00
|
|
|
{
|
|
|
|
request: request,
|
|
|
|
merge_request: merge_request,
|
|
|
|
merge_request_diffs: merge_request_diffs,
|
|
|
|
allow_tree_conflicts: allow_tree_conflicts
|
|
|
|
}
|
2020-11-13 13:09:11 -05:00
|
|
|
end
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
let(:entity) do
|
2020-11-13 13:09:11 -05:00
|
|
|
described_class.new(merge_request_diffs.first.diffs, options)
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as json' do
|
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
it 'contains needed attributes' do
|
|
|
|
expect(subject).to include(
|
|
|
|
:real_size, :size, :branch_name,
|
|
|
|
:target_branch_name, :commit, :merge_request_diff,
|
|
|
|
:start_version, :latest_diff, :latest_version_path,
|
|
|
|
:added_lines, :removed_lines, :render_overflow_warning,
|
|
|
|
:email_patch_path, :plain_diff_path, :diff_files,
|
2020-04-02 05:08:14 -04:00
|
|
|
:merge_request_diffs, :definition_path_prefix
|
2018-06-21 08:22:40 -04:00
|
|
|
)
|
|
|
|
end
|
2020-04-02 08:08:18 -04:00
|
|
|
|
2021-01-25 16:09:03 -05:00
|
|
|
context 'broken merge request' do
|
|
|
|
let(:merge_request) { create(:merge_request, :invalid, target_project: project, source_project: project) }
|
|
|
|
|
|
|
|
it 'renders without errors' do
|
|
|
|
expect { subject }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-02 08:08:18 -04:00
|
|
|
context "when a commit_id is passed" do
|
|
|
|
let(:commits) { merge_request.commits }
|
|
|
|
let(:entity) do
|
|
|
|
described_class.new(
|
|
|
|
merge_request_diffs.first.diffs,
|
|
|
|
request: request,
|
|
|
|
merge_request: merge_request,
|
|
|
|
merge_request_diffs: merge_request_diffs,
|
|
|
|
commit: commit
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { entity.as_json }
|
|
|
|
|
|
|
|
context "when the passed commit is not the first or last in the group" do
|
|
|
|
let(:commit) { commits.third }
|
|
|
|
|
|
|
|
it 'includes commit references for previous and next' do
|
2020-04-08 14:09:16 -04:00
|
|
|
expect(subject[:commit][:next_commit_id]).to eq(commits.second.id)
|
|
|
|
expect(subject[:commit][:prev_commit_id]).to eq(commits.fourth.id)
|
2020-04-02 08:08:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the passed commit is the first in the group" do
|
|
|
|
let(:commit) { commits.first }
|
|
|
|
|
2020-04-08 14:09:16 -04:00
|
|
|
it 'includes commit references for nil and previous commit' do
|
|
|
|
expect(subject[:commit][:next_commit_id]).to be_nil
|
|
|
|
expect(subject[:commit][:prev_commit_id]).to eq(commits.second.id)
|
2020-04-02 08:08:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the passed commit is the last in the group" do
|
|
|
|
let(:commit) { commits.last }
|
|
|
|
|
2020-04-08 14:09:16 -04:00
|
|
|
it 'includes commit references for the next and nil' do
|
|
|
|
expect(subject[:commit][:next_commit_id]).to eq(commits[-2].id)
|
|
|
|
expect(subject[:commit][:prev_commit_id]).to be_nil
|
2020-04-02 08:08:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-13 13:09:11 -05:00
|
|
|
|
|
|
|
context 'when there are conflicts' do
|
|
|
|
let(:diff_files) { merge_request_diffs.first.diffs.diff_files }
|
|
|
|
let(:diff_file_with_conflict) { diff_files.to_a.last }
|
|
|
|
let(:diff_file_without_conflict) { diff_files.to_a[-2] }
|
|
|
|
|
|
|
|
let(:resolvable_conflicts) { true }
|
2021-07-28 11:09:57 -04:00
|
|
|
let(:conflict_file) { double(path: diff_file_with_conflict.new_path, conflict_type: :both_modified) }
|
2020-11-13 13:09:11 -05:00
|
|
|
let(:conflicts) { double(conflicts: double(files: [conflict_file]), can_be_resolved_in_ui?: resolvable_conflicts) }
|
|
|
|
|
|
|
|
let(:merge_ref_head_diff) { true }
|
|
|
|
let(:options) { super().merge(merge_ref_head_diff: merge_ref_head_diff) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(MergeRequests::Conflicts::ListService).to receive(:new).and_return(conflicts)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'conflicts are highlighted' do
|
|
|
|
expect(conflict_file).to receive(:diff_lines_for_serializer)
|
|
|
|
expect(diff_file_with_conflict).not_to receive(:diff_lines_for_serializer)
|
|
|
|
expect(diff_file_without_conflict).to receive(:diff_lines_for_serializer).twice # for highlighted_diff_lines and is_fully_expanded
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge ref head diff is not chosen to be displayed' do
|
|
|
|
let(:merge_ref_head_diff) { false }
|
|
|
|
|
|
|
|
it 'conflicts are not calculated' do
|
|
|
|
expect(MergeRequests::Conflicts::ListService).not_to receive(:new)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when conflicts cannot be resolved' do
|
|
|
|
let(:resolvable_conflicts) { false }
|
|
|
|
|
|
|
|
it 'conflicts are not highlighted' do
|
|
|
|
expect(conflict_file).not_to receive(:diff_lines_for_serializer)
|
|
|
|
expect(diff_file_with_conflict).to receive(:diff_lines_for_serializer).twice # for highlighted_diff_lines and is_fully_expanded
|
|
|
|
expect(diff_file_without_conflict).to receive(:diff_lines_for_serializer).twice # for highlighted_diff_lines and is_fully_expanded
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2021-07-21 17:10:10 -04:00
|
|
|
|
|
|
|
context 'when allow_tree_conflicts is set to true' do
|
|
|
|
let(:allow_tree_conflicts) { true }
|
|
|
|
|
|
|
|
it 'conflicts are still highlighted' do
|
|
|
|
expect(conflict_file).to receive(:diff_lines_for_serializer)
|
|
|
|
expect(diff_file_with_conflict).not_to receive(:diff_lines_for_serializer)
|
|
|
|
expect(diff_file_without_conflict).to receive(:diff_lines_for_serializer).twice # for highlighted_diff_lines and is_fully_expanded
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2020-11-13 13:09:11 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-21 08:22:40 -04:00
|
|
|
end
|
|
|
|
end
|