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

3 4 5 6 7 8 9 10 11 12 13 14 15 16
module TestEnv
  extend self

  # Test environment
  #
  # all repositories and namespaces stored at
  # RAILS_APP/tmp/test-git-base-path
  #
  # Next shell methods are stubbed and return true
  # -  mv_repository
  # -  remove_repository
  # -  add_key
  # -  remove_key
  #
17
  def init(opts = {})
D
Dmitriy Zaporozhets 已提交
18 19
    RSpec::Mocks::setup(self)

20 21 22 23 24 25 26 27
    # Disable observers to improve test speed
    #
    # You can enable it in whole test case where needed by next string:
    #
    #   before(:each) { enable_observers }
    #
    disable_observers if opts[:observers] == false

D
Dmitriy Zaporozhets 已提交
28 29
    # Disable mailer for spinach tests
    disable_mailer if opts[:mailer] == false
I
Izaak Alpert 已提交
30
    setup_stubs
D
Dmitriy Zaporozhets 已提交
31 32


I
Izaak Alpert 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    clear_test_repo_dir if opts[:init_repos] == true
    setup_test_repos(opts) if opts[:repos] == true
  end

  def testing_path
    Rails.root.join('tmp', 'test-git-base-path')
  end

  def seed_repo_path
    Rails.root.join('tmp', 'repositories', 'gitlabhq')
  end

  def seed_satellite_path
    Rails.root.join('tmp', 'satellite', 'gitlabhq')
  end

  def satellite_path
    "#{testing_path()}/satellite"
  end

  def repo(namespace, name)
    unless (namespace.nil? || namespace.path.nil? || namespace.path.strip.empty?)
      repo = File.join(testing_path(), "#{namespace.path}/#{name}.git")
    else
      repo = File.join(testing_path(), "#{name}.git")
    end
  end

  def satellite(namespace, name)
    unless (namespace.nil? || namespace.path.nil? || namespace.path.strip.empty?)
      satellite_repo = File.join(satellite_path, namespace.path, name)
    else
      satellite_repo = File.join(satellite_path, name)
    end
  end

69

I
Izaak Alpert 已提交
70 71 72 73 74 75 76 77 78
  def setup_test_repos(opts ={})
    create_repo(nil, 'gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('')
    create_repo(nil, 'source_gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('source_')
    create_repo(nil, 'target_gitlabhq') #unless opts[:repo].nil? || !opts[:repo].include?('target_')
  end

  def setup_stubs()
    # Use tmp dir for FS manipulations
    repos_path = testing_path()
79
    GollumWiki.any_instance.stub(:init_repo) do |path|
80 81 82
      create_temp_repo(File.join(repos_path, "#{path}.git"))
    end

I
Izaak Alpert 已提交
83 84 85 86 87 88
    Gitlab.config.gitlab_shell.stub(repos_path: repos_path)

    Gitlab.config.satellites.stub(path: satellite_path)

    Gitlab::Git::Repository.stub(repos_path: repos_path)

89
    Gitlab::Shell.any_instance.stub(
90
      add_repository: true,
91 92
      mv_repository: true,
      remove_repository: true,
D
Dmitriy Zaporozhets 已提交
93
      update_repository_head: true,
94 95 96 97
      add_key: true,
      remove_key: true
    )

D
Dmitriy Zaporozhets 已提交
98
    Gitlab::Satellite::Satellite.any_instance.stub(
I
Izaak Alpert 已提交
99 100 101 102
        exists?: true,
        destroy: true,
        create: true,
        lock_files_dir: repos_path
103 104 105
    )

    MergeRequest.any_instance.stub(
I
Izaak Alpert 已提交
106
        check_if_can_be_merged: true
107 108
    )
    Repository.any_instance.stub(
I
Izaak Alpert 已提交
109
        size: 12.45
110
    )
I
Izaak Alpert 已提交
111
  end
112

I
Izaak Alpert 已提交
113 114 115 116
  def clear_test_repo_dir
    setup_stubs
    # Use tmp dir for FS manipulations
    repos_path = testing_path()
117 118 119 120 121
    # Remove tmp/test-git-base-path
    FileUtils.rm_rf Gitlab.config.gitlab_shell.repos_path

    # Recreate tmp/test-git-base-path
    FileUtils.mkdir_p Gitlab.config.gitlab_shell.repos_path
I
Izaak Alpert 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    #Since much more is happening in satellites
    FileUtils.mkdir_p Gitlab.config.satellites.path
  end

  def clear_repo_dir(namespace, name)
    setup_stubs
    #Clean any .wiki.git that may have been created
    FileUtils.rm_rf File.join(testing_path(), "#{name}.wiki.git")
  end

  #Create a repo and it's satellite
  def create_repo(namespace, name)
    setup_stubs
    repo = repo(namespace, name)

    # Symlink tmp/repositories/gitlabhq to tmp/test-git-base-path/gitlabhq
    system("ln -s -f #{seed_repo_path()} #{repo}")
    create_satellite(repo, namespace, name)
  end

  # Create a testing satellite, and clone the source repo into it
  def create_satellite(source_repo, namespace, satellite_name)
    satellite_repo = satellite(namespace, satellite_name)
    # Symlink tmp/satellite/gitlabhq to tmp/test-git-base-path/satellite/gitlabhq
    system("ln -s -f #{seed_satellite_path()} #{satellite_repo}")
147 148 149 150 151 152 153
  end

  def create_temp_repo(path)
    FileUtils.mkdir_p path
    command = "git init --quiet --bare #{path};"
    system(command)
  end
154 155 156 157 158 159 160 161

  def enable_observers
    ActiveRecord::Base.observers.enable(:all)
  end

  def disable_observers
    ActiveRecord::Base.observers.disable(:all)
  end
D
Dmitriy Zaporozhets 已提交
162 163

  def disable_mailer
D
Dmitriy Zaporozhets 已提交
164
    NotificationService.any_instance.stub(mailer: double.as_null_object)
D
Dmitriy Zaporozhets 已提交
165
  end
166
end