Permalink
Browse files

Trap SIGINFO or SIGTSTP and raise a custom exception to skip specs at

runtime
  • Loading branch information...
1 parent c765c3b commit 5f91a507870e570daea2df772f4133d307c66614 @matthewrobertson committed with Nov 30, 2012
Showing with 31 additions and 1 deletion.
  1. +14 −0 lib/rspec/core/configuration.rb
  2. +17 −1 lib/rspec/core/example.rb
View
14 lib/rspec/core/configuration.rb
@@ -900,6 +900,20 @@ def order_groups_and_examples(&block)
order_examples(&block)
end
+ # @private
+ def skip_example_trap
+ if @skip_example_trap.nil?
+ if(Signal.list['INFO'])
+ @skip_example_trap = { :signal => 'INFO', :message => 'Skipped at runtime via Ctrl+T'}
+ elsif(Signal.list['TSTP'])
+ @skip_example_trap = { :signal => 'TSTP', :message => 'Skipped at runtime via Ctrl+Z'}
+ else
+ @skip_example_trap = false
+ end
+ end
+ @skip_example_trap
+ end
+
private
def get_files_to_run(paths)
View
18 lib/rspec/core/example.rb
@@ -112,7 +112,7 @@ def run(example_group_instance, reporter)
begin
run_before_each
@example_group_instance.instance_eval(&@example_block)
- rescue Pending::PendingDeclaredInExample => e
+ rescue ExampleInterrupted, Pending::PendingDeclaredInExample => e
@pending_declared_in_example = e.message
rescue Exception => e
set_exception(e)
@@ -296,11 +296,13 @@ def record_finished(status, results={})
end
def run_before_each
+ set_interrupt_trap
@example_group_instance.setup_mocks_for_rspec
@example_group_class.run_before_each_hooks(self)
end
def run_after_each
+ unset_interrupt_trap
@example_group_class.run_after_each_hooks(self)
verify_mocks
rescue Exception => e
@@ -309,6 +311,18 @@ def run_after_each
@example_group_instance.teardown_mocks_for_rspec
end
+ def set_interrupt_trap
+ if trap_type = RSpec.configuration.skip_example_trap
+ trap(trap_type[:signal]) { raise ExampleInterrupted.new(trap_type[:message]) }
+ end
+ end
+
+ def unset_interrupt_trap
+ if trap_type = RSpec.configuration.skip_example_trap
+ trap(trap_type[:signal], 'DEFAULT')
+ end
+ end
+
def verify_mocks
@example_group_instance.verify_mocks_for_rspec
rescue Exception => e
@@ -327,5 +341,7 @@ def record(results={})
execution_result.update(results)
end
end
+
+ class ExampleInterrupted < StandardError ; end
end
end

0 comments on commit 5f91a50

Please sign in to comment.