BigW Consortium Gitlab

merge_request_diff_spec.rb 3.57 KB
Newer Older
1 2 3
require 'spec_helper'

describe MergeRequestDiff, models: true do
4
  describe 'create new record' do
5
    subject { create(:merge_request).merge_request_diff }
6 7

    it { expect(subject).to be_valid }
8
    it { expect(subject).to be_persisted }
9 10 11
    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') }
12 13
    it { expect(subject.base_commit_sha).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
    it { expect(subject.start_commit_sha).to eq('0b4bc9a49b562e85de7cc9e834518ea6828729b9') }
14 15
  end

16 17 18 19 20 21 22 23 24
  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

25 26 27 28 29 30 31 32 33
  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

34
        mr_diff.raw_diffs(ignore_whitespace_change: true)
35 36 37 38 39 40 41
      end
    end

    context 'when the raw diffs are empty' do
      before { mr_diff.update_attributes(st_diffs: '') }

      it 'returns an empty DiffCollection' do
42 43
        expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
        expect(mr_diff.raw_diffs).to be_empty
44 45 46
      end
    end

47 48 49 50 51 52 53 54 55 56
    context 'when the raw diffs have invalid content' do
      before { mr_diff.update_attributes(st_diffs: ["--broken-diff"]) }

      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

57 58
    context 'when the raw diffs exist' do
      it 'returns the diffs' do
59 60
        expect(mr_diff.raw_diffs).to be_a(Gitlab::Git::DiffCollection)
        expect(mr_diff.raw_diffs).not_to be_empty
61 62 63
      end

      context 'when the :paths option is set' do
64
        let(:diffs) { mr_diff.raw_diffs(paths: ['files/ruby/popen.rb', 'files/ruby/popen.rb']) }
65

66 67
        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')
68 69 70 71 72 73 74 75 76
        end

        it 'uses the diffs from the DB' do
          expect(mr_diff).to receive(:load_diffs)

          diffs
        end
      end
    end
77
  end
78

79 80 81 82
  describe '#commits_sha' do
    shared_examples 'returning all commits SHA' do
      it 'returns all commits SHA' do
        commits_sha = subject.commits_sha
83

84
        expect(commits_sha).to eq(subject.commits.map(&:sha))
85
      end
86
    end
87

88 89 90
    context 'when commits were loaded' do
      before do
        subject.commits
91 92
      end

93 94 95 96 97 98 99 100 101
      it_behaves_like 'returning all commits SHA'
    end

    context 'when commits were not loaded' do
      it_behaves_like 'returning all commits SHA'
    end
  end

  describe '#compare_with' do
102
    subject { create(:merge_request, source_branch: 'fix').merge_request_diff }
103 104 105 106

    it 'delegates compare to the service' do
      expect(CompareService).to receive(:new).and_call_original

107 108 109 110 111 112 113
      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)
114
    end
115 116
  end
end