2016-06-28 12:25:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe MergeRequestDiff, models: true do
|
2016-07-28 16:40:13 -04:00
|
|
|
describe 'create new record' do
|
2016-08-01 11:41:21 -04:00
|
|
|
subject { create(:merge_request).merge_request_diff }
|
2016-07-28 16:40:13 -04:00
|
|
|
|
|
|
|
it { expect(subject).to be_valid }
|
2016-08-01 11:41:21 -04:00
|
|
|
it { expect(subject).to be_persisted }
|
2016-10-03 08:11:16 -04:00
|
|
|
it { expect(subject.commits.count).to eq(29) }
|
|
|
|
it { expect(subject.diffs.count).to eq(20) }
|
|
|
|
it { expect(subject.head_commit_sha).to eq('b83d6e391c22777fca1ed3012fce84f633d7fed0') }
|
2016-08-02 08:38:03 -04:00
|
|
|
it { expect(subject.base_commit_sha).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
|
|
|
|
it { expect(subject.start_commit_sha).to eq('0b4bc9a49b562e85de7cc9e834518ea6828729b9') }
|
2016-07-28 16:40:13 -04:00
|
|
|
end
|
|
|
|
|
2016-08-25 03:59:30 -04:00
|
|
|
describe '#latest' do
|
|
|
|
let!(:mr) { create(:merge_request, :with_diffs) }
|
|
|
|
let!(:first_diff) { mr.merge_request_diff }
|
|
|
|
let!(:last_diff) { mr.create_merge_request_diff }
|
|
|
|
|
|
|
|
it { expect(last_diff.latest?).to be_truthy }
|
|
|
|
it { expect(first_diff.latest?).to be_falsey }
|
|
|
|
end
|
|
|
|
|
2016-06-28 12:25:32 -04:00
|
|
|
describe '#diffs' do
|
|
|
|
let(:mr) { create(:merge_request, :with_diffs) }
|
|
|
|
let(:mr_diff) { mr.merge_request_diff }
|
|
|
|
|
|
|
|
context 'when the :ignore_whitespace_change option is set' do
|
|
|
|
it 'creates a new compare object instead of loading from the DB' do
|
|
|
|
expect(mr_diff).not_to receive(:load_diffs)
|
|
|
|
expect(Gitlab::Git::Compare).to receive(:new).and_call_original
|
|
|
|
|
2016-08-03 17:32:12 -04:00
|
|
|
mr_diff.raw_diffs(ignore_whitespace_change: true)
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the raw diffs are empty' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-06-09 07:48:25 -04:00
|
|
|
MergeRequestDiffFile.delete_all(merge_request_diff_id: mr_diff.id)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-06-28 12:25:32 -04:00
|
|
|
|
|
|
|
it 'returns an empty DiffCollection' do
|
2016-08-03 17:32:12 -04:00
|
|
|
expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
|
|
|
|
expect(mr_diff.raw_diffs).to be_empty
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-08 01:48:23 -04:00
|
|
|
context 'when the raw diffs have invalid content' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-06-09 07:48:25 -04:00
|
|
|
MergeRequestDiffFile.delete_all(merge_request_diff_id: mr_diff.id)
|
2017-06-14 14:18:56 -04:00
|
|
|
mr_diff.update_attributes(st_diffs: ["--broken-diff"])
|
|
|
|
end
|
2016-10-08 01:48:23 -04:00
|
|
|
|
|
|
|
it 'returns an empty DiffCollection' do
|
|
|
|
expect(mr_diff.raw_diffs.to_a).to be_empty
|
|
|
|
expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
|
|
|
|
expect(mr_diff.raw_diffs).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-28 12:25:32 -04:00
|
|
|
context 'when the raw diffs exist' do
|
|
|
|
it 'returns the diffs' do
|
2016-08-03 17:32:12 -04:00
|
|
|
expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
|
|
|
|
expect(mr_diff.raw_diffs).not_to be_empty
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the :paths option is set' do
|
2016-08-03 17:32:12 -04:00
|
|
|
let(:diffs) { mr_diff.raw_diffs(paths: ['files/ruby/popen.rb', 'files/ruby/popen.rb']) }
|
2016-06-28 12:25:32 -04:00
|
|
|
|
2016-07-08 17:50:06 -04:00
|
|
|
it 'only returns diffs that match the (old path, new path) given' do
|
|
|
|
expect(diffs.map(&:new_path)).to contain_exactly('files/ruby/popen.rb')
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the diffs from the DB' do
|
|
|
|
expect(mr_diff).to receive(:load_diffs)
|
|
|
|
|
|
|
|
diffs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 10:46:47 -04:00
|
|
|
end
|
2016-09-22 05:34:46 -04:00
|
|
|
|
2016-12-30 14:16:25 -05:00
|
|
|
describe '#save_diffs' do
|
|
|
|
it 'saves collected state' do
|
|
|
|
mr_diff = create(:merge_request).merge_request_diff
|
|
|
|
|
|
|
|
expect(mr_diff.collected?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves overflow state' do
|
2017-02-22 17:54:59 -05:00
|
|
|
allow(Commit).to receive(:max_diff_options)
|
|
|
|
.and_return(max_lines: 0, max_files: 0)
|
2016-12-30 14:16:25 -05:00
|
|
|
|
|
|
|
mr_diff = create(:merge_request).merge_request_diff
|
|
|
|
|
|
|
|
expect(mr_diff.overflow?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves empty state' do
|
2017-02-22 17:54:59 -05:00
|
|
|
allow_any_instance_of(MergeRequestDiff).to receive(:commits)
|
|
|
|
.and_return([])
|
2016-12-30 14:16:25 -05:00
|
|
|
|
|
|
|
mr_diff = create(:merge_request).merge_request_diff
|
|
|
|
|
|
|
|
expect(mr_diff.empty?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-29 10:46:47 -04:00
|
|
|
describe '#commits_sha' do
|
2016-12-01 06:17:30 -05:00
|
|
|
it 'returns all commits SHA using serialized commits' do
|
|
|
|
subject.st_commits = [
|
|
|
|
{ id: 'sha1' },
|
|
|
|
{ id: 'sha2' }
|
|
|
|
]
|
2016-09-22 05:34:46 -04:00
|
|
|
|
2017-02-22 12:46:57 -05:00
|
|
|
expect(subject.commits_sha).to eq(%w(sha1 sha2))
|
2016-09-29 10:46:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#compare_with' do
|
2016-09-29 11:02:06 -04:00
|
|
|
subject { create(:merge_request, source_branch: 'fix').merge_request_diff }
|
2016-09-29 10:46:47 -04:00
|
|
|
|
|
|
|
it 'delegates compare to the service' do
|
|
|
|
expect(CompareService).to receive(:new).and_call_original
|
|
|
|
|
2016-09-29 11:02:06 -04:00
|
|
|
subject.compare_with(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses git diff A..B approach by default' do
|
|
|
|
diffs = subject.compare_with('0b4bc9a49b562e85de7cc9e834518ea6828729b9').diffs
|
|
|
|
|
|
|
|
expect(diffs.size).to eq(3)
|
2016-09-22 05:34:46 -04:00
|
|
|
end
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
2016-12-01 06:17:30 -05:00
|
|
|
|
|
|
|
describe '#commits_count' do
|
|
|
|
it 'returns number of commits using serialized commits' do
|
|
|
|
subject.st_commits = [
|
|
|
|
{ id: 'sha1' },
|
|
|
|
{ id: 'sha2' }
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(subject.commits_count).to eq 2
|
|
|
|
end
|
|
|
|
end
|
2017-06-06 12:40:07 -04:00
|
|
|
|
|
|
|
describe '#utf8_st_diffs' do
|
|
|
|
it 'does not raise error when a hash value is in binary' do
|
|
|
|
subject.st_diffs = [
|
|
|
|
{ diff: "\0" },
|
|
|
|
{ diff: "\x05\x00\x68\x65\x6c\x6c\x6f" }
|
|
|
|
]
|
|
|
|
|
|
|
|
expect { subject.utf8_st_diffs }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|