summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git/patches/commit_patches_spec.rb
blob: 760112155ce2d15b63344d9e95f662c0652c6e49 (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
# frozen_string_literal: true
require 'spec_helper'

describe Gitlab::Git::Patches::CommitPatches do
  describe '#commit' do
    let(:patches) do
      patches_folder = Rails.root.join('spec/fixtures/patchfiles')
      content_1 = File.read(File.join(patches_folder, "0001-This-does-not-apply-to-the-feature-branch.patch"))
      content_2 = File.read(File.join(patches_folder, "0001-A-commit-from-a-patch.patch"))

      Gitlab::Git::Patches::Collection.new([content_1, content_2])
    end
    let(:user) { build(:user) }
    let(:branch_name) { 'branch-with-patches' }
    let(:repository) { create(:project, :repository).repository }

    subject(:commit_patches) do
      described_class.new(user, repository, branch_name, patches)
    end

    it 'applies the patches' do
      new_rev = commit_patches.commit

      expect(repository.commit(new_rev)).not_to be_nil
    end

    it 'updates the branch cache' do
      expect(repository).to receive(:after_create_branch)

      commit_patches.commit
    end

    context 'when the repository does not exist' do
      let(:repository) { create(:project).repository }

      it 'raises the correct error' do
        expect { commit_patches.commit }.to raise_error(Gitlab::Git::Repository::NoRepository)
      end
    end

    context 'when the patch does not apply' do
      let(:branch_name) { 'feature' }

      it 'raises the correct error' do
        expect { commit_patches.commit }.to raise_error(Gitlab::Git::CommandError)
      end
    end
  end
end