summaryrefslogtreecommitdiff
path: root/spec/support/stubbed_repository.rb
blob: fa6c4c9a9b664f9c3c6caef9a08c8e28331ef6d1 (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
# Stubs out all Git repository access done by models so that specs can run
# against fake repositories without Grit complaining that they don't exist.
module StubbedRepository
  extend ActiveSupport::Concern

  included do
    # If a class defines the method we want to stub directly, rather than
    # inheriting it from a module (as is the case in UsersProject), that method
    # will overwrite our stub, so use alias_method to ensure it's our stub
    # getting called.

    alias_method :update_repository,     :fake_update_repository
    alias_method :destroy_repository,    :fake_destroy_repository
    alias_method :repository_delete_key, :fake_repository_delete_key
    alias_method :path_to_repo,          :fake_path_to_repo
    alias_method :satellite,             :fake_satellite
  end

  def fake_update_repository
    true
  end

  def fake_destroy_repository
    true
  end

  def fake_repository_delete_key
    true
  end

  def fake_path_to_repo
    if new_record?
      # There are a couple Project specs that expect the Project's path to be
      # in the returned path, so let's patronize them.
      File.join(Rails.root, 'tmp', 'tests', path)
    else
      # For everything else, just give it the path to one of our real seeded
      # repos.
      File.join(Rails.root, 'tmp', 'tests', 'gitlabhq_1')
    end
  end

  def fake_satellite
    FakeSatellite.new
  end

  class FakeSatellite
    def exists?
      true
    end

    def create
      true
    end
  end
end

[Project, Key, ProtectedBranch, UsersProject].each do |c|
  c.send(:include, StubbedRepository)
end