require 'opal/rspec' module Opal module RSpec class RakeTask include Rake::DSL if defined? Rake::DSL RUNNER = File.expand_path('../../../../vendor/spec_runner.js', __FILE__) PORT = 9999 URL = "http://localhost:9999/" def initialize(name = 'opal:rspec', spec_app = false, &block) desc "Run opal specs in phantomjs" task name do require 'rack' require 'webrick' apps = [] apps << app = Opal::Server.new { |s| s.main = 'opal/rspec/sprockets_runner' s.append_path 'spec' s.debug = false block.call s if block_given? } apps << Object.const_get(spec_app.to_s) if spec_app server = Thread.new do Thread.current.abort_on_exception = true Rack::Server.start( :app => Rack::Cascade.new([app, spec_app]), :Port => PORT, :AccessLog => [], :Logger => WEBrick::Log.new("/dev/null"), ) end if `phantomjs -v`.strip.to_i >= 2 warn <<-WARN.gsub(/^ /,'') Only PhantomJS v1 is currently supported, if you're using homebrew on OSX you can switch version with: brew switch phantomjs 1.9.8 WARN exit 1 end begin system %Q{phantomjs #{RUNNER} "#{URL}"} success = $?.success? exit 1 unless success ensure server.kill end end end end end end