BigW Consortium Gitlab

commit_range_spec.rb 3.47 KB
Newer Older
1 2 3
require 'spec_helper'

describe CommitRange do
4 5 6 7 8 9
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Referable) }
  end

10 11 12
  let!(:project) { create(:project, :public) }
  let!(:commit1) { project.commit("HEAD~2") }
  let!(:commit2) { project.commit }
13

14 15 16 17 18 19 20 21
  let(:sha_from) { commit1.short_id }
  let(:sha_to)   { commit2.short_id }

  let(:full_sha_from) { commit1.id }
  let(:full_sha_to)   { commit2.id }

  let(:range)  { described_class.new("#{sha_from}...#{sha_to}", project) }
  let(:range2) { described_class.new("#{sha_from}..#{sha_to}", project) }
22 23

  it 'raises ArgumentError when given an invalid range string' do
24
    expect { described_class.new("Foo", project) }.to raise_error(ArgumentError)
25 26
  end

27 28
  describe '#to_s' do
    it 'is correct for three-dot syntax' do
29
      expect(range.to_s).to eq "#{full_sha_from}...#{full_sha_to}"
30
    end
31

32
    it 'is correct for two-dot syntax' do
33
      expect(range2.to_s).to eq "#{full_sha_from}..#{full_sha_to}"
34
    end
35 36 37
  end

  describe '#to_reference' do
38
    let(:cross) { create(:project) }
39

40 41
    it 'returns a String reference to the object' do
      expect(range.to_reference).to eq "#{sha_from}...#{sha_to}"
42 43 44
    end

    it 'returns a String reference to the object' do
45
      expect(range2.to_reference).to eq "#{sha_from}..#{sha_to}"
46 47 48
    end

    it 'supports a cross-project reference' do
49
      expect(range.to_reference(cross)).to eq "#{project.to_reference}@#{sha_from}...#{sha_to}"
50 51 52
    end
  end

53
  describe '#reference_title' do
54
    it 'returns the correct String for three-dot ranges' do
55
      expect(range.reference_title).to eq "Commits #{full_sha_from} through #{full_sha_to}"
56
    end
57 58

    it 'returns the correct String for two-dot ranges' do
59
      expect(range2.reference_title).to eq "Commits #{full_sha_from}^ through #{full_sha_to}"
60
    end
61 62 63 64 65 66 67 68
  end

  describe '#to_param' do
    it 'includes the correct keys' do
      expect(range.to_param.keys).to eq %i(from to)
    end

    it 'includes the correct values for a three-dot range' do
69
      expect(range.to_param).to eq({ from: full_sha_from, to: full_sha_to })
70 71 72
    end

    it 'includes the correct values for a two-dot range' do
73
      expect(range2.to_param).to eq({ from: full_sha_from + '^', to: full_sha_to })
74 75 76
    end
  end

77
  describe '#exclude_start?' do
78
    it 'is false for three-dot ranges' do
79
      expect(range.exclude_start?).to eq false
80 81 82
    end

    it 'is true for two-dot ranges' do
83
      expect(range2.exclude_start?).to eq true
84 85 86 87
    end
  end

  describe '#valid_commits?' do
88 89 90
    context 'with a valid repo' do
      before do
        expect(project).to receive(:valid_repo?).and_return(true)
91 92
      end

93 94 95
      it 'is false when `sha_from` is invalid' do
        expect(project).to receive(:commit).with(sha_from).and_return(nil)
        expect(project).to receive(:commit).with(sha_to).and_call_original
96

97 98
        expect(range).not_to be_valid_commits
      end
99

100 101 102
      it 'is false when `sha_to` is invalid' do
        expect(project).to receive(:commit).with(sha_from).and_call_original
        expect(project).to receive(:commit).with(sha_to).and_return(nil)
103

104 105
        expect(range).not_to be_valid_commits
      end
106

107 108
      it 'is true when both `sha_from` and `sha_to` are valid' do
        expect(range).to be_valid_commits
109
      end
110
    end
111

112 113 114 115
    context 'without a valid repo' do
      before do
        expect(project).to receive(:valid_repo?).and_return(false)
      end
116

117 118
      it 'returns false' do
        expect(range).not_to be_valid_commits
119 120 121 122
      end
    end
  end
end