mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
Prevent enhance of load:defaults after invoke
Print a warning and abort if "load:defaults" is erroneously invoked after capistrano is already loaded, e.g. when a plugin is loaded in `deploy.rb` instead of `Capfile`.
This commit is contained in:
parent
e26503f928
commit
52b0d489d7
4 changed files with 60 additions and 0 deletions
|
@ -14,6 +14,9 @@ https://github.com/capistrano/capistrano/compare/v3.4.0...HEAD
|
|||
* Allow after() to refer to tasks that have not been loaded yet (@jcoglan)
|
||||
* Ensure scm fetch_revision methods strip trailing whitespace (@mattbrictson)
|
||||
* Allow use "all" as string for server filtering (@theist)
|
||||
* Print a warning and abort if "load:defaults" is erroneously invoked after
|
||||
capistrano is already loaded, e.g. when a plugin is loaded in `deploy.rb`
|
||||
instead of `Capfile`. (@mattbrictson)
|
||||
|
||||
## `3.4.0`
|
||||
|
||||
|
|
29
lib/capistrano/immutable_task.rb
Normal file
29
lib/capistrano/immutable_task.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Capistrano
|
||||
# This module extends a Rake::Task to freeze it to prevent it from being
|
||||
# enhanced. This is used to prevent users from enhancing a task at the wrong
|
||||
# point of Capistrano's boot process, which can happen if a Capistrano plugin
|
||||
# is loaded in deploy.rb by mistake (instead of in the Capfile).
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# task = Rake.application["load:defaults"]
|
||||
# task.invoke
|
||||
# task.extend(Capistrano::ImmutableTask) # prevent further modifications
|
||||
#
|
||||
module ImmutableTask
|
||||
def self.extended(task)
|
||||
task.freeze
|
||||
end
|
||||
|
||||
def enhance(*args, &block)
|
||||
$stderr.puts <<-MESSAGE
|
||||
WARNING: #{name} has already been invoked and can no longer be modified.
|
||||
Check that you haven't loaded a Capistrano plugin in deploy.rb by mistake.
|
||||
Plugins must be loaded in the Capfile to initialize properly.
|
||||
MESSAGE
|
||||
|
||||
# This will raise a frozen object error
|
||||
super(*args, &block)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,3 +1,4 @@
|
|||
require "capistrano/immutable_task"
|
||||
include Capistrano::DSL
|
||||
|
||||
namespace :load do
|
||||
|
@ -11,6 +12,7 @@ stages.each do |stage|
|
|||
set(:stage, stage.to_sym)
|
||||
|
||||
invoke 'load:defaults'
|
||||
Rake.application["load:defaults"].extend(Capistrano::ImmutableTask)
|
||||
load deploy_config_path
|
||||
load stage_config_path.join("#{stage}.rb")
|
||||
load "capistrano/#{fetch(:scm)}.rb"
|
||||
|
|
26
spec/lib/capistrano/immutable_task_spec.rb
Normal file
26
spec/lib/capistrano/immutable_task_spec.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require 'spec_helper'
|
||||
require 'rake'
|
||||
require 'capistrano/immutable_task'
|
||||
|
||||
module Capistrano
|
||||
describe ImmutableTask do
|
||||
it 'prints warning and raises when task is enhanced' do
|
||||
extend(Rake::DSL)
|
||||
|
||||
load_defaults = Rake::Task.define_task('load:defaults')
|
||||
load_defaults.extend(Capistrano::ImmutableTask)
|
||||
|
||||
$stderr.expects(:puts).with do |message|
|
||||
message =~ /^WARNING: load:defaults has already been invoked/
|
||||
end
|
||||
|
||||
expect {
|
||||
namespace :load do
|
||||
task :defaults do
|
||||
# Never reached since load_defaults is frozen and can't be enhanced
|
||||
end
|
||||
end
|
||||
}.to raise_error(/frozen/i)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue