Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-core
base: v2.11.0
...
head fork: rspec/rspec-core
compare: minimal_debugger
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 16, 2012
@myronmarston myronmarston Improve `debugger` so that an IRB session is started when ruby-debug …
…is not loaded.

There's more to do, but I want to push this and get feedback first.
b9b3531
Showing with 84 additions and 1 deletion.
  1. +81 −0 lib/rspec/core/debugger.rb
  2. +3 −1 lib/rspec/core/extensions/kernel.rb
View
81 lib/rspec/core/debugger.rb
@@ -0,0 +1,81 @@
+require 'irb'
+
+module RSpec
+ module Core
+ module IrbExtension
+ def new(workspace = nil, *args)
+ workspace ||= ::IRB::WorkSpace.new(Debugger.irb_binding)
+ super(workspace, *args)
+ end
+
+ ::IRB::Irb.extend self
+ end
+
+ module Debugger
+ extend self
+ attr_accessor :irb_binding
+
+ def start(binding, breakpoint)
+ explain(breakpoint)
+
+ with_binding binding do
+ ::IRB.start
+ end
+ end
+
+ private
+
+ def explain(breakpoint)
+ out.puts <<-EOS.gsub(/^\s*\|/, '')
+ |
+ |*****************************************************************
+ |A `debugger` statement has been encountered but ruby-debug is not
+ |loaded. For a full debugger, install `ruby-debug` and use the -d
+ |or --debug option.
+ |
+ |The `debugger` statement was encounted at:
+ |#{breakpoint}
+ |
+ |#{listing(breakpoint)}
+ |
+ |We've setup an IRB sessionso you can poke around here
+ |a bit. Type `exit` to continue.
+ |*****************************************************************
+ |
+ EOS
+ end
+
+ def with_binding(binding)
+ self.irb_binding = binding
+ yield
+ ensure
+ self.irb_binding = nil
+ end
+
+ NUM_LINES = 11 # 5 on either side of the breakpoint
+
+ def listing(breakpoint)
+ file, line_num, *rest = breakpoint.split(':')
+ line_num = line_num.to_i
+ start_line = [line_num - 6, 1].max
+
+ lines = File.read(file).lines.to_a[start_line, NUM_LINES]
+
+ prefixes = (start_line...(start_line + NUM_LINES)).map do |num|
+ num += 1
+ arrow = (num == line_num) ? " => " : " "
+ arrow + num.to_s.rjust(4)
+ end
+
+ prefixes.zip(lines).map do |(prefix, line)|
+ prefix + line
+ end.join("\n|")
+ end
+
+ def out
+ RSpec.configuration.error_stream || $stderr
+ end
+ end
+ end
+end
+
View
4 lib/rspec/core/extensions/kernel.rb
@@ -3,7 +3,9 @@ module Kernel
# If not already defined by ruby-debug, this implementation prints helpful
# message to STDERR when ruby-debug is not loaded.
def debugger(*args)
- (RSpec.configuration.error_stream || $stderr).puts "\n***** debugger statement ignored, use -d or --debug option to enable debugging\n#{caller(0)[1]}"
+ require 'rspec/core/debugger'
+ ::RSpec::Core::Debugger.start(binding, caller[0])
end
end
end
+

No commit comments for this range

Something went wrong with that request. Please try again.