Skip to content

Commit 0ecea7b

Browse files
authored
Merge pull request #578 from jmax315/master
Fix incorrect use of Process::exit. This fixes open issue #244.
2 parents 3cf3555 + 1e1d85d commit 0ecea7b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

lib/thor/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,13 @@ def start(given_args = ARGV, config = {})
466466
dispatch(nil, given_args.dup, nil, config)
467467
rescue Thor::Error => e
468468
config[:debug] || ENV["THOR_DEBUG"] == "1" ? (raise e) : config[:shell].error(e.message)
469-
exit(1) if exit_on_failure?
469+
exit(false) if exit_on_failure?
470470
rescue Errno::EPIPE
471471
# This happens if a thor command is piped to something like `head`,
472472
# which closes the pipe when it's done reading. This will also
473473
# mean that if the pipe is closed, further unnecessary
474474
# computation will not occur.
475-
exit(0)
475+
exit(true)
476476
end
477477

478478
# Allows to use private methods from parent in child classes as commands.

spec/fixtures/exit_status.thor

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "thor"
2+
3+
class ExitStatus < Thor
4+
def self.exit_on_failure?
5+
true
6+
end
7+
8+
desc "error", "exit with a planned error"
9+
def error
10+
raise Thor::Error.new("planned error")
11+
end
12+
13+
desc "ok", "exit with no error"
14+
def ok
15+
end
16+
end
17+
18+
ExitStatus.start(ARGV)
19+

spec/script_exit_status_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe "when the Thor class's exit_with_failure? method returns true" do
2+
def thor_command(command)
3+
gem_dir= File.expand_path("#{File.dirname(__FILE__)}/..")
4+
lib_path= "#{gem_dir}/lib"
5+
script_path= "#{gem_dir}/spec/fixtures/exit_status.thor"
6+
ruby_lib= ENV['RUBYLIB'].nil? ? lib_path : "#{lib_path}:#{ENV['RUBYLIB']}"
7+
8+
full_command= "ruby #{script_path} #{command}"
9+
r,w= IO.pipe
10+
pid= spawn({'RUBYLIB' => ruby_lib},
11+
full_command,
12+
{:out => w, :err => [:child, :out]})
13+
w.close
14+
15+
junk, exit_status= Process.wait2(pid)
16+
junk= r.read
17+
r.close
18+
19+
exit_status.exitstatus
20+
end
21+
22+
it "a command that raises a Thor::Error exits with a status of 1" do
23+
expect(thor_command("error")).to eq(1)
24+
end
25+
26+
it "a command that does not raise a Thor::Error exits with a status of 0" do
27+
expect(thor_command("ok")).to eq(0)
28+
end
29+
end

0 commit comments

Comments
 (0)