Skip to content

Commit 37001c9

Browse files
authored
Merge pull request #625 from marcandre/deprecate_exit
Deprecate relying on default exit_on_failure?
2 parents 1a59f35 + cc8e8fd commit 37001c9

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed

lib/thor.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ def disable_required_check?(command) #:nodoc:
344344
command && disable_required_check.include?(command.name.to_sym)
345345
end
346346

347+
def deprecation_warning(message) #:nodoc:
348+
unless ENV['THOR_SILENCE_DEPRECATION']
349+
warn "Deprecation warning: #{message}\n" +
350+
'You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.'
351+
end
352+
end
353+
347354
protected
348355

349356
def stop_on_unknown_option #:nodoc:

lib/thor/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ def from_superclass(method, default = nil)
645645

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

spec/fixtures/command.thor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# module: random
22

33
class Amazing < Thor
4+
def self.exit_on_failure?
5+
false
6+
end
7+
48
desc "describe NAME", "say that someone is amazing"
59
method_options :forcefully => :boolean
610
def describe(name, opts)

spec/fixtures/script.thor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class MyScript < Thor
22
check_unknown_options! :except => :with_optional
33

4+
def self.exit_on_failure?
5+
false
6+
end
7+
48
attr_accessor :some_attribute
59
attr_writer :another_attribute
610
attr_reader :another_attribute
@@ -186,6 +190,10 @@ module Scripts
186190
class MyDefaults < Thor
187191
check_unknown_options!
188192

193+
def self.exit_on_failure?
194+
false
195+
end
196+
189197
namespace :default
190198
desc "cow", "prints 'moo'"
191199
def cow
@@ -206,6 +214,10 @@ module Scripts
206214
end
207215

208216
class Arities < Thor
217+
def self.exit_on_failure?
218+
false
219+
end
220+
209221
desc "zero_args", "takes zero args"
210222
def zero_args
211223
end

spec/subcommand_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def foo(name)
5555
class Parent < Thor
5656
desc "child1", "child1 description"
5757
subcommand "child1", Child1
58+
59+
def self.exit_on_failure?
60+
false
61+
end
5862
end
5963
end
6064

spec/thor_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def boring(*args)
173173
def exec(*args)
174174
[options, args]
175175
end
176+
177+
def self.exit_on_failure?
178+
false
179+
end
176180
end
177181

178182
it "passes remaining args to command when it encounters a non-option" do
@@ -223,6 +227,10 @@ def exec(*args)
223227
def checked(*args)
224228
[options, args]
225229
end
230+
231+
def self.exit_on_failure?
232+
false
233+
end
226234
end
227235

228236
it "still accept options and arguments" do
@@ -285,6 +293,10 @@ def exec(*args)
285293
def boring(*args)
286294
[options, args]
287295
end
296+
297+
def self.exit_on_failure?
298+
false
299+
end
288300
end
289301

290302
it "does not check the required option in the given command" do
@@ -732,4 +744,19 @@ def unknown(*args)
732744
expect(MyScript.start(%w(send))).to eq(true)
733745
end
734746
end
747+
748+
context "without an exit_on_failure? method" do
749+
my_script = Class.new(Thor) do
750+
desc "no arg", "do nothing"
751+
def no_arg
752+
end
753+
end
754+
755+
it "outputs a deprecation warning on error" do
756+
expect do
757+
my_script.start(%w[no_arg one])
758+
end.to output(/^Deprecation.*exit_on_failure/).to_stderr
759+
end
760+
end
761+
735762
end

0 commit comments

Comments
 (0)