2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Branches::CreateService do
|
2020-01-01 13:55:28 +05:30
|
|
|
subject(:service) { described_class.new(project, user) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
let_it_be(:project) { create(:project_empty_repo) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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-24 23:13:21 +05:30
|
|
|
it 'creates another-feature branch' do
|
|
|
|
service.execute('another-feature', 'master')
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(project.repository.branch_exists?('another-feature')).to be_truthy
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
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-12-26 22:10:19 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when incorrect reference is provided' do
|
2019-12-26 22:10:19 +05:30
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:add_branch).and_return(false)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'returns an error with a reference name' do
|
2021-09-30 23:02:18 +05:30
|
|
|
err_msg = 'Failed to create branch \'new-feature\': invalid reference name \'unknown\''
|
2020-05-24 23:13:21 +05:30
|
|
|
result = service.execute('new-feature', 'unknown')
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(result[:message]).to eq(err_msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when an ambiguous branch name is provided' do
|
|
|
|
it 'returns an error that branch could not be created' do
|
|
|
|
err_msg = 'Failed to create branch \'feature\': 13:reference is ambiguous.'
|
|
|
|
|
|
|
|
service.execute('feature/widget', 'master')
|
|
|
|
result = service.execute('feature', 'master')
|
|
|
|
|
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq(err_msg)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
|
2020-10-24 23:57:45 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|