BigW Consortium Gitlab

diff_collection_spec.rb 12.2 KB
Newer Older
Robert Speicher committed
1 2 3 4 5 6 7 8
require 'spec_helper'

describe Gitlab::Git::DiffCollection, seed_helper: true do
  subject do
    Gitlab::Git::DiffCollection.new(
      iterator,
      max_files: max_files,
      max_lines: max_lines,
9
      limits: limits,
10
      expanded: expanded
Robert Speicher committed
11 12
    )
  end
13
  let(:iterator) { MutatingConstantIterator.new(file_count, fake_diff(line_length, line_count)) }
Robert Speicher committed
14 15 16 17 18
  let(:file_count) { 0 }
  let(:line_length) { 1 }
  let(:line_count) { 1 }
  let(:max_files) { 10 }
  let(:max_lines) { 100 }
19
  let(:limits) { true }
20
  let(:expanded) { true }
Robert Speicher committed
21 22 23 24 25 26

  describe '#to_a' do
    subject { super().to_a }
    it { is_expected.to be_kind_of ::Array }
  end

27
  describe '#decorate!' do
Robert Speicher committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    let(:file_count) { 3 }

    it 'modifies the array in place' do
      count = 0
      subject.decorate! { |d| !d.nil? && count += 1 }
      expect(subject.to_a).to eq([1, 2, 3])
      expect(count).to eq(3)
    end

    it 'avoids future iterator iterations' do
      subject.decorate! { |d| d unless d.nil? }

      expect(iterator).not_to receive(:each)

      subject.overflow?
    end
  end

  context 'overflow handling' do
    context 'adding few enough files' do
      let(:file_count) { 3 }

      context 'and few enough lines' do
        let(:line_count) { 10 }

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_falsey }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('3') }
        end
67 68 69 70 71 72 73 74 75

        describe '#size' do
          it { expect(subject.size).to eq(3) }

          it 'does not change after peeking' do
            subject.any?
            expect(subject.size).to eq(3)
          end
        end
Robert Speicher committed
76 77

        context 'when limiting is disabled' do
78
          let(:limits) { false }
Robert Speicher committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

          describe '#overflow?' do
            subject { super().overflow? }
            it { is_expected.to be_falsey }
          end

          describe '#empty?' do
            subject { super().empty? }
            it { is_expected.to be_falsey }
          end

          describe '#real_size' do
            subject { super().real_size }
            it { is_expected.to eq('3') }
          end
94 95 96

          describe '#size' do
            it { expect(subject.size).to eq(3) }
97

98 99 100 101 102
            it 'does not change after peeking' do
              subject.any?
              expect(subject.size).to eq(3)
            end
          end
Robert Speicher committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        end
      end

      context 'and too many lines' do
        let(:line_count) { 1000 }

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_truthy }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('0+') }
        end
        it { expect(subject.size).to eq(0) }

        context 'when limiting is disabled' do
126
          let(:limits) { false }
Robert Speicher committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

          describe '#overflow?' do
            subject { super().overflow? }
            it { is_expected.to be_falsey }
          end

          describe '#empty?' do
            subject { super().empty? }
            it { is_expected.to be_falsey }
          end

          describe '#real_size' do
            subject { super().real_size }
            it { is_expected.to eq('3') }
          end
          it { expect(subject.size).to eq(3) }
        end
      end
    end

    context 'adding too many files' do
      let(:file_count) { 11 }

      context 'and few enough lines' do
        let(:line_count) { 1 }

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_truthy }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('10+') }
        end
        it { expect(subject.size).to eq(10) }

        context 'when limiting is disabled' do
170
          let(:limits) { false }
Robert Speicher committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

          describe '#overflow?' do
            subject { super().overflow? }
            it { is_expected.to be_falsey }
          end

          describe '#empty?' do
            subject { super().empty? }
            it { is_expected.to be_falsey }
          end

          describe '#real_size' do
            subject { super().real_size }
            it { is_expected.to eq('11') }
          end
          it { expect(subject.size).to eq(11) }
        end
      end

      context 'and too many lines' do
        let(:line_count) { 30 }

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_truthy }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('3+') }
        end
        it { expect(subject.size).to eq(3) }

        context 'when limiting is disabled' do
210
          let(:limits) { false }
Robert Speicher committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

          describe '#overflow?' do
            subject { super().overflow? }
            it { is_expected.to be_falsey }
          end

          describe '#empty?' do
            subject { super().empty? }
            it { is_expected.to be_falsey }
          end

          describe '#real_size' do
            subject { super().real_size }
            it { is_expected.to eq('11') }
          end
          it { expect(subject.size).to eq(11) }
        end
      end
    end

    context 'adding exactly the maximum number of files' do
      let(:file_count) { 10 }

      context 'and few enough lines' do
        let(:line_count) { 1 }

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_falsey }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('10') }
        end
        it { expect(subject.size).to eq(10) }
      end
    end

    context 'adding too many bytes' do
      let(:file_count) { 10 }
      let(:line_length) { 5200 }

      describe '#overflow?' do
        subject { super().overflow? }
        it { is_expected.to be_truthy }
      end

      describe '#empty?' do
        subject { super().empty? }
        it { is_expected.to be_falsey }
      end

      describe '#real_size' do
        subject { super().real_size }
        it { is_expected.to eq('9+') }
      end
      it { expect(subject.size).to eq(9) }

      context 'when limiting is disabled' do
276
        let(:limits) { false }
