Skip to content
repository_fork_worker_spec.rb 2.25 KiB
Newer Older
require 'spec_helper'

describe RepositoryForkWorker do
  let(:project) { create(:project, :repository, :import_scheduled) }
  let(:fork_project) { create(:project, :repository, forked_from_project: project) }
  let(:shell) { Gitlab::Shell.new }
  subject { described_class.new }
  before do
    allow(subject).to receive(:gitlab_shell).and_return(shell)
  end

  describe "#perform" do
    it "creates a new repository from a fork" do
      expect(shell).to receive(:fork_repository).with(
        fork_project.namespace.full_path
Gabriel Mazetto's avatar
Gabriel Mazetto committed
      ).and_return(true)
Gabriel Mazetto's avatar
Gabriel Mazetto committed
      subject.perform(
        project.id,
        project.full_path,
        fork_project.namespace.full_path)
    it 'flushes various caches' do
      expect(shell).to receive(:fork_repository).with(
        fork_project.namespace.full_path
      expect_any_instance_of(Repository).to receive(:expire_emptiness_caches)
        .and_call_original
      expect_any_instance_of(Repository).to receive(:expire_exists_cache)
        .and_call_original
      subject.perform(project.id, '/test/path', project.full_path,
                      fork_project.namespace.full_path)
      source_path = project.full_path
      target_path = fork_project.namespace.full_path
      error_message = "Unable to fork project #{project.id} for repository #{source_path} -> #{target_path}"

      expect(shell).to receive(:fork_repository).and_return(false)

      expect do
        subject.perform(project.id, '/test/path', source_path, target_path)
      end.to raise_error(RepositoryForkWorker::ForkError, error_message)
    end
    it 'handles unexpected error' do
      source_path = project.full_path
      target_path = fork_project.namespace.full_path

      allow_any_instance_of(Gitlab::Shell).to receive(:fork_repository).and_raise(RuntimeError)

      expect do
        subject.perform(project.id, '/test/path', source_path, target_path)
      end.to raise_error(RepositoryForkWorker::ForkError)
      expect(project.reload.import_status).to eq('failed')