Skip to content

Commit

Permalink
Merge pull request #625 from marcandre/deprecate_exit
Browse files Browse the repository at this point in the history
Deprecate relying on default exit_on_failure?
  • Loading branch information
rafaelfranca authored Nov 15, 2019
2 parents 1a59f35 + cc8e8fd commit 37001c9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ def disable_required_check?(command) #:nodoc:
command && disable_required_check.include?(command.name.to_sym)
end

def deprecation_warning(message) #:nodoc:
unless ENV['THOR_SILENCE_DEPRECATION']
warn "Deprecation warning: #{message}\n" +
'You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.'
end
end

protected

def stop_on_unknown_option #:nodoc:
Expand Down
1 change: 1 addition & 0 deletions lib/thor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ def from_superclass(method, default = nil)

# A flag that makes the process exit with status 1 if any error happens.
def exit_on_failure?
Thor.deprecation_warning 'Thor exit with status 0 on errors. To keep this behavior, you must define `exit_on_failure?`'
false
end

Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/command.thor
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# module: random

class Amazing < Thor
def self.exit_on_failure?
false
end

desc "describe NAME", "say that someone is amazing"
method_options :forcefully => :boolean
def describe(name, opts)
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/script.thor
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class MyScript < Thor
check_unknown_options! :except => :with_optional

def self.exit_on_failure?
false
end

attr_accessor :some_attribute
attr_writer :another_attribute
attr_reader :another_attribute
Expand Down Expand Up @@ -186,6 +190,10 @@ module Scripts
class MyDefaults < Thor
check_unknown_options!

def self.exit_on_failure?
false
end

namespace :default
desc "cow", "prints 'moo'"
def cow
Expand All @@ -206,6 +214,10 @@ module Scripts
end

class Arities < Thor
def self.exit_on_failure?
false
end

desc "zero_args", "takes zero args"
def zero_args
end
Expand Down
4 changes: 4 additions & 0 deletions spec/subcommand_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def foo(name)
class Parent < Thor
desc "child1", "child1 description"
subcommand "child1", Child1

def self.exit_on_failure?
false
end
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def boring(*args)
def exec(*args)
[options, args]
end

def self.exit_on_failure?
false
end
end

it "passes remaining args to command when it encounters a non-option" do
Expand Down Expand Up @@ -223,6 +227,10 @@ def exec(*args)
def checked(*args)
[options, args]
end

def self.exit_on_failure?
false
end
end

it "still accept options and arguments" do
Expand Down Expand Up @@ -285,6 +293,10 @@ def exec(*args)
def boring(*args)
[options, args]
end

def self.exit_on_failure?
false
end
end

it "does not check the required option in the given command" do
Expand Down Expand Up @@ -732,4 +744,19 @@ def unknown(*args)
expect(MyScript.start(%w(send))).to eq(true)
end
end

context "without an exit_on_failure? method" do
my_script = Class.new(Thor) do
desc "no arg", "do nothing"
def no_arg
end
end

it "outputs a deprecation warning on error" do
expect do
my_script.start(%w[no_arg one])
end.to output(/^Deprecation.*exit_on_failure/).to_stderr
end
end

end

0 comments on commit 37001c9

Please sign in to comment.