spec/example_spec.rb in asir-1.2.1 vs spec/example_spec.rb in asir-1.2.2

- old
+ new

@@ -1,23 +1,33 @@ require File.expand_path('../spec_helper', __FILE__) $:.unshift File.expand_path('../../example', __FILE__) +require 'timeout' + +class ASIR::Error::TestTimeout < ASIR::Error::Fatal; end + describe "ASIR Example" do attr_accessor :file, :expects before(:each) do @expects = [ ] + @verbose = (ENV['SPEC_VERBOSE'] || 0).to_i end - after(:each) do - @file.should_not == nil + def file! file + @file = file File.open(@file) do | fh | until fh.eof? line = fh.readline line.chomp! case + when line.sub!(/^\s*#\s*PENDING:\s*/, '') + line.strip! + @pending_reason = line + @pending = ! ! (eval(line) rescue nil) + # $stderr.puts " PENDING #{@pending_reason.inspect} => #{@pending.inspect}" if ENV['SPEC_VERBOSE'] when line.sub!(/^\s*#\s*EXPECT\/:\s*/, '') expect Regexp.new(line) when line.sub!(/^\s*#\s*EXPECT!\/:\s*/, '') expect Regexp.new(line), :'!~' when line.sub!(/^\s*#\s*EXPECT:\s*/, '') @@ -25,15 +35,27 @@ when line.sub!(/^\s*#\s*EXPECT!:\s*/, '') expect Regexp.new(Regexp.escape(line)), :'!~' end end end + @output, @exit_code = run_file!(@file) + + if @pending + pending @pending_reason do + check_result! + end + else + check_result! + end + end + + def check_result! @exit_code.should == 0 @expects.empty?.should_not == true @expects.each do | rx, mode | - $stderr.puts " Checking #{mode} #{rx.inspect}" if ENV['SPEC_VERBOSE'] + $stderr.puts " Checking #{mode} #{rx.inspect}" if @verbose >= 2 case mode when :'=~' @output.should =~ rx when :'!~' @output.should_not =~ rx @@ -41,35 +63,40 @@ raise ArgumentError end end end - def run_file! file, output = StringIO.new('') + def run_file! file + output = nil + output_file = "#{file}.out" progname_save, stdout_save, stderr_save = $0, $stdout, $stderr + ruby = ASIR.ruby_path exc = system_exit = nil; exit_code = 0 begin - if true - cmd = "ASIR_EXAMPLE_SILENT=1 ruby -I example -I lib #{file}" - $stderr.puts "\n Running #{cmd}:" if ENV['SPEC_VERBOSE'] - output = `#{cmd} 2>&1 | tee #{file}.out` - else - $stderr.puts "\n Loading #{file}:" if ENV['SPEC_VERBOSE'] - $stdout.puts "*** #{$$}: client process"; $stdout.flush - $stdout = $stderr = output - $0 = file - Kernel.load(file, true) - output = output.string if StringIO === output + Timeout.timeout(20, ASIR::Error::TestTimeout) do + cmd = "ASIR_EXAMPLE_SILENT=1 #{ruby.inspect} -I example -I lib #{file}" + if @verbose >= 1 + $stderr.puts "\n Running #{cmd}:" + $stderr.puts "\n -- #{@title}" if @title + end + File.unlink(output_file) rescue nil + system("#{cmd} >#{output_file} 2>&1") end + rescue ASIR::Error::TestTimeout + $stderr.puts " Warning: Timeout in #{@file}" + exit_code = 0 # OK if checks pass rescue ::SystemExit => system_exit exit_code = 1 # ??? rescue ::Exception => exc exit_code = -1 end + output = File.read(output_file) [ output, exit_code ] ensure $0, $stdout, $stderr = progname_save, stdout_save, stderr_save - $stderr.write output if ENV['SPEC_VERBOSE'] + output ||= File.read(output_file) + $stderr.write output if @verbose >= 1 if exc stderr_save.puts "ERROR: #{file}: #{exc.inspect}\n#{exc.backtrace * "\n"}" raise exc end end @@ -80,9 +107,10 @@ Dir['example/**/ex[0-9]*.rb'].sort.each do | file | title = File.open(file) { | fh | fh.read(4096) } title = title =~ /#\s+!SLIDE[^\n]*\n\s*#\s*([^\n]+)/ && $1 it "#{file} - #{title}" do - @file = file + @title = title + file! file end end end