summaryrefslogtreecommitdiff
path: root/spec/services/branches/delete_merged_service_spec.rb
blob: 2cf0f53c8c3a989e09c60f760774b47c8ea017f1 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Branches::DeleteMergedService do
  include ProjectForksHelper

  subject(:service) { described_class.new(project, project.owner) }

  let(:project) { create(:project, :repository) }

  describe '#execute' do
    it 'deletes a branch that was merged' do
      service.execute

      expect(project.repository.branch_names).not_to include('improve/awesome')
    end

    it 'keeps branch that is unmerged' do
      service.execute

      expect(project.repository.branch_names).to include('feature')
    end

    it 'keeps "master"' do
      service.execute

      expect(project.repository.branch_names).to include('master')
    end

    it 'keeps protected branches' do
      create(:protected_branch, project: project, name: 'improve/awesome')

      service.execute

      expect(project.repository.branch_names).to include('improve/awesome')
    end

    it 'keeps wildcard protected branches' do
      create(:protected_branch, project: project, name: 'improve/*')

      service.execute

      expect(project.repository.branch_names).to include('improve/awesome')
    end

    it 'ignores protected tags' do
      create(:protected_tag, project: project, name: 'improve/*')

      service.execute

      expect(project.repository.branch_names).not_to include('improve/awesome')
    end

    context 'user without rights' do
      let(:user) { create(:user) }

      it 'cannot execute' do
        expect { described_class.new(project, user).execute }.to raise_error(Gitlab::Access::AccessDeniedError)
      end
    end

    context 'open merge requests' do
      it 'does not delete branches from open merge requests' do
        forked_project = fork_project(project)
        create(:merge_request, :opened, source_project: project, target_project: project, source_branch: 'branch-merged', target_branch: 'master')
        create(:merge_request, :opened, source_project: forked_project, target_project: project, target_branch: 'improve/awesome', source_branch: 'master')

        service.execute

        expect(project.repository.branch_names).to include('branch-merged')
        expect(project.repository.branch_names).to include('improve/awesome')
      end
    end
  end

  describe '#async_execute' do
    it 'calls DeleteMergedBranchesWorker async' do
      expect(DeleteMergedBranchesWorker).to receive(:perform_async)

      service.async_execute
    end
  end
end