Add latest changes from gitlab-org/gitlab@master

This commit is contained in:
GitLab Bot
2023-06-07 21:08:30 +00:00
parent 9ee2305f46
commit e0d38e233d
63 changed files with 1402 additions and 225 deletions

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Diff::Formatters::FileFormatter, feature_category: :code_review_workflow do
let(:base_attrs) do
{
base_sha: 123,
start_sha: 456,
head_sha: 789,
old_path: nil,
new_path: nil,
position_type: 'file'
}
end
let(:attrs) { base_attrs.merge(old_path: 'path.rb', new_path: 'path.rb') }
it_behaves_like 'position formatter' do
# rubocop:disable Fips/SHA1 (This is used to match the existing class method)
let(:key) do
[123, 456, 789,
Digest::SHA1.hexdigest(formatter.old_path), Digest::SHA1.hexdigest(formatter.new_path),
'path.rb', 'path.rb']
end
# rubocop:enable Fips/SHA1
end
describe '#==' do
subject { described_class.new(attrs) }
it { is_expected.to eq(subject) }
[:old_path, :new_path].each do |attr|
context "with attribute:#{attr}" do
let(:other_formatter) do
described_class.new(attrs.merge(attr => 9))
end
it { is_expected.not_to eq(other_formatter) }
end
end
end
end

View File

@ -0,0 +1,238 @@
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Diff::PositionTracer::FileStrategy, feature_category: :code_review_workflow do
include PositionTracerHelpers
let_it_be(:project) { create(:project, :repository) }
let(:current_user) { project.first_owner }
let(:file_name) { 'test-file' }
let(:new_file_name) { "#{file_name}-new" }
let(:second_file_name) { "#{file_name}-2" }
let(:branch_name) { 'position-tracer-test' }
let(:old_position) { position(old_path: file_name, new_path: file_name, position_type: 'file') }
let(:tracer) do
Gitlab::Diff::PositionTracer.new(
project: project,
old_diff_refs: old_diff_refs,
new_diff_refs: new_diff_refs
)
end
let(:strategy) { described_class.new(tracer) }
let(:initial_commit) do
project.commit(create_branch(branch_name, 'master')[:branch]&.name || 'master')
end
subject { strategy.trace(old_position) }
describe '#trace' do
describe 'diff scenarios' do
let(:create_file_commit) do
initial_commit
create_file(
branch_name,
file_name,
Base64.encode64('content')
)
end
let(:update_file_commit) do
create_file_commit
update_file(
branch_name,
file_name,
Base64.encode64('updatedcontent')
)
end
let(:update_file_again_commit) do
update_file_commit
update_file(
branch_name,
file_name,
Base64.encode64('updatedcontentagain')
)
end
let(:delete_file_commit) do
create_file_commit
delete_file(branch_name, file_name)
end
let(:rename_file_commit) do
delete_file_commit
create_file(
branch_name,
new_file_name,
Base64.encode64('renamedcontent')
)
end
let(:create_second_file_commit) do
create_file_commit
create_file(
branch_name,
second_file_name,
Base64.encode64('morecontent')
)
end
let(:create_another_file_commit) do
create_file(
branch_name,
second_file_name,
Base64.encode64('morecontent')
)
end
let(:update_another_file_commit) do
update_file(
branch_name,
second_file_name,
Base64.encode64('updatedmorecontent')
)
end
context 'when the file was created in the old diff' do
context 'when the file is unchanged between the old and the new diff' do
let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
let(:new_diff_refs) { diff_refs(initial_commit, create_second_file_commit) }
it 'returns the new position' do
expect_new_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was updated between the old and the new diff' do
let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
let(:new_diff_refs) { diff_refs(initial_commit, update_file_commit) }
let(:change_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was renamed in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
let(:new_diff_refs) { diff_refs(initial_commit, rename_file_commit) }
let(:change_diff_refs) { diff_refs(create_file_commit, rename_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was removed in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
let(:new_diff_refs) { diff_refs(initial_commit, delete_file_commit) }
let(:change_diff_refs) { diff_refs(create_file_commit, delete_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file is unchanged in the new diff' do
let(:old_diff_refs) { diff_refs(initial_commit, create_file_commit) }
let(:new_diff_refs) { diff_refs(create_another_file_commit, update_another_file_commit) }
let(:change_diff_refs) { diff_refs(initial_commit, create_another_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
end
context 'when the file was changed in the old diff' do
context 'when the file is unchanged in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
let(:new_diff_refs) { diff_refs(create_file_commit, create_second_file_commit) }
it 'returns the new position' do
expect_new_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was updated in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
let(:new_diff_refs) { diff_refs(create_file_commit, update_file_again_commit) }
let(:change_diff_refs) { diff_refs(update_file_commit, update_file_again_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was renamed in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
let(:new_diff_refs) { diff_refs(create_file_commit, rename_file_commit) }
let(:change_diff_refs) { diff_refs(update_file_commit, rename_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file was removed in between the old and the new diff' do
let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
let(:new_diff_refs) { diff_refs(create_file_commit, delete_file_commit) }
let(:change_diff_refs) { diff_refs(update_file_commit, delete_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
context 'when the file is unchanged in the new diff' do
let(:old_diff_refs) { diff_refs(create_file_commit, update_file_commit) }
let(:new_diff_refs) { diff_refs(create_another_file_commit, update_another_file_commit) }
let(:change_diff_refs) { diff_refs(create_file_commit, create_another_file_commit) }
it 'returns the position of the change' do
expect_change_position(
old_path: file_name,
new_path: file_name
)
end
end
end
end
end
end

View File

@ -18,8 +18,13 @@ RSpec.describe Gitlab::Diff::PositionTracer do
let(:project) { double }
let(:old_diff_refs) { diff_refs }
let(:new_diff_refs) { diff_refs }
let(:position) { double(on_text?: on_text?, diff_refs: diff_refs, ignore_whitespace_change: false) }
let(:on_file?) { false }
let(:on_text?) { false }
let(:tracer) { double }
let(:position) do
double(on_text?: on_text?, on_image?: false, on_file?: on_file?, diff_refs: diff_refs,
ignore_whitespace_change: false)
end
context 'position is on text' do
let(:on_text?) { true }
@ -48,6 +53,20 @@ RSpec.describe Gitlab::Diff::PositionTracer do
subject.trace(position)
end
end
context 'position on file' do
let(:on_file?) { true }
it 'calls ImageStrategy#trace' do
expect(Gitlab::Diff::PositionTracer::FileStrategy)
.to receive(:new)
.with(subject)
.and_return(tracer)
expect(tracer).to receive(:trace).with(position)
subject.trace(position)
end
end
end
describe 'diffs methods' do