debian-mirror-gitlab/spec/serializers/diffs_metadata_entity_spec.rb

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

105 lines
3.9 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe DiffsMetadataEntity do
2019-12-21 20:55:43 +05:30
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:request) { EntityRequest.new(project: project, current_user: user) }
let(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
let(:merge_request_diffs) { merge_request.merge_request_diffs }
let(:merge_request_diff) { merge_request_diffs.last }
2021-10-27 15:23:28 +05:30
let(:options) { {} }
2019-12-21 20:55:43 +05:30
let(:entity) do
2021-10-27 15:23:28 +05:30
described_class.new(
merge_request_diff.diffs,
options.merge(
request: request,
merge_request: merge_request,
merge_request_diffs: merge_request_diffs
)
)
2019-12-21 20:55:43 +05:30
end
context 'as json' do
subject { entity.as_json }
it 'contain only required attributes' do
expect(subject.keys).to contain_exactly(
# Inherited attributes
: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,
2021-06-08 01:23:25 +05:30
:merge_request_diffs, :context_commits, :context_commits_diff,
2020-05-24 23:13:21 +05:30
:definition_path_prefix, :source_branch_exists,
2020-10-24 23:57:45 +05:30
:can_merge, :conflict_resolution_path, :has_conflicts,
2021-03-08 18:12:59 +05:30
:project_name, :project_path, :user_full_name, :username,
2019-12-21 20:55:43 +05:30
# Attributes
:diff_files
)
end
describe 'diff_files' do
2021-10-27 15:23:28 +05:30
let!(:raw_diff_files) { merge_request_diff.diffs.raw_diff_files }
2020-01-01 13:55:28 +05:30
2021-10-27 15:23:28 +05:30
before do
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiff) do |instance|
# Use lightweight version instead. Several methods delegate to it, so putting a 5
# calls limit.
expect(instance).to receive(:raw_diff_files).at_most(5).times.and_call_original
expect(instance).not_to receive(:diff_files)
end
2021-10-27 15:23:28 +05:30
end
2020-01-01 13:55:28 +05:30
2021-10-27 15:23:28 +05:30
it 'returns diff files metadata' do
2020-01-01 13:55:28 +05:30
payload = DiffFileMetadataEntity.represent(raw_diff_files).as_json
2019-12-21 20:55:43 +05:30
expect(subject[:diff_files]).to eq(payload)
end
2021-10-27 15:23:28 +05:30
context 'when merge_ref_head_diff and allow_tree_conflicts options are set' do
let(:conflict_file) { double(path: raw_diff_files.first.new_path, conflict_type: :both_modified) }
let(:conflicts) { double(conflicts: double(files: [conflict_file]), can_be_resolved_in_ui?: false) }
before do
2022-08-13 15:12:31 +05:30
allow(merge_request).to receive(:cannot_be_merged?).and_return(true)
2021-10-27 15:23:28 +05:30
allow(MergeRequests::Conflicts::ListService).to receive(:new).and_return(conflicts)
end
context 'when merge_ref_head_diff is true and allow_tree_conflicts is false' do
let(:options) { { merge_ref_head_diff: true, allow_tree_conflicts: false } }
it 'returns diff files metadata without conflicts' do
payload = DiffFileMetadataEntity.represent(raw_diff_files).as_json
expect(subject[:diff_files]).to eq(payload)
end
end
context 'when merge_ref_head_diff is false and allow_tree_conflicts is true' do
let(:options) { { merge_ref_head_diff: false, allow_tree_conflicts: true } }
it 'returns diff files metadata without conflicts' do
payload = DiffFileMetadataEntity.represent(raw_diff_files).as_json
expect(subject[:diff_files]).to eq(payload)
end
end
context 'when merge_ref_head_diff and allow_tree_conflicts are true' do
let(:options) { { merge_ref_head_diff: true, allow_tree_conflicts: true } }
it 'returns diff files metadata with conflicts' do
payload = DiffFileMetadataEntity.represent(raw_diff_files, conflicts: { conflict_file.path => conflict_file }).as_json
expect(subject[:diff_files]).to eq(payload)
end
end
end
2019-12-21 20:55:43 +05:30
end
end
end