Skip to content

Commit

Permalink
Add test to error message
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed May 24, 2023
1 parent d40abfa commit 9bee675
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
20 changes: 10 additions & 10 deletions mrblib/rf/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ def run(argv)
pre_action
do_action
post_action
rescue SyntaxError, Files::NotFound => e
warn "Error: #{e}"
rescue StandardError => e
print_exception(e)
exit 1
rescue Files::NotFound => e
print_exception_and_exit(e, false)
rescue SyntaxError, StandardError => e
print_exception_and_exit(e)
end

def setup(argv)
Expand Down Expand Up @@ -95,18 +94,19 @@ def all_print?(val)
val == true
end

def print_exception(exc)
if debug?
def print_exception_and_exit(exc, backtrace = debug?)
if backtrace
warn "Error: #{exc.inspect}"
warn
warn 'trace (most recent call last):'
warn exc.backtrace.join("\n")
exc.backtrace.each_with_index.reverse_each do |line, index|
warn " [#{index + 1}] #{line}"
exc.backtrace.each_with_index do |line, index|
i = exc.backtrace.size - index
warn " [#{i}] #{line}"
end
else
warn "Error: #{exc}"
end
exit 1
end
end
end
48 changes: 48 additions & 0 deletions spec/error_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,52 @@
end
end
end

context 'Syntax error' do
let(:input) { "test\n" }
before { run_rf('if', input) }
let(:error_message) do
'Error: line 1: syntax error, unexpected end of file'
end

it { expect(last_command_started).not_to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stderr error_message }
end

context 'No method error (StandardError)' do
let(:input) { "test\n" }
before { run_rf('_.very_useful_method', input) }
let(:error_message) do
"Error: undefined method 'very_useful_method'"
end

it { expect(last_command_started).not_to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stderr error_message }
end

context 'File not found' do
let(:file) { 'not_found_file' }
before { run_rf("'' #{file}") }
let(:error_message) do
"Error: file not found: #{file}"
end

it { expect(last_command_started).not_to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stderr error_message }
end

context 'Enable debug mode' do
let(:input) { "test\n" }
before { run_rf('-d if', input) }
let(:error_message) do
<<~OUTPUT
Error: line 1: syntax error, unexpected end of file (SyntaxError)
trace (most recent call last):
OUTPUT
end

it { expect(last_command_started).not_to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stderr include_output_string error_message }
end
end

0 comments on commit 9bee675

Please sign in to comment.