Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

commit_range_spec.rb 4.21 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1
2
3
4
5
6
7
8
9
10
11
12
13
require 'spec_helper'

describe CommitRange do
  let(:sha_from) { 'f3f85602' }
  let(:sha_to)   { 'e86e1013' }

  let(:range)  { described_class.new("#{sha_from}...#{sha_to}") }
  let(:range2) { described_class.new("#{sha_from}..#{sha_to}") }

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

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Referable) }
  end

  describe '#to_reference' do
    let(:project) { double('project', to_reference: 'namespace1/project') }

    before do
      range.project = project
    end

    it 'returns a String reference to the object' do
      expect(range.to_reference).to eq range.to_s
    end

    it 'supports a cross-project reference' do
      cross = double('project')
      expect(range.to_reference(cross)).to eq "#{project.to_reference}@#{range.to_s}"
    end
  end

Robert Speicher's avatar
Robert Speicher committed
37
  describe '#to_s' do
38
39
    it 'is correct for three-dot syntax' do
      expect(range.to_s).to eq "#{sha_from[0..7]}...#{sha_to[0..7]}"
Robert Speicher's avatar
Robert Speicher committed
40
41
    end

42
43
    it 'is correct for two-dot syntax' do
      expect(range2.to_s).to eq "#{sha_from[0..7]}..#{sha_to[0..7]}"
Robert Speicher's avatar
Robert Speicher committed
44
45
46
47
    end
  end

  describe '#reference_title' do
48
    it 'returns the correct String for three-dot ranges' do
Robert Speicher's avatar
Robert Speicher committed
49
50
      expect(range.reference_title).to eq "Commits #{sha_from} through #{sha_to}"
    end
51
52
53
54

    it 'returns the correct String for two-dot ranges' do
      expect(range2.reference_title).to eq "Commits #{sha_from}^ through #{sha_to}"
    end
Robert Speicher's avatar
Robert Speicher committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  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
      expect(range.to_param).to eq({from: sha_from, to: sha_to})
    end

    it 'includes the correct values for a two-dot range' do
      expect(range2.to_param).to eq({from: sha_from + '^', to: sha_to})
    end
  end

71
  describe '#exclude_start?' do
Robert Speicher's avatar
Robert Speicher committed
72
    it 'is false for three-dot ranges' do
73
      expect(range.exclude_start?).to eq false
Robert Speicher's avatar
Robert Speicher committed
74
75
76
    end

    it 'is true for two-dot ranges' do
77
      expect(range2.exclude_start?).to eq true
Robert Speicher's avatar
Robert Speicher committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    end
  end

  describe '#valid_commits?' do
    context 'without a project' do
      it 'returns nil' do
        expect(range.valid_commits?).to be_nil
      end
    end

    it 'accepts an optional project argument' do
      project1 = double('project1').as_null_object
      project2 = double('project2').as_null_object

      # project1 gets assigned through the accessor, but ignored when not given
      # as an argument to `valid_commits?`
      expect(project1).not_to receive(:present?)
      range.project = project1

      # project2 gets passed to `valid_commits?`
      expect(project2).to receive(:present?).and_return(false)

      range.valid_commits?(project2)
    end

    context 'with a project' do
      let(:project) { double('project', repository: double('repository')) }

      context 'with a valid repo' do
        before do
          expect(project).to receive(:valid_repo?).and_return(true)
          range.project = project
        end

        it 'is false when `sha_from` is invalid' do
          expect(project.repository).to receive(:commit).with(sha_from).and_return(false)
          expect(project.repository).not_to receive(:commit).with(sha_to)
          expect(range).not_to be_valid_commits
        end

        it 'is false when `sha_to` is invalid' do
          expect(project.repository).to receive(:commit).with(sha_from).and_return(true)
          expect(project.repository).to receive(:commit).with(sha_to).and_return(false)
          expect(range).not_to be_valid_commits
        end

        it 'is true when both `sha_from` and `sha_to` are valid' do
          expect(project.repository).to receive(:commit).with(sha_from).and_return(true)
          expect(project.repository).to receive(:commit).with(sha_to).and_return(true)
          expect(range).to be_valid_commits
        end
      end

      context 'without a valid repo' do
        before do
          expect(project).to receive(:valid_repo?).and_return(false)
          range.project = project
        end

        it 'returns false' do
          expect(range).not_to be_valid_commits
        end
      end
    end
  end
end