2016-06-28 16:25:32 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe MergeRequestDiff do
|
2017-11-21 16:58:08 +00:00
|
|
|
let(:diff_with_commits) { create(:merge_request).merge_request_diff }
|
|
|
|
|
2016-07-28 20:40:13 +00:00
|
|
|
describe 'create new record' do
|
2017-11-21 16:58:08 +00:00
|
|
|
subject { diff_with_commits }
|
2016-07-28 20:40:13 +00:00
|
|
|
|
|
|
|
it { expect(subject).to be_valid }
|
2016-08-01 15:41:21 +00:00
|
|
|
it { expect(subject).to be_persisted }
|
2016-10-03 12:11:16 +00: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 12:38:03 +00:00
|
|
|
it { expect(subject.base_commit_sha).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
|
|
|
|
it { expect(subject.start_commit_sha).to eq('0b4bc9a49b562e85de7cc9e834518ea6828729b9') }
|
2016-07-28 20:40:13 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 07:59:30 +00: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 }
|
|
|
|
|
Use latest_merge_request_diff association
Compared to the merge_request_diff association:
1. It's simpler to query. The query uses a foreign key to the
merge_request_diffs table, so no ordering is necessary.
2. It's faster for preloading. The merge_request_diff association has to load
every diff for the MRs in the set, then discard all but the most recent for
each. This association means that Rails can just query for N diffs from N
MRs.
3. It's more complicated to update. This is a bidirectional foreign key, so we
need to update two tables when adding a diff record. This also means we need
to handle this as a special case when importing a GitLab project.
There is some juggling with this association in the merge request model:
* `MergeRequest#latest_merge_request_diff` is _always_ the latest diff.
* `MergeRequest#merge_request_diff` reuses
`MergeRequest#latest_merge_request_diff` unless:
* Arguments are passed. These are typically to force-reload the association.
* It doesn't exist. That means we might be trying to implicitly create a
diff. This only seems to happen in specs.
* The association is already loaded. This is important for the reasons
explained in the comment, which I'll reiterate here: if we a) load a
non-latest diff, then b) get its `merge_request`, then c) get that MR's
`merge_request_diff`, we should get the diff we loaded in c), even though
that's not the latest diff.
Basically, `MergeRequest#merge_request_diff` is the latest diff in most cases,
but not quite all.
2017-11-15 17:22:18 +00:00
|
|
|
it { expect(last_diff.reload).to be_latest }
|
|
|
|
it { expect(first_diff.reload).not_to be_latest }
|
2016-08-25 07:59:30 +00:00
|
|
|
end
|
|
|
|
|
2016-06-28 16:25:32 +00:00
|
|
|
describe '#diffs' do
|
|
|
|
context 'when the :ignore_whitespace_change option is set' do
|
|
|
|
it 'creates a new compare object instead of loading from the DB' do
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diff_with_commits).not_to receive(:load_diffs)
|
|
|
|
expect(diff_with_commits.compare).to receive(:diffs).and_call_original
|
2016-06-28 16:25:32 +00:00
|
|
|
|
2017-11-21 16:58:08 +00:00
|
|
|
diff_with_commits.raw_diffs(ignore_whitespace_change: true)
|
2016-06-28 16:25:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the raw diffs are empty' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2017-11-21 16:58:08 +00:00
|
|
|
MergeRequestDiffFile.delete_all(merge_request_diff_id: diff_with_commits.id)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-10-08 05:48:23 +00:00
|
|
|
|
|
|
|
it 'returns an empty DiffCollection' do
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diff_with_commits.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
|
|
|
|
expect(diff_with_commits.raw_diffs).to be_empty
|
2016-10-08 05:48:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-28 16:25:32 +00:00
|
|
|
context 'when the raw diffs exist' do
|
|
|
|
it 'returns the diffs' do
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diff_with_commits.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
|
|
|
|
expect(diff_with_commits.raw_diffs).not_to be_empty
|
2016-06-28 16:25:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the :paths option is set' do
|
2017-11-21 16:58:08 +00:00
|
|
|
let(:diffs) { diff_with_commits.raw_diffs(paths: ['files/ruby/popen.rb', 'files/ruby/popen.rb']) }
|
2016-06-28 16:25:32 +00:00
|
|
|
|
2016-07-08 21:50:06 +00: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 16:25:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the diffs from the DB' do
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diff_with_commits).to receive(:load_diffs)
|
2016-06-28 16:25:32 +00:00
|
|
|
|
|
|
|
diffs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 14:46:47 +00:00
|
|
|
end
|
2016-09-22 09:34:46 +00:00
|
|
|
|
2016-12-30 19:16:25 +00: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 22:54:59 +00:00
|
|
|
allow(Commit).to receive(:max_diff_options)
|
|
|
|
.and_return(max_lines: 0, max_files: 0)
|
2016-12-30 19:16:25 +00:00
|
|
|
|
|
|
|
mr_diff = create(:merge_request).merge_request_diff
|
|
|
|
|
|
|
|
expect(mr_diff.overflow?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves empty state' do
|
2017-07-25 17:09:00 +00:00
|
|
|
allow_any_instance_of(described_class).to receive_message_chain(:compare, :commits)
|
2017-02-22 22:54:59 +00:00
|
|
|
.and_return([])
|
2016-12-30 19:16:25 +00:00
|
|
|
|
|
|
|
mr_diff = create(:merge_request).merge_request_diff
|
|
|
|
|
|
|
|
expect(mr_diff.empty?).to be_truthy
|
|
|
|
end
|
2017-07-25 16:57:02 +00:00
|
|
|
|
|
|
|
it 'saves binary diffs correctly' do
|
|
|
|
path = 'files/images/icn-time-tracking.pdf'
|
|
|
|
mr_diff = create(:merge_request, source_branch: 'add-pdf-text-binary', target_branch: 'master').merge_request_diff
|
|
|
|
diff_file = mr_diff.merge_request_diff_files.find_by(new_path: path)
|
|
|
|
|
|
|
|
expect(diff_file).to be_binary
|
|
|
|
expect(diff_file.diff).to eq(mr_diff.compare.diffs(paths: [path]).to_a.first.diff)
|
|
|
|
end
|
2016-12-30 19:16:25 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
describe '#commit_shas' do
|
2017-11-21 16:58:08 +00:00
|
|
|
it 'returns all commit SHAs using commits from the DB' do
|
|
|
|
expect(diff_with_commits.commit_shas).not_to be_empty
|
|
|
|
expect(diff_with_commits.commit_shas).to all(match(/\h{40}/))
|
2016-09-29 14:46:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#compare_with' do
|
|
|
|
it 'delegates compare to the service' do
|
|
|
|
expect(CompareService).to receive(:new).and_call_original
|
|
|
|
|
2017-11-21 16:58:08 +00:00
|
|
|
diff_with_commits.compare_with(nil)
|
2016-09-29 15:02:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses git diff A..B approach by default' do
|
2017-11-21 16:58:08 +00:00
|
|
|
diffs = diff_with_commits.compare_with('0b4bc9a49b562e85de7cc9e834518ea6828729b9').diffs
|
2016-09-29 15:02:06 +00:00
|
|
|
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diffs.size).to eq(21)
|
2016-09-22 09:34:46 +00:00
|
|
|
end
|
2016-06-28 16:25:32 +00:00
|
|
|
end
|
2016-12-01 11:17:30 +00:00
|
|
|
|
|
|
|
describe '#commits_count' do
|
|
|
|
it 'returns number of commits using serialized commits' do
|
2017-11-21 16:58:08 +00:00
|
|
|
expect(diff_with_commits.commits_count).to eq(29)
|
2017-06-06 16:40:07 +00:00
|
|
|
end
|
|
|
|
end
|
2016-06-28 16:25:32 +00:00
|
|
|
end
|