Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_env.rb « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3230301eead96f10fd4cf4f40d543a00f85e060c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'rspec/mocks'

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
  #
  def init(opts = {})
    RSpec::Mocks::setup(self)

    # 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

    # Disable mailer for spinach tests
    disable_mailer if opts[:mailer] == false


    # Use tmp dir for FS manipulations
    repos_path = Rails.root.join('tmp', 'test-git-base-path')
    Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
    Gitlab::Git::Repository.stub(repos_path: repos_path)

    GollumWiki.any_instance.stub(:init_repo) do |path|
      create_temp_repo(File.join(repos_path, "#{path}.git"))
    end

    Gitlab::Shell.any_instance.stub(
      add_repository: true,
      mv_repository: true,
      remove_repository: true,
      update_repository_head: true,
      add_key: true,
      remove_key: true
    )

    Gitlab::Satellite::Satellite.any_instance.stub(
      exists?: true,
      destroy: true,
      create: true
    )

    MergeRequest.any_instance.stub(
      check_if_can_be_merged: true
    )

    Repository.any_instance.stub(
      size: 12.45
    )

    # 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
  end

  def create_temp_repo(path)
    FileUtils.mkdir_p path
    command = "git init --quiet --bare #{path};"
    system(command)
  end

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

  def disable_observers
    ActiveRecord::Base.observers.disable(:all)
  end

  def disable_mailer
    NotificationService.any_instance.stub(mailer: double.as_null_object)
  end
end