debian-mirror-gitlab/spec/lib/gitlab/suggestions/suggestion_set_spec.rb

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

126 lines
4 KiB
Ruby
Raw Permalink Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Suggestions::SuggestionSet do
2022-06-21 17:19:12 +05:30
include ProjectForksHelper
using RSpec::Parameterized::TableSyntax
2020-06-23 00:09:42 +05:30
def create_suggestion(file_path, new_line, to_content)
position = Gitlab::Diff::Position.new(old_path: file_path,
new_path: file_path,
old_line: nil,
new_line: new_line,
diff_refs: merge_request.diff_refs)
diff_note = create(:diff_note_on_merge_request,
noteable: merge_request,
position: position,
project: project)
create(:suggestion,
:content_from_repo,
note: diff_note,
to_content: to_content)
end
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
2022-06-21 17:19:12 +05:30
let_it_be(:forked_project) { fork_project(project, nil, repository: true) }
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
let_it_be(:merge_request_same_project) do
2020-06-23 00:09:42 +05:30
create(:merge_request, source_project: project, target_project: project)
end
2022-06-21 17:19:12 +05:30
let_it_be(:merge_request_from_fork) do
create(:merge_request, source_project: forked_project, target_project: project)
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
where(:merge_request) { [ref(:merge_request_same_project), ref(:merge_request_from_fork)] }
with_them do
let(:note) { create(:diff_note_on_merge_request, project: project, noteable: merge_request) }
let(:suggestion) { create(:suggestion, note: note) }
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
let(:suggestion2) do
create_suggestion('files/ruby/popen.rb', 13, "*** SUGGESTION 2 ***")
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
let(:suggestion3) do
create_suggestion('files/ruby/regex.rb', 22, "*** SUGGESTION 3 ***")
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
let(:unappliable_suggestion) { create(:suggestion, :unappliable) }
let(:suggestion_set) { described_class.new([suggestion]) }
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
describe '#source_project' do
it 'returns the source project associated with the suggestions' do
expect(suggestion_set.source_project).to be(merge_request.source_project)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#target_project' do
it 'returns the target project associated with the suggestions' do
expect(suggestion_set.target_project).to be(project)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#branch' do
it 'returns the branch associated with the suggestions' do
expected_branch = suggestion.branch
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expect(suggestion_set.branch).to be(expected_branch)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#valid?' do
it 'returns true if no errors are found' do
expect(suggestion_set.valid?).to be(true)
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
it 'returns false if an error is found' do
suggestion_set = described_class.new([unappliable_suggestion])
expect(suggestion_set.valid?).to be(false)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#error_message' do
it 'returns an error message if an error is found' do
suggestion_set = described_class.new([unappliable_suggestion])
expect(suggestion_set.error_message).to be_a(String)
end
it 'returns nil if no errors are found' do
expect(suggestion_set.error_message).to be(nil)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#actions' do
it 'returns an array of hashes with proper key/value pairs' do
first_action = suggestion_set.actions.first
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
file_suggestion = suggestion_set.send(:suggestions_per_file).first
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expect(first_action[:action]).to be('update')
expect(first_action[:file_path]).to eq(file_suggestion.file_path)
expect(first_action[:content]).to eq(file_suggestion.new_content)
end
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
describe '#file_paths' do
it 'returns an array of unique file paths associated with the suggestions' do
suggestion_set = described_class.new([suggestion, suggestion2, suggestion3])
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expected_paths = %w(files/ruby/popen.rb files/ruby/regex.rb)
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
actual_paths = suggestion_set.file_paths
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expect(actual_paths.sort).to eq(expected_paths)
end
2020-06-23 00:09:42 +05:30
end
end
end