2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe DiffsEntity do
|
2021-03-11 19:13:27 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
let(:request) { EntityRequest.new(project: project, current_user: user) }
|
|
|
|
let(:merge_request_diffs) { merge_request.merge_request_diffs }
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:options) do
|
2021-10-27 15:23:28 +05:30
|
|
|
{
|
|
|
|
request: request,
|
|
|
|
merge_request: merge_request,
|
2023-03-17 16:20:25 +05:30
|
|
|
merge_request_diffs: merge_request_diffs
|
2021-10-27 15:23:28 +05:30
|
|
|
}
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
let(:entity) do
|
2021-01-29 00:20:46 +05:30
|
|
|
described_class.new(merge_request_diffs.first.diffs, options)
|
2018-11-08 19:23:39 +05:30
|
|
|
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-22 19:07:51 +05:30
|
|
|
:merge_request_diffs, :definition_path_prefix
|
2018-11-08 19:23:39 +05:30
|
|
|
)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
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-22 19:07:51 +05:30
|
|
|
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
|
|
|
|
expect(subject[:commit][:next_commit_id]).to eq(commits.second.id)
|
|
|
|
expect(subject[:commit][:prev_commit_id]).to eq(commits.fourth.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the passed commit is the first in the group" do
|
|
|
|
let(:commit) { commits.first }
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the passed commit is the last in the group" do
|
|
|
|
let(:commit) { commits.last }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
describe 'diff_files' do
|
2021-01-29 00:20:46 +05:30
|
|
|
let(:diff_files) { merge_request_diffs.first.diffs.diff_files }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it 'serializes diff files using DiffFileEntity' do
|
|
|
|
expect(DiffFileEntity)
|
|
|
|
.to receive(:represent)
|
|
|
|
.with(
|
|
|
|
diff_files,
|
|
|
|
hash_including(options.merge(conflicts: nil))
|
|
|
|
)
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
subject[:diff_files]
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context 'when there are conflicts' do
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:conflict_file) { double(path: diff_files.first.new_path, conflict_type: :both_modified) }
|
|
|
|
let(:conflicts) { double(conflicts: double(files: [conflict_file]), can_be_resolved_in_ui?: false) }
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
before do
|
|
|
|
allow(merge_request).to receive(:cannot_be_merged?).and_return(true)
|
|
|
|
allow(MergeRequests::Conflicts::ListService).to receive(:new).and_return(conflicts)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it 'serializes diff files with conflicts' do
|
|
|
|
expect(DiffFileEntity)
|
|
|
|
.to receive(:represent)
|
|
|
|
.with(
|
|
|
|
diff_files,
|
|
|
|
hash_including(options.merge(conflicts: { conflict_file.path => conflict_file }))
|
|
|
|
)
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
subject[:diff_files]
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|