lib/autowatchr.rb in autowatchr-0.1.4 vs lib/autowatchr.rb in autowatchr-0.1.5

- old
+ new

@@ -1,12 +1,13 @@ require 'erb' +require 'stringio' class Autowatchr class Config attr_writer :command, :ruby, :include, :require, :lib_dir, :test_dir, - :lib_re, :test_re, :failed_results_re, :completed_re, :failing_only, - :run_suite + :lib_re, :test_re, :test_file, :failed_results_re, :completed_re, + :failing_only, :run_suite def initialize(options = {}) @failing_only = @run_suite = true options.each_pair do |key, value| @@ -56,10 +57,14 @@ end def test_re @test_re ||= '^%s.*/test_.*\.rb$' % self.test_dir end + + def test_file + @test_file ||= 'test_%s.rb' + end def failed_results_re @failed_results_re ||= /^\s+\d+\) (?:Failure|Error):\n(.*?)\((.*?)\)/ end @@ -92,10 +97,11 @@ @io.write(string) end end attr_reader :config + attr_accessor :interrupt_received def initialize(script, options = {}) @config = Config.new(options) yield @config if block_given? @script = script @@ -103,16 +109,35 @@ @failed_tests = {} discover_files run_all_tests start_watching_files + start_sigint_handler end + # Traps any INT signals (like Control-C) and re-runs all + # the tests, unless the user sends the INT signal again within + # 2 seconds. + def start_sigint_handler + Signal.trap 'INT' do + if self.interrupt_received + exit 0 + else + self.interrupt_received = true + puts "\nInterrupt a second time to quit" + Kernel.sleep 2 + self.interrupt_received = false + puts "Running all tests..." + run_all_tests + end + end + end + def run_lib_file(file) md = file.match(%r{^#{@config.lib_dir}#{File::SEPARATOR}?(.+)$}) parts = md[1].split(File::SEPARATOR) - parts[-1] = "test_#{parts[-1]}" + parts[-1] = config.test_file % File.basename(parts[-1],'.rb') file = "#{@config.test_dir}/" + File.join(parts) run_test_file(file) end def run_test_file(files) @@ -212,6 +237,7 @@ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end + end