summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/branch_formatter_spec.rb
blob: fc9d5204148f31259ec76c386d42b9d758b917f3 (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
require 'spec_helper'

describe Gitlab::GithubImport::BranchFormatter, lib: true do
  let(:project) { create(:project) }
  let(:commit) { create(:commit, project: project) }
  let(:repo) { double }
  let(:raw) do
    {
      ref: 'feature',
      repo: repo,
      sha: commit.id
    }
  end

  describe '#exists?' do
    it 'returns true when both branch, and commit exists' do
      branch = described_class.new(project, double(raw))

      expect(branch.exists?).to eq true
    end

    it 'returns false when branch does not exist' do
      branch = described_class.new(project, double(raw.merge(ref: 'removed-branch')))

      expect(branch.exists?).to eq false
    end

    it 'returns false when commit does not exist' do
      branch = described_class.new(project, double(raw.merge(sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b')))

      expect(branch.exists?).to eq false
    end
  end

  describe '#name' do
    it 'returns raw ref when branch exists' do
      branch = described_class.new(project, double(raw))

      expect(branch.name).to eq 'feature'
    end

    it 'returns formatted ref when branch does not exist' do
      branch = described_class.new(project, double(raw.merge(ref: 'removed-branch', sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b')))

      expect(branch.name).to eq 'removed-branch-2e5d3239'
    end
  end

  describe '#repo' do
    it 'returns raw repo' do
      branch = described_class.new(project, double(raw))

      expect(branch.repo).to eq repo
    end
  end

  describe '#sha' do
    it 'returns raw sha' do
      branch = described_class.new(project, double(raw))

      expect(branch.sha).to eq commit.id
    end
  end

  describe '#valid?' do
    it 'returns true when raw repo is present' do
      branch = described_class.new(project, double(raw))

      expect(branch.valid?).to eq true
    end

    it 'returns false when raw repo is blank' do
      branch = described_class.new(project, double(raw.merge(repo: nil)))

      expect(branch.valid?).to eq false
    end
  end
end