Handle and format Uh::WM::RuntimeError in CLI.run

This commit is contained in:
Thibault Jouan 2015-04-12 23:44:42 +00:00
parent 34d84ad111
commit c3e1f9639a
2 changed files with 39 additions and 1 deletions

View File

@ -6,6 +6,7 @@ module Uh
USAGE = "Usage: #{File.basename $0} [options]".freeze USAGE = "Usage: #{File.basename $0} [options]".freeze
EX_USAGE = 64 EX_USAGE = 64
EX_SOFTWARE = 70
class << self class << self
def run arguments, stdout: $stdout, stderr: $stderr def run arguments, stdout: $stdout, stderr: $stderr
@ -15,6 +16,10 @@ module Uh
rescue ArgumentError => e rescue ArgumentError => e
stderr.puts e stderr.puts e
exit EX_USAGE exit EX_USAGE
rescue RuntimeError => e
stderr.puts "#{e.class.name}: #{e.message}"
stderr.puts e.backtrace.map { |e| ' %s' % e } if cli.env.debug?
exit EX_SOFTWARE
end end
end end

View File

@ -52,6 +52,39 @@ module Uh
end end
end end
end end
context 'when the new CLI raises a runtime error' do
before do
allow(cli).to receive(:run) { fail RuntimeError, 'some error' }
allow(described_class).to receive(:new) { cli }
end
it 'exits with a return status of 70' do
expect { run }.to raise_error(SystemExit) do |e|
expect(e.status).to eq 70
end
end
it 'formats the error' do
trap_exit { run }
expect(stderr.string)
.to match /\AUh::WM::RuntimeError: some error\n/
end
it 'does not output a backtrace' do
trap_exit { run }
expect(stderr.string).not_to include __FILE__
end
context 'when debug mode is enabled' do
let(:arguments) { %w[-d] }
it 'outputs a backtrace' do
trap_exit { run }
expect(stderr.string).to include __FILE__
end
end
end
end end
describe '#initialize' do describe '#initialize' do