summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/branch_spec.rb
blob: 0df282d0ae312b7283fc22001e35007a4c422cbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
67
68
69
70
71
72
73
74
75
76
77
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
require "spec_helper"

describe Gitlab::Git::Branch, :seed_helper do
  let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
  let(:rugged) do
    Rugged::Repository.new(File.join(TestEnv.repos_path, repository.relative_path))
  end

  subject { repository.branches }

  it { is_expected.to be_kind_of Array }

  describe '.find' do
    subject { described_class.find(repository, branch) }

    before do
      allow(repository).to receive(:find_branch).with(branch)
        .and_call_original
    end

    context 'when finding branch via branch name' do
      let(:branch) { 'master' }

      it 'returns a branch object' do
        expect(subject).to be_a(described_class)
        expect(subject.name).to eq(branch)

        expect(repository).to have_received(:find_branch).with(branch)
      end
    end

    context 'when the branch is already a branch' do
      let(:commit) { repository.commit('master') }
      let(:branch) { described_class.new(repository, 'master', commit.sha, commit) }

      it 'returns a branch object' do
        expect(subject).to be_a(described_class)
        expect(subject).to eq(branch)

        expect(repository).not_to have_received(:find_branch).with(branch)
      end
    end
  end

  describe '#size' do
    subject { super().size }
    it { is_expected.to eq(SeedRepo::Repo::BRANCHES.size) }
  end

  describe 'first branch' do
    let(:branch) { repository.branches.first }

    it { expect(branch.name).to eq(SeedRepo::Repo::BRANCHES.first) }
    it { expect(branch.dereferenced_target.sha).to eq("0b4bc9a49b562e85de7cc9e834518ea6828729b9") }
  end

  describe 'master branch' do
    let(:branch) do
      repository.branches.find { |branch| branch.name == 'master' }
    end

    it { expect(branch.dereferenced_target.sha).to eq(SeedRepo::LastCommit::ID) }
  end

  context 'with active, stale and future branches' do
    let(:repository) do
      Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
    end

    let(:user) { create(:user) }
    let(:committer) do
      Gitlab::Git.committer_hash(email: user.email, name: user.name)
    end
    let(:params) do
      parents = [rugged.head.target]
      tree = parents.first.tree

      {
        message: 'commit message',
        author: committer,
        committer: committer,
        tree: tree,
        parents: parents
      }
    end
    let(:stale_sha) { Timecop.freeze(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago - 5.days) { create_commit } }
    let(:active_sha) { Timecop.freeze(Gitlab::Git::Branch::STALE_BRANCH_THRESHOLD.ago + 5.days) { create_commit } }
    let(:future_sha) { Timecop.freeze(100.days.since) { create_commit } }

    before do
      repository.create_branch('stale-1', stale_sha)
      repository.create_branch('active-1', active_sha)
      repository.create_branch('future-1', future_sha)
    end

    after do
      ensure_seeds
    end

    describe 'examine if the branch is active or stale' do
      let(:stale_branch) { repository.find_branch('stale-1') }
      let(:active_branch) { repository.find_branch('active-1') }
      let(:future_branch) { repository.find_branch('future-1') }

      describe '#active?' do
        it { expect(stale_branch.active?).to be_falsey }
        it { expect(active_branch.active?).to be_truthy }
        it { expect(future_branch.active?).to be_truthy }
      end

      describe '#stale?' do
        it { expect(stale_branch.stale?).to be_truthy }
        it { expect(active_branch.stale?).to be_falsey }
        it { expect(future_branch.stale?).to be_falsey }
      end

      describe '#state' do
        it { expect(stale_branch.state).to eq(:stale) }
        it { expect(active_branch.state).to eq(:active) }
        it { expect(future_branch.state).to eq(:active) }
      end
    end
  end

  it { expect(repository.branches.size).to eq(SeedRepo::Repo::BRANCHES.size) }

  def create_commit
    params[:message].delete!("\r")
    Rugged::Commit.create(rugged, params.merge(committer: committer.merge(time: Time.now)))
  end
end