diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 21:06:24 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 21:06:24 +0000 |
commit | 4b28d5ae770c6bd332283a3f13ceae06329c409b (patch) | |
tree | ae4d46e1d017002935fe75dc14cb3c108be12fae /spec/lib | |
parent | 41efffa17c67405ca5f5dac49d72be7872cee339 (diff) | |
download | gitlab-ce-4b28d5ae770c6bd332283a3f13ceae06329c409b.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/diff/file_collection/merge_request_diff_batch_spec.rb | 126 | ||||
-rw-r--r-- | spec/lib/gitlab/git/diff_collection_spec.rb | 64 |
2 files changed, 190 insertions, 0 deletions
diff --git a/spec/lib/gitlab/diff/file_collection/merge_request_diff_batch_spec.rb b/spec/lib/gitlab/diff/file_collection/merge_request_diff_batch_spec.rb new file mode 100644 index 00000000000..265c6260ca9 --- /dev/null +++ b/spec/lib/gitlab/diff/file_collection/merge_request_diff_batch_spec.rb @@ -0,0 +1,126 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Diff::FileCollection::MergeRequestDiffBatch do + let(:merge_request) { create(:merge_request) } + let(:batch_page) { 1 } + let(:batch_size) { 10 } + let(:diffable) { merge_request.merge_request_diff } + let(:diff_files_relation) { diffable.merge_request_diff_files } + + subject do + described_class.new(diffable, + batch_page, + batch_size, + diff_options: nil) + end + + let(:diff_files) { subject.diff_files } + + describe 'initialize' do + it 'memoizes pagination_data' do + expect(subject.pagination_data).to eq(current_page: 1, next_page: 2, total_pages: 2) + end + end + + describe '#diff_files' do + let(:batch_size) { 3 } + let(:paginated_rel) { diff_files_relation.page(batch_page).per(batch_size) } + + let(:expected_batch_files) do + paginated_rel.map(&:new_path) + end + + it 'returns paginated diff files' do + expect(diff_files.size).to eq(3) + end + + it 'returns a valid instance of a DiffCollection' do + expect(diff_files).to be_a(Gitlab::Git::DiffCollection) + end + + context 'first page' do + it 'returns correct diff files' do + expect(diff_files.map(&:new_path)).to eq(expected_batch_files) + end + end + + context 'another page' do + let(:batch_page) { 2 } + + it 'returns correct diff files' do + expect(diff_files.map(&:new_path)).to eq(expected_batch_files) + end + end + + context 'nil batch_page' do + let(:batch_page) { nil } + + it 'returns correct diff files' do + expected_batch_files = + diff_files_relation.page(described_class::DEFAULT_BATCH_PAGE).per(batch_size).map(&:new_path) + + expect(diff_files.map(&:new_path)).to eq(expected_batch_files) + end + end + + context 'nil batch_size' do + let(:batch_size) { nil } + + it 'returns correct diff files' do + expected_batch_files = + diff_files_relation.page(batch_page).per(described_class::DEFAULT_BATCH_SIZE).map(&:new_path) + + expect(diff_files.map(&:new_path)).to eq(expected_batch_files) + end + end + + context 'invalid page' do + let(:batch_page) { 999 } + + it 'returns correct diff files' do + expect(diff_files.map(&:new_path)).to be_empty + end + end + + context 'last page' do + it 'returns correct diff files' do + last_page = paginated_rel.total_pages + collection = described_class.new(diffable, + last_page, + batch_size, + diff_options: nil) + + expected_batch_files = diff_files_relation.page(last_page).per(batch_size).map(&:new_path) + + expect(collection.diff_files.map(&:new_path)).to eq(expected_batch_files) + end + end + end + + it_behaves_like 'unfoldable diff' do + subject do + described_class.new(merge_request.merge_request_diff, + batch_page, + batch_size, + diff_options: nil) + end + end + + it_behaves_like 'diff statistics' do + let(:collection_default_args) do + { diff_options: {} } + end + + let(:diffable) { merge_request.merge_request_diff } + let(:stub_path) { '.gitignore' } + + subject do + described_class.new(merge_request.merge_request_diff, + batch_page, + batch_size, + collection_default_args) + end + end +end diff --git a/spec/lib/gitlab/git/diff_collection_spec.rb b/spec/lib/gitlab/git/diff_collection_spec.rb index be6ab0c1200..ded173c49ef 100644 --- a/spec/lib/gitlab/git/diff_collection_spec.rb +++ b/spec/lib/gitlab/git/diff_collection_spec.rb @@ -537,6 +537,70 @@ describe Gitlab::Git::DiffCollection, :seed_helper do end end end + + context 'when offset_index is given' do + subject do + Gitlab::Git::DiffCollection.new( + iterator, + max_files: max_files, + max_lines: max_lines, + limits: limits, + offset_index: 2, + expanded: expanded + ) + end + + def diff(raw) + raw['diff'] + end + + let(:iterator) do + [ + fake_diff(1, 1), + fake_diff(2, 2), + fake_diff(3, 3), + fake_diff(4, 4) + ] + end + + it 'does not yield diffs before the offset' do + expect(subject.to_a.map(&:diff)).to eq( + [ + diff(fake_diff(3, 3)), + diff(fake_diff(4, 4)) + ] + ) + end + + context 'when go over safe limits on bytes' do + let(:iterator) do + [ + fake_diff(1, 10), # 10 + fake_diff(1, 10), # 20 + fake_diff(1, 15), # 35 + fake_diff(1, 20), # 55 + fake_diff(1, 45), # 100 - limit hit + fake_diff(1, 45), + fake_diff(1, 20480), + fake_diff(1, 1) + ] + end + + before do + stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', + { max_files: max_files, max_lines: 80 }) + end + + it 'considers size of diffs before the offset for prunning' do + expect(subject.to_a.map(&:diff)).to eq( + [ + diff(fake_diff(1, 15)), + diff(fake_diff(1, 20)) + ] + ) + end + end + end end def fake_diff(line_length, line_count) |