1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00
capistrano/spec/support/test_app.rb

203 lines
4.1 KiB
Ruby
Raw Permalink Normal View History

2016-03-01 10:51:47 -05:00
require "English"
2016-02-28 18:16:11 -05:00
require "fileutils"
require "pathname"
2013-06-14 12:09:32 -04:00
module TestApp
extend self
def install
install_test_app_with(default_config)
end
def default_config
2016-03-01 23:13:36 -05:00
<<-CONFIG
set :deploy_to, '#{deploy_to}'
set :repo_url, 'https://github.com/capistrano/capistrano.git'
set :branch, 'master'
set :ssh_options, { keys: "\#{ENV['HOME']}/.vagrant.d/insecure_private_key", auth_methods: ['publickey'] }
server 'vagrant@localhost:2220', roles: %w{web app}
set :linked_files, #{linked_files}
set :linked_dirs, #{linked_dirs}
set :format_options, log_file: nil
set :local_user, #{current_user.inspect}
2016-03-01 23:13:36 -05:00
CONFIG
end
def linked_files
%w{config/database.yml}
end
def linked_file
shared_path.join(linked_files.first)
end
def linked_dirs
%w{bin log public/system}
end
2013-06-14 12:09:32 -04:00
def create_test_app
FileUtils.rm_rf(test_app_path)
FileUtils.mkdir(test_app_path)
2013-06-14 12:09:32 -04:00
2016-02-28 18:16:11 -05:00
File.open(gemfile, "w+") do |file|
file.write "source 'https://rubygems.org'\n"
2013-06-14 12:09:32 -04:00
file.write "gem 'capistrano', path: '#{path_to_cap}'"
end
Dir.chdir(test_app_path) do
run "bundle"
2013-06-14 12:09:32 -04:00
end
end
def install_test_app_with(config)
create_test_app
Dir.chdir(test_app_path) do
run "cap install STAGES=#{stage}"
end
write_local_deploy_file(config)
end
def write_local_deploy_file(config)
2016-02-28 18:16:11 -05:00
File.open(test_stage_path, "w") do |file|
file.write config
end
end
def write_local_stage_file(filename, config=nil)
File.open(test_app_path.join("config/deploy/#{filename}"), "w") do |file|
file.write(config) if config
end
end
def append_to_deploy_file(config)
2016-02-28 18:16:11 -05:00
File.open(test_stage_path, "a") do |file|
file.write config + "\n"
end
end
def prepend_to_capfile(config)
current_capfile = File.read(capfile)
2016-02-28 18:16:11 -05:00
File.open(capfile, "w") do |file|
file.write config
file.write current_capfile
end
end
def create_shared_directory(path)
FileUtils.mkdir_p(shared_path.join(path))
end
def create_shared_file(path)
2016-02-28 18:16:11 -05:00
File.open(shared_path.join(path), "w")
end
def cap(task, subdirectory=nil)
run "cap #{stage} #{task} --trace", subdirectory
end
def run(command, subdirectory=nil)
output = nil
command = "bundle exec #{command}" unless command =~ /^bundle\b/
dir = subdirectory ? test_app_path.join(subdirectory) : test_app_path
Dir.chdir(dir) do
output = with_clean_bundler_env { `#{command}` }
end
2016-03-01 10:36:05 -05:00
[$CHILD_STATUS.success?, output]
end
def stage
2016-02-28 18:16:11 -05:00
"test"
end
def test_stage_path
2016-02-28 18:16:11 -05:00
test_app_path.join("config/deploy/test.rb")
end
2013-06-14 12:09:32 -04:00
def test_app_path
2016-02-28 18:16:11 -05:00
Pathname.new("/tmp/test_app")
2013-06-14 12:09:32 -04:00
end
def deploy_to
2016-02-28 18:16:11 -05:00
Pathname.new("/home/vagrant/var/www/deploy")
end
def shared_path
2016-02-28 18:16:11 -05:00
deploy_to.join("shared")
end
def current_path
2016-02-28 18:16:11 -05:00
deploy_to.join("current")
end
def releases_path
2016-02-28 18:16:11 -05:00
deploy_to.join("releases")
end
def release_path(t=timestamp)
releases_path.join(t)
end
def timestamp(offset=0)
(Time.now.utc + offset).strftime("%Y%m%d%H%M%S")
end
def repo_path
2016-02-28 18:16:11 -05:00
deploy_to.join("repo")
end
2013-06-14 12:09:32 -04:00
def path_to_cap
2016-02-28 18:16:11 -05:00
File.expand_path(".")
2013-06-14 12:09:32 -04:00
end
def gemfile
2016-02-28 18:16:11 -05:00
test_app_path.join("Gemfile")
2013-06-14 12:09:32 -04:00
end
def capfile
2016-02-28 18:16:11 -05:00
test_app_path.join("Capfile")
end
def current_user
"(GitHub Web Flow) via ShipIt"
end
def task_dir
2016-02-28 18:16:11 -05:00
test_app_path.join("lib/capistrano/tasks")
end
def copy_task_to_test_app(source)
FileUtils.cp(source, task_dir)
end
def config_path
2016-02-28 18:16:11 -05:00
test_app_path.join("config")
end
def move_configuration_to_custom_location(location)
prepend_to_capfile(
2016-03-01 23:13:36 -05:00
<<-CONFIG
set :stage_config_path, "app/config/deploy"
set :deploy_config_path, "app/config/deploy.rb"
2016-03-01 23:13:36 -05:00
CONFIG
)
location = test_app_path.join(location)
FileUtils.mkdir_p(location)
FileUtils.mv(config_path, location)
end
def git_wrapper_path_glob
"/tmp/git-ssh-*.sh"
end
def with_clean_bundler_env(&block)
return yield unless defined?(Bundler)
if Bundler.respond_to?(:with_unbundled_env)
Bundler.with_unbundled_env(&block)
else
Bundler.with_clean_env(&block)
end
end
2013-06-14 12:09:32 -04:00
end