#!/usr/bin/env ruby $VERBOSE = true require 'jazrb/options' ENV["JAZRB_JS_PATH"] = JAZ_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'jazrb')) intro = File.join(JAZ_DIR, "intro.js") outro = File.join(JAZ_DIR, "outro.js") if $jazrb_qunit intro = File.join(JAZ_DIR, "qintro.js") outro = File.join(JAZ_DIR, "qoutro.js") end args = [ "envjsrb" ] if $jazrb_deps args += [ "--deps", $jazrb_deps ] end args << intro introd = true outrod = true ARGV.each do |f| if f =~ /\.x?html?$/ || f =~ %r(^https?://) if introd && !outrod args << outro args << "about:blank" outrod = true introd = false end if !introd args << intro end args << f introd = true outrod = false elsif f == "about:blank" if introd && !outrod args << outro outrod = true introd = false end args << f else args << f end end args << outro cmd = args.join(" ") puts cmd if $jazrb_verbose require 'eventmachine' require 'nokogiri' class Restart < Exception; end def colour(text, colour_code) # return text unless ENV['RSPEC_COLOR'] || (colour? & (autospec? || output_to_tty?)) "#{colour_code}#{text}\e[0m" end def green(text); colour(text, "\e[32m"); end def red(text); colour(text, "\e[31m"); end def yellow(text); colour(text, "\e[33m"); end def blue(text); colour(text, "\e[34m"); end $spec_info = {} $spec_info[:specs] = [] $spec_info[:failures] = [] $spec_info[:pending] = [] class SAX < Nokogiri::XML::SAX::Document def start_element name, attrs # p attrs attrs = Hash[*attrs] # p attrs case name when "testsuites"; when "testsuite"; @testsuite_name = attrs["name"] if @testcase_pending = ( attrs["skipped"] == "true" ) $spec_info[:specs] << (tc = {}) $spec_info[:pending] << $spec_info[:specs].last end when "testcase"; $spec_info[:specs] << (tc = {}) tc[:name] = @testsuite_name + " : " + attrs["name"] @testcase_failed = false when "failure"; $spec_info[:failures] << (tc = $spec_info[:specs].last) tc[:message] = attrs["message"] @testcase_failed = true else; raise "hell: #{name}" end end def cdata_block string if @testcase_failed $spec_info[:failures].last[:stack] ||= "" $spec_info[:failures].last[:stack] << string end end def end_element name case name when "testsuites"; raise Restart when "testsuite"; when "testcase"; if !$jazrb_xml char = nil if @testcase_failed char = red("F") elsif @testcase_pending char = yellow("*") else char = green(".") end print char $stdout.flush end when "failure"; else; raise "hell: #{name}" end end end $failed_processes = 0 module WriteToNokogiri def post_init @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new ) end def receive_data data begin if $jazrb_xml puts data $stdout.flush end @parser << data rescue Restart @parser = Nokogiri::XML::SAX::PushParser.new( SAX.new ) rescue Exception => e $stderr.puts "Parser raised #{e} on #{data}" exit 1 end end def unbind $failed_processes += 1 if get_status.exitstatus && get_status.exitstatus > 0 EM.stop end end date = Time.now failures = nil examples = nil pending = nil EM.run do EM.popen cmd, WriteToNokogiri end puts puts if !$jazrb_xml $spec_info[:failures].each_with_index do |failure,i| print i+1,")\n" print red("#{failure[:name]} FAILED\n") print red(failure[:message]),"\n" # double check, but is there really anything in the jaz stack that's useful? stack = failure[:stack] stack and ( stack = stack.split "\n" ) stack and stack.each do |line| if line =~ %r(/jasmine.js:\d+) || line =~ %r(/jasmine/src/.*\.js:\d+) break end print line, "\n" end # p stack # print failure[:stack],"\n" end puts "Finished in #{Time.now - date} seconds" puts examples = $spec_info[:specs].length failures = $spec_info[:failures].length pending = $spec_info[:pending].length if $failed_processes > 0 examples += $failed_processes failures += $failed_processes end msg = "#{examples} examples, #{failures} failures, #{pending} pending" if failures > 0 msg = red(msg) elsif pending > 0 msg = yellow(msg) else msg = green(msg) end puts msg end exit ( !failures.nil? && failures > 0 ) ? 1 : 0