test_env.rb 1.6 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
require 'rspec/mocks'

3 4 5 6 7
module TestEnv
  extend self

  # Test environment
  #
8
  # See gitlab.yml.example test section for paths
9
  #
10
  def init(opts = {})
D
Dmitriy Zaporozhets 已提交
11 12
    RSpec::Mocks::setup(self)

D
Dmitriy Zaporozhets 已提交
13 14 15
    # Disable mailer for spinach tests
    disable_mailer if opts[:mailer] == false

16 17
    # Clean /tmp/tests
    tmp_test_path = Rails.root.join('tmp', 'tests')
18 19 20 21 22 23

    if File.directory?(tmp_test_path)
      FileUtils.rm_r(tmp_test_path)
    end

    FileUtils.mkdir_p(tmp_test_path)
24

25 26 27
    # Setup GitLab shell for test instance
    setup_gitlab_shell

28 29
    # Create repository for FactoryGirl.create(:project)
    setup_factory_repo
I
Izaak Alpert 已提交
30 31
  end

32 33
  def disable_mailer
    NotificationService.any_instance.stub(mailer: double.as_null_object)
I
Izaak Alpert 已提交
34
  end
D
Dmitriy Zaporozhets 已提交
35

36 37
  def enable_mailer
    NotificationService.any_instance.unstub(:mailer)
I
Izaak Alpert 已提交
38 39
  end

40 41 42
  def setup_gitlab_shell
    unless File.directory?(Gitlab.config.gitlab_shell.path)
      %x[rake gitlab:shell:install]
43
    end
I
Izaak Alpert 已提交
44
  end
45

46 47
  def setup_factory_repo
    repo_path = repos_path + "/root/testme.git"
48
    clone_url = 'https://gitlab.com/gitlab-org/gitlab-test.git'
I
Izaak Alpert 已提交
49

50 51 52
    unless File.directory?(repo_path)
      git_cmd = %W(git clone --bare #{clone_url} #{repo_path})
      system(*git_cmd)
53 54 55
    end
  end

56
  def copy_repo(project)
D
Dmitriy Zaporozhets 已提交
57 58 59 60 61
    base_repo_path = File.expand_path(repos_path + "/root/testme.git")
    target_repo_path = File.expand_path(repos_path + "/#{project.namespace.path}/#{project.path}.git")
    FileUtils.mkdir_p(target_repo_path)
    FileUtils.cp_r("#{base_repo_path}/.", target_repo_path)
    FileUtils.chmod_R 0755, target_repo_path
62 63
  end

64 65
  def repos_path
    Gitlab.config.gitlab_shell.repos_path
66 67
  end
end