test_env.rb 4.7 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
    clear_test_repo_dir if opts[:init_repos] == true
    setup_test_repos(opts) if opts[:repos] == true
  end

37 38
  def enable_observers
    ActiveRecord::Base.observers.enable(:all)
I
Izaak Alpert 已提交
39 40
  end

41 42
  def disable_observers
    ActiveRecord::Base.observers.disable(:all)
I
Izaak Alpert 已提交
43 44
  end

45 46
  def disable_mailer
    NotificationService.any_instance.stub(mailer: double.as_null_object)
I
Izaak Alpert 已提交
47
  end
D
Dmitriy Zaporozhets 已提交
48

49 50
  def enable_mailer
    NotificationService.any_instance.unstub(:mailer)
I
Izaak Alpert 已提交
51 52 53 54 55
  end

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

I
Izaak Alpert 已提交
60 61 62 63 64 65
    Gitlab.config.gitlab_shell.stub(repos_path: repos_path)

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

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

66
    Gitlab::Shell.any_instance.stub(
67
      add_repository: true,
68 69
      mv_repository: true,
      remove_repository: true,
D
Dmitriy Zaporozhets 已提交
70
      update_repository_head: true,
71 72 73 74
      add_key: true,
      remove_key: true
    )

D
Dmitriy Zaporozhets 已提交
75
    Gitlab::Satellite::Satellite.any_instance.stub(
76 77 78 79
      exists?: true,
      destroy: true,
      create: true,
      lock_files_dir: repos_path
80 81 82
    )

    MergeRequest.any_instance.stub(
83
      check_if_can_be_merged: true
84 85
    )
    Repository.any_instance.stub(
86
      size: 12.45
87
    )
88 89

    ActivityObserver.any_instance.stub(
D
Dmitriy Zaporozhets 已提交
90
      current_user: double("current_user", id: 1)
91
    )
I
Izaak Alpert 已提交
92
  end
93

I
Izaak Alpert 已提交
94 95
  def clear_repo_dir(namespace, name)
    setup_stubs
96
    # Clean any .wiki.git that may have been created
I
Izaak Alpert 已提交
97 98 99
    FileUtils.rm_rf File.join(testing_path(), "#{name}.wiki.git")
  end

100
  # Create a repo and it's satellite
I
Izaak Alpert 已提交
101 102 103 104 105
  def create_repo(namespace, name)
    setup_stubs
    repo = repo(namespace, name)

    # Symlink tmp/repositories/gitlabhq to tmp/test-git-base-path/gitlabhq
D
Dmitriy Zaporozhets 已提交
106
    system("ln -s -f #{seed_repo_path()} #{repo}")
I
Izaak Alpert 已提交
107 108 109
    create_satellite(repo, namespace, name)
  end

110 111 112 113 114 115 116 117 118 119 120 121 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 147 148 149 150 151 152 153 154 155 156 157 158 159
  private

  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

  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 clear_test_repo_dir
    setup_stubs
    # Use tmp dir for FS manipulations
    repos_path = testing_path()
    # 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

160
    # Since much more is happening in satellites
161 162 163
    FileUtils.mkdir_p Gitlab.config.satellites.path
  end

I
Izaak Alpert 已提交
164 165 166
  # 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)
I
Izaak Alpert 已提交
167 168
    # Symlink tmp/satellite/gitlabhq to tmp/test-git-base-path/satellite/gitlabhq, create the directory if it doesn't exist already
    satellite_dir = File.dirname(satellite_repo)
169
    FileUtils.mkdir_p(satellite_dir) unless File.exists?(satellite_dir)
D
Dmitriy Zaporozhets 已提交
170
    system("ln -s -f #{seed_satellite_path} #{satellite_repo}")
171 172 173 174
  end

  def create_temp_repo(path)
    FileUtils.mkdir_p path
D
Dmitriy Zaporozhets 已提交
175 176
    command = "git init --quiet --bare #{path};"
    system(command)
177 178
  end
end