diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e9595fe58..1de731451 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2016-05-10 10:05:38 +0200 using RuboCop version 0.39.0. +# on 2016-05-14 08:08:13 +0200 using RuboCop version 0.40.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 18 +# Offense count: 17 Metrics/AbcSize: Max: 28 @@ -19,7 +19,7 @@ Metrics/ClassLength: Metrics/CyclomaticComplexity: Max: 10 -# Offense count: 59 +# Offense count: 58 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 39 diff --git a/CHANGELOG.md b/CHANGELOG.md index 930ce8662..62351f5fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Master (Unreleased) +### Fixed + +* Unfriendly output in byebug's executable when no script specified (#256). +* Unfriendly output in byebug's executable when script doesn't exist. +* Unfriendly output in byebug's executable when script has invalid code. + ## 9.0.2 - 2016-05-15 ### Fixed diff --git a/lib/byebug/runner.rb b/lib/byebug/runner.rb index ab93d86c3..92059479b 100644 --- a/lib/byebug/runner.rb +++ b/lib/byebug/runner.rb @@ -13,21 +13,6 @@ module Byebug class Runner include Helpers::ParseHelper - # - # Error class signaling absence of a script to debug. - # - class NoScript < StandardError; end - - # - # Error class signaling a non existent script to debug. - # - class NonExistentScript < StandardError; end - - # - # Error class signaling a script with invalid Ruby syntax. - # - class InvalidScript < StandardError; end - # # Special working modes that don't actually start the debugger. # @@ -63,7 +48,7 @@ def initialize(stop = true, quit = true) def help=(text) @help ||= text - interface.puts("\n#{text}\n") + interface.puts("#{text}\n") end def version=(number) @@ -74,6 +59,8 @@ def version=(number) def remote=(host_and_port) @remote ||= Byebug.parse_host_and_port(host_and_port) + + Byebug.start_client(*@remote) end def init_script @@ -97,18 +84,11 @@ def banner # Starts byebug to debug a program. # def run - prepare_options.order!($ARGV) - return if version || help - - if remote - Byebug.start_client(*remote) - return - end + option_parser.order!($ARGV) + return if non_script_option? || error_in_script? Byebug.run_init_script if init_script - setup_cmd_line_args - loop do debug_program @@ -127,36 +107,70 @@ def interface # # Processes options passed from the command line. # - def prepare_options - OptionParser.new(banner, 25) do |opts| + def option_parser + @option_parser ||= OptionParser.new(banner, 25) do |opts| opts.banner = banner OptionSetter.new(self, opts).setup end end + # + # An option that doesn't need a script specified was given + # + def non_script_option? + version || help || remote + end + + # + # There is an error with the specified script + # + def error_in_script? + no_script? || non_existing_script? || invalid_script? + end + + # + # No script to debug specified + # + def no_script? + return false unless $ARGV.empty? + + print_error('You must specify a program to debug') + true + end + # # Extracts debugged program from command line args. # - def setup_cmd_line_args + def non_existing_script? Byebug.mode = :standalone - raise(NoScript, 'You must specify a program to debug...') if $ARGV.empty? - program = which($ARGV.shift) program = which($ARGV.shift) if program == which('ruby') - raise(NonExistentScript, "The script doesn't exist") unless program - $PROGRAM_NAME = program + if program + $PROGRAM_NAME = program + false + else + print_error("The script doesn't exist") + true + end + end + + # + # Checks the debugged script has correct syntax + # + def invalid_script? + return false if syntax_valid?(File.read($PROGRAM_NAME)) + + print_error('The script has incorrect syntax') + true end # # Debugs a script only if syntax checks okay. # def debug_program - ok = syntax_valid?(File.read($PROGRAM_NAME)) - raise(InvalidScript, 'The script has incorrect syntax') unless ok - error = Byebug.debug_load($PROGRAM_NAME, stop) puts "#{error}\n#{error.backtrace}" if error end @@ -178,5 +192,13 @@ def which(cmd) nil end + + # + # Prints an error message and a help string + # + def print_error(msg) + interface.errmsg(msg) + interface.puts(option_parser.help) + end end end diff --git a/test/runner_test.rb b/test/runner_test.rb index f9af477db..504e5fe85 100644 --- a/test/runner_test.rb +++ b/test/runner_test.rb @@ -47,13 +47,17 @@ def test_run_with_remote_option_with_host_and_port_specification def test_run_without_a_script_to_debug with_command_line('bin/byebug') do - assert_raises(Runner::NoScript) { runner.run } + runner.run + + check_error_includes 'You must specify a program to debug' end end def test_run_with_an_nonexistent_script with_command_line('bin/byebug', 'non_existent_script.rb') do - assert_raises(Runner::NonExistentScript) { runner.run } + runner.run + + check_error_includes "The script doesn't exist" end end @@ -62,7 +66,9 @@ def test_run_with_an_invalid_script example_file.close with_command_line('bin/byebug', example_path) do - assert_raises(Runner::InvalidScript) { runner.run } + runner.run + + check_error_includes 'The script has incorrect syntax' end end