BigW Consortium Gitlab

commit_range_spec.rb 4.61 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe CommitRange, models: true do
4 5 6 7 8 9
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(Referable) }
  end

10
  let!(:project) { create(:project, :public, :repository) }
11 12
  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 29 30 31 32 33 34 35 36
  describe '#initialize' do
    it 'does not modify strings in-place' do
      input = "#{sha_from}...#{sha_to}   "

      described_class.new(input, project)

      expect(input).to eq("#{sha_from}...#{sha_to}   ")
    end
  end

37 38
  describe '#to_s' do
    it 'is correct for three-dot syntax' do
39
      expect(range.to_s).to eq "#{full_sha_from}...#{full_sha_to}"
40
    end
41

42
    it 'is correct for two-dot syntax' do
43
      expect(range2.to_s).to eq "#{full_sha_from}..#{full_sha_to}"
44
    end
45 46 47
  end

  describe '#to_reference' do
48
    let(:cross) { create(:empty_project, namespace: project.namespace) }
49

50
    it 'returns a String reference to the object' do
51
      expect(range.to_reference).to eq "#{full_sha_from}...#{full_sha_to}"
52 53 54
    end

    it 'returns a String reference to the object' do
55
      expect(range2.to_reference).to eq "#{full_sha_from}..#{full_sha_to}"
56 57 58
    end

    it 'supports a cross-project reference' do
59
      expect(range.to_reference(cross)).to eq "#{project.path}@#{full_sha_from}...#{full_sha_to}"
60 61 62 63
    end
  end

  describe '#reference_link_text' do
64
    let(:cross) { create(:empty_project, namespace: project.namespace) }
65 66 67 68 69 70 71 72 73 74

    it 'returns a String reference to the object' do
      expect(range.reference_link_text).to eq "#{sha_from}...#{sha_to}"
    end

    it 'returns a String reference to the object' do
      expect(range2.reference_link_text).to eq "#{sha_from}..#{sha_to}"
    end

    it 'supports a cross-project reference' do
75
      expect(range.reference_link_text(cross)).to eq "#{project.path}@#{sha_from}...#{sha_to}"
76 77 78
    end
  end

79 80 81 82 83 84
  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
85
      expect(range.to_param).to eq({ from: full_sha_from, to: full_sha_to })
86 87 88
    end

    it 'includes the correct values for a two-dot range' do
89
      expect(range2.to_param).to eq({ from: full_sha_from + '^', to: full_sha_to })
90 91 92
    end
  end

93
  describe '#exclude_start?' do
94
    it 'is false for three-dot ranges' do
95
      expect(range.exclude_start?).to eq false
96 97 98
    end

    it 'is true for two-dot ranges' do
99
      expect(range2.exclude_start?).to eq true
100 101 102 103
    end
  end

  describe '#valid_commits?' do
104 105 106
    context 'with a valid repo' do
      before do
        expect(project).to receive(:valid_repo?).and_return(true)
107 108
      end

109 110 111
      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
112

113 114
        expect(range).not_to be_valid_commits
      end
115

116 117 118
      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)
119

120 121
        expect(range).not_to be_valid_commits
      end
122

123 124
      it 'is true when both `sha_from` and `sha_to` are valid' do
        expect(range).to be_valid_commits
125
      end
126
    end
127

128 129 130 131
    context 'without a valid repo' do
      before do
        expect(project).to receive(:valid_repo?).and_return(false)
      end
132

133 134
      it 'returns false' do
        expect(range).not_to be_valid_commits
135 136 137
      end
    end
  end
Yorick Peterse committed
138 139

  describe '#has_been_reverted?' do
140 141
    let(:issue) { create(:issue) }
    let(:user) { issue.author }
Yorick Peterse committed
142

143
    it 'returns true if the commit has been reverted' do
Yorick Peterse committed
144
      create(:note_on_issue,
145
             noteable: issue,
Yorick Peterse committed
146
             system: true,
147
             note: commit1.revert_description(user),
148
             project: issue.project)
Yorick Peterse committed
149 150

      expect_any_instance_of(Commit).to receive(:reverts_commit?).
151
        with(commit1, user).
Yorick Peterse committed
152 153
        and_return(true)

154
      expect(commit1.has_been_reverted?(user, issue)).to eq(true)
Yorick Peterse committed
155 156 157
    end

    it 'returns false a commit has not been reverted' do
158
      expect(commit1.has_been_reverted?(user, issue)).to eq(false)
Yorick Peterse committed
159 160
    end
  end
161
end