Robert Speicher committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

        describe '#overflow?' do
          subject { super().overflow? }
          it { is_expected.to be_falsey }
        end

        describe '#empty?' do
          subject { super().empty? }
          it { is_expected.to be_falsey }
        end

        describe '#real_size' do
          subject { super().real_size }
          it { is_expected.to eq('10') }
        end
        it { expect(subject.size).to eq(10) }
      end
    end
  end

  describe 'empty collection' do
    subject { Gitlab::Git::DiffCollection.new([]) }

    describe '#overflow?' do
      subject { super().overflow? }
      it { is_expected.to be_falsey }
    end

    describe '#empty?' do
      subject { super().empty? }
      it { is_expected.to be_truthy }
    end

    describe '#size' do
      subject { super().size }
      it { is_expected.to eq(0) }
    end

    describe '#real_size' do
      subject { super().real_size }
      it { is_expected.to eq('0')}
    end
  end

321
  describe '#each' do
Robert Speicher committed
322 323 324 325 326 327
    context 'when diff are too large' do
      let(:collection) do
        Gitlab::Git::DiffCollection.new([{ diff: 'a' * 204800 }])
      end

      it 'yields Diff instances even when they are too large' do
328 329
        expect { |b| collection.each(&b) }
          .to yield_with_args(an_instance_of(Gitlab::Git::Diff))
Robert Speicher committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343
      end

      it 'prunes diffs that are too large' do
        diff = nil

        collection.each do |d|
          diff = d
        end

        expect(diff.diff).to eq('')
      end
    end

    context 'when diff is quite large will collapse by default' do
344 345
      let(:iterator) { [{ diff: 'a' * (Gitlab::Git::Diff.collapse_limit + 1) }] }
      let(:max_files) { 100 }
Robert Speicher committed
346 347

      context 'when no collapse is set' do
348
        let(:expanded) { true }
Robert Speicher committed
349 350

        it 'yields Diff instances even when they are quite big' do
351 352
          expect { |b| subject.each(&b) }
            .to yield_with_args(an_instance_of(Gitlab::Git::Diff))
Robert Speicher committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366
        end

        it 'does not prune diffs' do
          diff = nil

          subject.each do |d|
            diff = d
          end

          expect(diff.diff).not_to eq('')
        end
      end

      context 'when no collapse is unset' do
367
        let(:expanded) { false }
Robert Speicher committed
368 369

        it 'yields Diff instances even when they are quite big' do
370 371
          expect { |b| subject.each(&b) }
            .to yield_with_args(an_instance_of(Gitlab::Git::Diff))
Robert Speicher committed
372 373 374 375 376 377 378 379 380 381 382 383 384
        end

        it 'prunes diffs that are quite big' do
          diff = nil

          subject.each do |d|
            diff = d
          end

          expect(diff.diff).to eq('')
        end

        context 'when go over safe limits on files' do
385
          let(:iterator) { [fake_diff(1, 1)] * 4 }
Robert Speicher committed
386

387
          before do
Robert Speicher committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
            stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', { max_files: 2, max_lines: max_lines })
          end

          it 'prunes diffs by default even little ones' do
            subject.each_with_index do |d, i|
              if i < 2
                expect(d.diff).not_to eq('')
              else # 90 lines
                expect(d.diff).to eq('')
              end
            end
          end
        end

        context 'when go over safe limits on lines' do
          let(:iterator) do
            [
              fake_diff(1, 45),
              fake_diff(1, 45),
              fake_diff(1, 20480),
              fake_diff(1, 1)
            ]
          end

412
          before do
Robert Speicher committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
            stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', { max_files: max_files, max_lines: 80 })
          end

          it 'prunes diffs by default even little ones' do
            subject.each_with_index do |d, i|
              if i < 2
                expect(d.diff).not_to eq('')
              else # 90 lines
                expect(d.diff).to eq('')
              end
            end
          end
        end

        context 'when go over safe limits on bytes' do
          let(:iterator) do
            [
              fake_diff(1, 45),
              fake_diff(1, 45),
              fake_diff(1, 20480),
              fake_diff(1, 1)
            ]
          end

437
          before do
Robert Speicher committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
            stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', { max_files: max_files, max_lines: 80 })
          end

          it 'prunes diffs by default even little ones' do
            subject.each_with_index do |d, i|
              if i < 2
                expect(d.diff).not_to eq('')
              else # > 80 bytes
                expect(d.diff).to eq('')
              end
            end
          end
        end
      end

      context 'when limiting is disabled' do
454
        let(:limits) { false }
Robert Speicher committed
455 456

        it 'yields Diff instances even when they are quite big' do
457 458
          expect { |b| subject.each(&b) }
            .to yield_with_args(an_instance_of(Gitlab::Git::Diff))
Robert Speicher committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
        end

        it 'does not prune diffs' do
          diff = nil

          subject.each do |d|
            diff = d
          end

          expect(diff.diff).not_to eq('')
        end
      end
    end
  end

  def fake_diff(line_length, line_count)
    { 'diff' => "#{'a' * line_length}\n" * line_count }
  end
477 478 479 480 481 482 483 484 485 486

  class MutatingConstantIterator
    include Enumerable

    def initialize(count, value)
      @count = count
      @value = value
    end

    def each
487 488
      return enum_for(:each) unless block_given?

489 490 491 492 493 494 495 496
      loop do
        break if @count.zero?
        # It is critical to decrement before yielding. We may never reach the lines after 'yield'.
        @count -= 1
        yield @value
      end
    end
  end
Robert Speicher committed
497
end