Permalink
Browse files

Add -i option to pry-rescue

  • Loading branch information...
1 parent 3e31a1f commit dc1605282d0c245e4c00027f97b241543ae9e081 @vincentwoo vincentwoo committed Feb 15, 2014
Showing with 39 additions and 7 deletions.
  1. +12 −5 bin/rescue
  2. +11 −2 lib/pry-rescue.rb
  3. +16 −0 lib/pry-rescue/kernel_exit_hooks.rb
View
17 bin/rescue
@@ -4,7 +4,7 @@ USAGE = %{
rescue (pry-rescue wrapper)
Usage:
- rescue [--peek] <script.rb> [arguments...]
+ rescue [-i] <script.rb> [arguments...]
What it does:
Runs <script.rb>, and if an uncaught exception is raised,
@@ -13,16 +13,23 @@ What it does:
You can then poke around to figure out why your code broke!
- If --peek is specified, then you can hit <Ctrl+C> to interrupt
- your program and open a pry session at any time you need.
+ If -i is specified, then rescue will open a REPL whether or
+ not there was an exception. Specifying -i will also wrap
+ Kernel.at_exit and run exit callbacks before launching the
+ REPL if the script succeeds. This is useful for minitest and
+ other testing frameworks.
See the README (http://bitly.com/pry-rescue) for more.
}
+
+ensure_repl = false
+
case ARGV[0]
when '-h', '--help'
puts USAGE
exit
-when '--'
+when '-i'
+ ensure_repl = true
ARGV.shift
when /\A-/
puts USAGE
@@ -39,7 +46,7 @@ if script = ARGV.shift
$0 = File.expand_path(script)
if File.exists? script
require File.expand_path('../../lib/pry-rescue.rb', __FILE__)
- PryRescue.load $0
+ PryRescue.load $0, ensure_repl
else
$stderr.puts "Error: #{script.inspect} not found."
end
View
13 lib/pry-rescue.rb
@@ -38,10 +38,12 @@
# @see {Pry::rescue}
class PryRescue
class << self
+ attr_accessor :any_exception_captured
# Start a Pry session in the context of the exception.
# @param [Exception] exception The exception raised
def enter_exception_context(exception)
+ @any_exception_captured = true
@exception_context_depth ||= 0
@exception_context_depth += 1
@@ -66,8 +68,15 @@ def enter_exception_context(exception)
# Load a script wrapped in Pry::rescue{ }
# @param [String] script The name of the script
- def load(script)
- Pry::rescue{ Kernel.load script }
+ def load(script, ensure_repl = false)
+ require File.expand_path('../pry-rescue/kernel_exit_hooks.rb', __FILE__) if ensure_repl
+ Pry::rescue do
+ begin
+ TOPLEVEL_BINDING.eval File.read(script), script, 1
+ rescue SyntaxError => exception
+ puts "#{exception}\n"
+ end
+ end
end
# Is the user currently inside pry rescue?
View
16 lib/pry-rescue/kernel_exit_hooks.rb
@@ -0,0 +1,16 @@
+Kernel.class_eval '@@exit_callbacks = []'
+
+Kernel.at_exit { Kernel.run_exit_callbacks }
+
+module Kernel
+ def at_exit(&block)
+ @@exit_callbacks.push block
+ end
+
+ def run_exit_callbacks
+ Pry::rescue do
+ @@exit_callbacks.dup.each &:call
+ end
+ TOPLEVEL_BINDING.pry unless PryRescue.any_exception_captured
+ end
+end

0 comments on commit dc16052

Please sign in to comment.