diff --git a/features/configuration.feature b/features/configuration.feature new file mode 100644 index 00000000..31dd59da --- /dev/null +++ b/features/configuration.feature @@ -0,0 +1,15 @@ +Feature: The path to the configuration can be changed, removing the need to + follow Ruby/Rails conventions + + Background: + Given a test app with the default configuration + And servers with the roles app and web + + Scenario: Deploying with configuration in default location + When I run "cap test" + Then the task is successful + + Scenario: Deploying with configuration in a custom location + But the configuration is in a custom location + When I run "cap test" + Then the task is successful diff --git a/features/step_definitions/assertions.rb b/features/step_definitions/assertions.rb index efa002cd..8b24040e 100644 --- a/features/step_definitions/assertions.rb +++ b/features/step_definitions/assertions.rb @@ -88,3 +88,7 @@ end Then(/^it will not recreate the file$/) do # end + +Then(/^the task is successful$/) do + expect(@success).to be_true +end diff --git a/features/step_definitions/cap_commands.rb b/features/step_definitions/cap_commands.rb index f5b5c14d..401e25f2 100644 --- a/features/step_definitions/cap_commands.rb +++ b/features/step_definitions/cap_commands.rb @@ -1,8 +1,12 @@ When(/^I run cap "(.*?)"$/) do |task| - TestApp.cap(task) + @success = TestApp.cap(task) end When(/^I run cap "(.*?)" as part of a release$/) do |task| TestApp.cap("deploy:new_release_path #{task}") end +When(/^I run "(.*?)"$/) do |command| + @success = TestApp.run(command) +end + diff --git a/features/step_definitions/setup.rb b/features/step_definitions/setup.rb index 4d16620a..7379deb4 100644 --- a/features/step_definitions/setup.rb +++ b/features/step_definitions/setup.rb @@ -23,3 +23,6 @@ Given(/^a custom task to generate a file$/) do TestApp.copy_task_to_test_app('spec/support/tasks/database.cap') end +Given(/^the configuration is in a custom location$/) do + TestApp.move_configuration_to_custom_location('app') +end diff --git a/lib/capistrano/dsl/paths.rb b/lib/capistrano/dsl/paths.rb index 020b2c7b..17e0a044 100644 --- a/lib/capistrano/dsl/paths.rb +++ b/lib/capistrano/dsl/paths.rb @@ -27,6 +27,14 @@ module Capistrano set(:release_path, releases_path.join(timestamp)) end + def stage_config_path + Pathname.new fetch(:stage_config_path, 'config/deploy') + end + + def deploy_config_path + Pathname.new fetch(:deploy_config_path, 'config/deploy.rb') + end + def repo_url require 'cgi' require 'uri' diff --git a/lib/capistrano/dsl/stages.rb b/lib/capistrano/dsl/stages.rb index 283421b0..3455a87a 100644 --- a/lib/capistrano/dsl/stages.rb +++ b/lib/capistrano/dsl/stages.rb @@ -3,7 +3,14 @@ module Capistrano module Stages def stages - Dir['config/deploy/*.rb'].map { |f| File.basename(f, '.rb') } + Dir[stage_definitions].map { |f| File.basename(f, '.rb') } + end + + def infer_stages_from_stage_files + end + + def stage_definitions + stage_config_path.join('*.rb') end def stage_set? diff --git a/lib/capistrano/setup.rb b/lib/capistrano/setup.rb index f08b470d..13c160f8 100644 --- a/lib/capistrano/setup.rb +++ b/lib/capistrano/setup.rb @@ -9,8 +9,8 @@ end stages.each do |stage| Rake::Task.define_task(stage) do invoke 'load:defaults' - load 'config/deploy.rb' - load "config/deploy/#{stage}.rb" + load deploy_config_path + load stage_config_path.join("#{stage}.rb") load "capistrano/#{fetch(:scm)}.rb" set(:stage, stage.to_sym) I18n.locale = fetch(:locale, :en) diff --git a/spec/integration/dsl_spec.rb b/spec/integration/dsl_spec.rb index d99a8657..ed5cadff 100644 --- a/spec/integration/dsl_spec.rb +++ b/spec/integration/dsl_spec.rb @@ -340,5 +340,53 @@ describe Capistrano::DSL do end end + describe 'setting deploy configuration path' do + subject { dsl.deploy_config_path.to_s } + + context 'where no config path is set' do + before do + dsl.delete(:deploy_config_path) + end + + it 'returns "config/deploy.rb"' do + expect(subject).to eq 'config/deploy.rb' + end + end + + context 'where a custom path is set' do + before do + dsl.set(:deploy_config_path, 'my/custom/path.rb') + end + + it 'returns the custom path' do + expect(subject).to eq 'my/custom/path.rb' + end + end + end + + describe 'setting stage configuration path' do + subject { dsl.stage_config_path.to_s } + + context 'where no config path is set' do + + before do + dsl.delete(:stage_config_path) + end + + it 'returns "config/deploy"' do + expect(subject).to eq 'config/deploy' + end + end + + context 'where a custom path is set' do + before do + dsl.set(:stage_config_path, 'my/custom/path') + end + + it 'returns the custom path' do + expect(subject).to eq 'my/custom/path' + end + end + end end end diff --git a/spec/lib/capistrano/dsl_spec.rb b/spec/lib/capistrano/dsl_spec.rb index 4157a996..9d244218 100644 --- a/spec/lib/capistrano/dsl_spec.rb +++ b/spec/lib/capistrano/dsl_spec.rb @@ -20,17 +20,6 @@ module Capistrano end end - describe '#stages' do - before do - Dir.expects(:[]).with('config/deploy/*.rb'). - returns(['config/deploy/staging.rb', 'config/deploy/production.rb']) - end - - it 'returns a list of defined stages' do - expect(dsl.stages).to eq %w{staging production} - end - end - describe '#stage_set?' do subject { dsl.stage_set? } diff --git a/spec/support/test_app.rb b/spec/support/test_app.rb index 5f18d1fd..3608426a 100644 --- a/spec/support/test_app.rb +++ b/spec/support/test_app.rb @@ -58,6 +58,14 @@ module TestApp end end + def prepend_to_capfile(config) + current_capfile = File.read(capfile) + 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 @@ -67,9 +75,14 @@ module TestApp end def cap(task) + run "bundle exec cap #{stage} #{task}" + end + + def run(command) Dir.chdir(test_app_path) do - %x[bundle exec cap #{stage} #{task}] + %x[#{command}] end + $?.success? end def stage @@ -135,4 +148,22 @@ module TestApp def copy_task_to_test_app(source) FileUtils.cp(source, task_dir) end + + def config_path + test_app_path.join('config') + end + + def move_configuration_to_custom_location(location) + prepend_to_capfile( + %{ + set :stage_config_path, "app/config/deploy" + set :deploy_config_path, "app/config/deploy.rb" + } + ) + + location = test_app_path.join(location) + FileUtils.mkdir_p(location) + FileUtils.mv(config_path, location) + end + end