Commit 22817398e6c1cf9a479fecd99c55369fd81717cb

Authored by Dmitriy Zaporozhets
1 parent 7bb71bb0

define TestEnv and keep all global stubs in one place

features/support/env.rb
@@ -14,7 +14,7 @@ require 'spinach/capybara' @@ -14,7 +14,7 @@ require 'spinach/capybara'
14 require 'sidekiq/testing/inline' 14 require 'sidekiq/testing/inline'
15 15
16 16
17 -%w(stubbed_repository valid_commit select2_helper).each do |f| 17 +%w(valid_commit select2_helper test_env).each do |f|
18 require Rails.root.join('spec', 'support', f) 18 require Rails.root.join('spec', 'support', f)
19 end 19 end
20 20
@@ -35,13 +35,8 @@ Capybara.default_wait_time = 10 @@ -35,13 +35,8 @@ Capybara.default_wait_time = 10
35 DatabaseCleaner.strategy = :truncation 35 DatabaseCleaner.strategy = :truncation
36 36
37 Spinach.hooks.before_scenario do 37 Spinach.hooks.before_scenario do
38 - # Use tmp dir for FS manipulations  
39 - Gitlab.config.gitlab_shell.stub(repos_path: Rails.root.join('tmp', 'test-git-base-path'))  
40 - Gitlab::Shell.any_instance.stub(:add_repository) do |path|  
41 - create_temp_repo("#{Rails.root}/tmp/test-git-base-path/#{path}.git")  
42 - end  
43 - FileUtils.rm_rf Gitlab.config.gitlab_shell.repos_path  
44 - FileUtils.mkdir_p Gitlab.config.gitlab_shell.repos_path 38 + TestEnv.init
  39 +
45 DatabaseCleaner.start 40 DatabaseCleaner.start
46 end 41 end
47 42
@@ -54,9 +49,3 @@ Spinach.hooks.before_run do @@ -54,9 +49,3 @@ Spinach.hooks.before_run do
54 49
55 include FactoryGirl::Syntax::Methods 50 include FactoryGirl::Syntax::Methods
56 end 51 end
57 -  
58 -def create_temp_repo(path)  
59 - FileUtils.mkdir_p path  
60 - command = "git init --quiet --bare #{path};"  
61 - system(command)  
62 -end  
lib/gitlab/git/repository.rb
@@ -34,7 +34,11 @@ module Gitlab @@ -34,7 +34,11 @@ module Gitlab
34 end 34 end
35 35
36 def path_to_repo 36 def path_to_repo
37 - @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git") 37 + @path_to_repo ||= File.join(repos_path, "#{path_with_namespace}.git")
  38 + end
  39 +
  40 + def repos_path
  41 + Gitlab.config.gitlab_shell.repos_path
38 end 42 end
39 43
40 def repo 44 def repo
spec/factories.rb
@@ -25,7 +25,7 @@ FactoryGirl.define do @@ -25,7 +25,7 @@ FactoryGirl.define do
25 25
26 factory :project do 26 factory :project do
27 sequence(:name) { |n| "project#{n}" } 27 sequence(:name) { |n| "project#{n}" }
28 - path { name.downcase.gsub(/\s/, '_') } 28 + path { 'gitlabhq' }
29 creator 29 creator
30 end 30 end
31 31
spec/support/stubbed_repository.rb
@@ -1,71 +0,0 @@ @@ -1,71 +0,0 @@
1 -require "gitlab/git/repository"  
2 -require "project"  
3 -require "merge_request"  
4 -require "shell"  
5 -  
6 -# Stubs out all Git repository access done by models so that specs can run  
7 -# against fake repositories without Grit complaining that they don't exist.  
8 -class Project  
9 - def repository  
10 - if path == "empty" || !path  
11 - nil  
12 - else  
13 - GitLabTestRepo.new(Rails.root.join('tmp', 'repositories', 'gitlabhq'), 'master')  
14 - end  
15 - end  
16 -  
17 - def satellite  
18 - FakeSatellite.new  
19 - end  
20 -  
21 - class FakeSatellite  
22 - def exists?  
23 - true  
24 - end  
25 -  
26 - def destroy  
27 - true  
28 - end  
29 -  
30 - def create  
31 - true  
32 - end  
33 - end  
34 -end  
35 -  
36 -class MergeRequest  
37 - def check_if_can_be_merged  
38 - true  
39 - end  
40 -end  
41 -  
42 -class GitLabTestRepo < Repository  
43 - # patch repo size (in mb)  
44 - def size  
45 - 12.45  
46 - end  
47 -end  
48 -  
49 -module Gitlab  
50 - class Shell  
51 - def add_repository name  
52 - true  
53 - end  
54 -  
55 - def mv_repository name, new_name  
56 - true  
57 - end  
58 -  
59 - def remove_repository name  
60 - true  
61 - end  
62 -  
63 - def add_key id, key  
64 - true  
65 - end  
66 -  
67 - def remove_key id, key  
68 - true  
69 - end  
70 - end  
71 -end  
spec/support/test_env.rb 0 → 100644
@@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
  1 +module TestEnv
  2 + extend self
  3 +
  4 + # Test environment
  5 + #
  6 + # all repositories and namespaces stored at
  7 + # RAILS_APP/tmp/test-git-base-path
  8 + #
  9 + # Next shell methods are stubbed and return true
  10 + # - mv_repository
  11 + # - remove_repository
  12 + # - add_key
  13 + # - remove_key
  14 + #
  15 + def init
  16 + # Use tmp dir for FS manipulations
  17 + repos_path = Rails.root.join('tmp', 'test-git-base-path')
  18 + Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
  19 +
  20 + Gitlab::Shell.any_instance.stub(
  21 + add_repository: ->(path) { create_temp_repo(File.join(repos_path, "#{path}.git")) },
  22 + mv_repository: true,
  23 + remove_repository: true,
  24 + add_key: true,
  25 + remove_key: true
  26 + )
  27 +
  28 + fake_satellite = double(
  29 + exists?: true,
  30 + destroy: true,
  31 + create: true
  32 + )
  33 +
  34 + Project.any_instance.stub(
  35 + satellite: fake_satellite
  36 + )
  37 +
  38 + MergeRequest.any_instance.stub(
  39 + check_if_can_be_merged: true
  40 + )
  41 +
  42 + Repository.any_instance.stub(
  43 + size: 12.45
  44 + )
  45 +
  46 + # Remove tmp/test-git-base-path
  47 + FileUtils.rm_rf Gitlab.config.gitlab_shell.repos_path
  48 +
  49 + # Recreate tmp/test-git-base-path
  50 + FileUtils.mkdir_p Gitlab.config.gitlab_shell.repos_path
  51 +
  52 + # Symlink tmp/repositories/gitlabhq to tmp/test-git-base-path/gitlabhq
  53 + seed_repo = Rails.root.join('tmp', 'repositories', 'gitlabhq')
  54 + target_repo = File.join(repos_path, 'gitlabhq.git')
  55 + system("ln -s #{seed_repo} #{target_repo}")
  56 + end
  57 +
  58 + def create_temp_repo(path)
  59 + FileUtils.mkdir_p path
  60 + command = "git init --quiet --bare #{path};"
  61 + system(command)
  62 + end
  63 +end