2013-04-11 16:54:20 -04:00
|
|
|
require 'rspec/mocks'
|
|
|
|
|
2013-04-01 07:39:19 -04:00
|
|
|
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
|
|
|
|
#
|
2013-04-10 16:28:42 -04:00
|
|
|
def init(opts = {})
|
2013-04-11 16:54:20 -04:00
|
|
|
RSpec::Mocks::setup(self)
|
|
|
|
|
2013-04-10 16:28:42 -04:00
|
|
|
# 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
|
|
|
|
|
2013-04-11 04:50:58 -04:00
|
|
|
# Disable mailer for spinach tests
|
|
|
|
disable_mailer if opts[:mailer] == false
|
2013-04-25 10:15:33 -04:00
|
|
|
setup_stubs
|
2013-04-11 04:50:58 -04:00
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
clear_test_repo_dir if opts[:init_repos] == true
|
|
|
|
setup_test_repos(opts) if opts[:repos] == true
|
|
|
|
end
|
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
def enable_observers
|
|
|
|
ActiveRecord::Base.observers.enable(:all)
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
def disable_observers
|
|
|
|
ActiveRecord::Base.observers.disable(:all)
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
def disable_mailer
|
|
|
|
NotificationService.any_instance.stub(mailer: double.as_null_object)
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
2013-11-07 11:53:35 -05:00
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
def enable_mailer
|
|
|
|
NotificationService.any_instance.unstub(:mailer)
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_stubs()
|
|
|
|
# Use tmp dir for FS manipulations
|
|
|
|
repos_path = testing_path()
|
2014-04-09 07:35:58 -04:00
|
|
|
ProjectWiki.any_instance.stub(:init_repo) do |path|
|
2013-04-01 09:04:35 -04:00
|
|
|
create_temp_repo(File.join(repos_path, "#{path}.git"))
|
|
|
|
end
|
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
|
|
|
|
|
|
|
|
Gitlab.config.satellites.stub(path: satellite_path)
|
|
|
|
|
|
|
|
Gitlab::Git::Repository.stub(repos_path: repos_path)
|
|
|
|
|
2013-04-01 07:39:19 -04:00
|
|
|
Gitlab::Shell.any_instance.stub(
|
2013-04-01 10:27:44 -04:00
|
|
|
add_repository: true,
|
2013-04-01 07:39:19 -04:00
|
|
|
mv_repository: true,
|
|
|
|
remove_repository: true,
|
2013-06-26 12:13:29 -04:00
|
|
|
update_repository_head: true,
|
2013-04-01 07:39:19 -04:00
|
|
|
add_key: true,
|
2013-11-20 12:27:49 -05:00
|
|
|
remove_key: true,
|
|
|
|
version: '6.3.0'
|
2013-04-01 07:39:19 -04:00
|
|
|
)
|
|
|
|
|
2014-01-23 05:55:43 -05:00
|
|
|
Gitlab::Satellite::MergeAction.any_instance.stub(
|
|
|
|
merge!: true,
|
|
|
|
)
|
|
|
|
|
2013-04-01 11:35:29 -04:00
|
|
|
Gitlab::Satellite::Satellite.any_instance.stub(
|
2013-07-16 17:14:03 -04:00
|
|
|
exists?: true,
|
|
|
|
destroy: true,
|
|
|
|
create: true,
|
|
|
|
lock_files_dir: repos_path
|
2013-04-01 07:39:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
MergeRequest.any_instance.stub(
|
2013-07-16 17:14:03 -04:00
|
|
|
check_if_can_be_merged: true
|
2013-04-01 07:39:19 -04:00
|
|
|
)
|
|
|
|
Repository.any_instance.stub(
|
2013-07-16 17:14:03 -04:00
|
|
|
size: 12.45
|
2013-04-01 07:39:19 -04:00
|
|
|
)
|
2013-10-04 15:11:50 -04:00
|
|
|
|
2014-03-25 10:25:59 -04:00
|
|
|
BaseObserver.any_instance.stub(
|
2013-10-17 03:52:38 -04:00
|
|
|
current_user: double("current_user", id: 1)
|
2013-10-04 15:11:50 -04:00
|
|
|
)
|
2013-04-25 10:15:33 -04:00
|
|
|
end
|
2013-04-01 07:39:19 -04:00
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
def clear_repo_dir(namespace, name)
|
|
|
|
setup_stubs
|
2013-08-08 08:14:59 -04:00
|
|
|
# Clean any .wiki.git that may have been created
|
2013-04-25 10:15:33 -04:00
|
|
|
FileUtils.rm_rf File.join(testing_path(), "#{name}.wiki.git")
|
|
|
|
end
|
|
|
|
|
2013-11-12 09:11:46 -05:00
|
|
|
def reset_satellite_dir
|
|
|
|
setup_stubs
|
2014-02-25 06:15:18 -05:00
|
|
|
[
|
|
|
|
%W(git reset --hard --quiet),
|
2014-03-13 17:25:55 -04:00
|
|
|
%W(git clean -fx --quiet),
|
2014-02-25 06:15:18 -05:00
|
|
|
%W(git checkout --quiet origin/master)
|
|
|
|
].each do |git_cmd|
|
|
|
|
system(*git_cmd, chdir: seed_satellite_path)
|
2013-11-12 09:11:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-08 08:14:59 -04:00
|
|
|
# Create a repo and it's satellite
|
2013-04-25 10:15:33 -04:00
|
|
|
def create_repo(namespace, name)
|
|
|
|
setup_stubs
|
|
|
|
repo = repo(namespace, name)
|
|
|
|
|
|
|
|
# Symlink tmp/repositories/gitlabhq to tmp/test-git-base-path/gitlabhq
|
2014-02-25 06:14:17 -05:00
|
|
|
FileUtils.ln_sf(seed_repo_path, repo)
|
2013-04-25 10:15:33 -04:00
|
|
|
create_satellite(repo, namespace, name)
|
|
|
|
end
|
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
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
|
2014-03-22 15:40:42 -04:00
|
|
|
|
2013-06-07 16:39:32 -04:00
|
|
|
# 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
|
|
|
|
|
2013-08-08 08:14:59 -04:00
|
|
|
# Since much more is happening in satellites
|
2013-06-07 16:39:32 -04:00
|
|
|
FileUtils.mkdir_p Gitlab.config.satellites.path
|
|
|
|
end
|
|
|
|
|
2013-04-25 10:15:33 -04:00
|
|
|
# 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)
|
2013-07-15 15:10:49 -04:00
|
|
|
# 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)
|
2013-08-08 08:14:59 -04:00
|
|
|
FileUtils.mkdir_p(satellite_dir) unless File.exists?(satellite_dir)
|
2014-02-25 06:14:17 -05:00
|
|
|
FileUtils.ln_sf(seed_satellite_path, satellite_repo)
|
2013-04-01 07:39:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_temp_repo(path)
|
|
|
|
FileUtils.mkdir_p path
|
2014-02-25 06:15:18 -05:00
|
|
|
system(*%W(git init --quiet --bare -- #{path}))
|
2013-04-01 07:39:19 -04:00
|
|
|
end
|
|
|
|
end
|