2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-20 08:01:06 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Branches::CreateService do
|
2019-12-03 13:06:49 -05:00
|
|
|
subject(:service) { described_class.new(project, user) }
|
2017-03-20 08:01:06 -04:00
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
let_it_be(:project) { create(:project_empty_repo) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2017-03-20 08:01:06 -04:00
|
|
|
describe '#execute' do
|
|
|
|
context 'when repository is empty' do
|
|
|
|
it 'creates master branch' do
|
|
|
|
service.execute('my-feature', 'master')
|
|
|
|
|
|
|
|
expect(project.repository.branch_exists?('master')).to be_truthy
|
|
|
|
end
|
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
it 'creates another-feature branch' do
|
|
|
|
service.execute('another-feature', 'master')
|
2017-03-20 08:01:06 -04:00
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
expect(project.repository.branch_exists?('another-feature')).to be_truthy
|
2017-03-20 08:01:06 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-21 08:06:14 -04:00
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
context 'when branch already exists' do
|
|
|
|
it 'returns an error' do
|
|
|
|
result = service.execute('master', 'master')
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Branch already exists')
|
|
|
|
end
|
|
|
|
end
|
2019-10-21 08:06:14 -04:00
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
context 'when incorrect reference is provided' do
|
2019-10-21 08:06:14 -04:00
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:add_branch).and_return(false)
|
|
|
|
end
|
|
|
|
|
2020-05-12 11:10:33 -04:00
|
|
|
it 'returns an error with a reference name' do
|
|
|
|
result = service.execute('new-feature', 'unknown')
|
2019-10-21 08:06:14 -04:00
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
2020-05-12 11:10:33 -04:00
|
|
|
expect(result[:message]).to eq('Invalid reference name: unknown')
|
2019-10-21 08:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
2020-08-05 23:10:05 -04:00
|
|
|
|
|
|
|
it 'logs and returns an error if there is a PreReceiveError exception' do
|
|
|
|
error_message = 'pre receive error'
|
|
|
|
raw_message = "GitLab: #{error_message}"
|
|
|
|
pre_receive_error = Gitlab::Git::PreReceiveError.new(raw_message)
|
|
|
|
|
|
|
|
allow(project.repository).to receive(:add_branch).and_raise(pre_receive_error)
|
|
|
|
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
|
|
|
|
pre_receive_error,
|
|
|
|
pre_receive_message: raw_message,
|
|
|
|
branch_name: 'new-feature',
|
|
|
|
ref: 'unknown'
|
|
|
|
)
|
|
|
|
|
|
|
|
result = service.execute('new-feature', 'unknown')
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq(error_message)
|
|
|
|
end
|
2017-03-20 08:01:06 -04:00
|
|
|
end
|
|
|
|
end
|