stubbed_repository.rb 1.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# 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.

12 13
    alias_method :path_to_repo, :fake_path_to_repo
    alias_method :satellite,    :fake_satellite
14 15 16
  end

  def fake_path_to_repo
17 18 19
    if new_record? || path == 'newproject'
      # There are a couple Project specs and features that expect the Project's
      # path to be in the returned path, so let's patronize them.
20 21 22 23
      File.join(Rails.root, 'tmp', 'tests', path)
    else
      # For everything else, just give it the path to one of our real seeded
      # repos.
24
      File.join(Rails.root, 'tmp', 'tests', 'gitlabhq_0')
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    end
  end

  def fake_satellite
    FakeSatellite.new
  end

  class FakeSatellite
    def exists?
      true
    end

    def create
      true
    end
  end
end

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