Add Project#ensure_repository

This commit is contained in:
Douglas Barbosa Alexandre 2017-06-28 10:42:51 -03:00
parent 4f620eb9e7
commit 2a64607b98
2 changed files with 35 additions and 0 deletions

View file

@ -1094,6 +1094,10 @@ class Project < ActiveRecord::Base
end
end
def ensure_repository
create_repository unless repository_exists?
end
def repository_exists?
!!repository.exists?
end

View file

@ -1327,6 +1327,37 @@ describe Project, models: true do
end
end
describe '#ensure_repository' do
let(:project) { create(:project, :repository) }
let(:shell) { Gitlab::Shell.new }
before do
allow(project).to receive(:gitlab_shell).and_return(shell)
end
it 'creates the repository if it not exist' do
allow(project).to receive(:repository_exists?)
.and_return(false)
allow(shell).to receive(:add_repository)
.with(project.repository_storage_path, project.path_with_namespace)
.and_return(true)
expect(project).to receive(:create_repository)
project.ensure_repository
end
it 'does not create the repository if it exists' do
allow(project).to receive(:repository_exists?)
.and_return(true)
expect(project).not_to receive(:create_repository)
project.ensure_repository
end
end
describe '#user_can_push_to_empty_repo?' do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }