From 0d4550524fa1822eb814f4a2f7f5d5df4f175b94 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 18 Jan 2012 18:04:36 -0500 Subject: [PATCH] crazy crazy changes for server running and outside browser usage and other *magic* --- lib/jasmine/headless/options.rb | 6 +- lib/jasmine/headless/runner.rb | 79 +++++++++++++++++++----- lib/jasmine/headless/template_writer.rb | 4 +- skel/template.html.erb | 29 +++++++++ spec/lib/jasmine/headless/runner_spec.rb | 44 +++++++++---- 5 files changed, 130 insertions(+), 32 deletions(-) diff --git a/lib/jasmine/headless/options.rb b/lib/jasmine/headless/options.rb index 8bc7c1e..53719c0 100644 --- a/lib/jasmine/headless/options.rb +++ b/lib/jasmine/headless/options.rb @@ -19,7 +19,8 @@ module Jasmine :files => [], :reporters => [ [ 'Console' ] ], :quiet => false, - :use_server => false + :use_server => false, + :server_port => nil } DEFAULTS_FILE = File.join(Dir.pwd, '.jasmine-headless-webkit') @@ -78,6 +79,8 @@ module Jasmine add_reporter(arg) when '--use-server' @options[:use_server] = true + when '--server-port' + @options[:server_port] = arg.to_i when '--out' add_reporter_file(arg) when '-h', '--help' @@ -111,6 +114,7 @@ module Jasmine [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ], [ '--out', GetoptLong::REQUIRED_ARGUMENT ], [ '--use-server', GetoptLong::NO_ARGUMENT ], + [ '--server-port', GetoptLong::REQUIRED_ARGUMENT ], [ '-h', '--help', GetoptLong::NO_ARGUMENT ], [ '-q', '--quiet', GetoptLong::NO_ARGUMENT ] ) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index 964aef6..3ce7247 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -9,6 +9,24 @@ require 'sprockets' module Jasmine module Headless + class IndexHandler + class << self + attr_accessor :index + end + + def initialize(app) + @app = app + end + + def call(env) + if env['PATH_INFO'] == '/' + return [ 302, { 'Location' => self.class.index }, [ 'Redirecting...' ] ] + end + + @app.call(env) + end + end + class Runner JASMINE_DEFAULTS = { 'spec_files' => [ '**/*[sS]pec.js' ], @@ -32,6 +50,8 @@ module Jasmine def self.server_port return @server_port if @server_port + require 'socket' + count = 100 begin port = select_server_port @@ -56,6 +76,10 @@ module Jasmine @server_port end + def self.server_port=(port) + @server_port = port + end + def self.select_server_port 21000 + rand(10000) end @@ -68,7 +92,11 @@ module Jasmine "http://#{server_interface}:#{server_port}" end - def self.ensure_server + def self.server_spec_path + self.server_uri + '/__JHW__/' + end + + def self.ensure_server(options) return if @server require 'webrick' @@ -81,8 +109,20 @@ module Jasmine @server = Thread.new do Jasmine::Headless.warn "Powering up!" + app = Rack::Builder.new do + use IndexHandler + + map '/__JHW__' do + run Rack::File.new(Dir.pwd) + end + + map '/' do + run Rack::File.new('/') + end + end + Rack::Handler::WEBrick.run( - Rack::File.new('/'), + app, :Port => port, :Logger => Logger.new(StringIO.new), :AccessLog => [ @@ -134,16 +174,7 @@ module Jasmine command << "-r #{file}" end - command += targets.flatten.collect do |target| - target = File.expand_path(target) - if options[:use_server] - target = self.class.server_uri + target - else - target = "file://" + target - end - target - end - + command += targets command.compact.join(' ') end @@ -152,9 +183,11 @@ module Jasmine Jasmine::Headless.show_warnings = !@options[:quiet] FilesList.reset! + self.class.server_port = options[:server_port] + @_targets = template_writer.write - run_targets = @_targets.dup + run_targets = absolute_run_targets(@_targets.dup) if run_targets.length == 2 if (!@options[:full_run] && files_list.filtered?) || files_list.has_spec_outside_scope? @@ -165,7 +198,7 @@ module Jasmine runner = lambda { system jasmine_command(run_targets) } if options[:use_server] - wrap_in_server(&runner) + wrap_in_server(run_targets, &runner) else runner.call end @@ -177,6 +210,17 @@ module Jasmine end end + def absolute_run_targets(targets) + targets.flatten.collect do |target| + if options[:use_server] + target = self.class.server_spec_path + target + else + target = "file://" + File.expand_path(target) + end + target + end + end + def runner_filename options[:runner_output_filename] || begin if (runner_output = jasmine_config['runner_output']) && !runner_output.empty? @@ -196,10 +240,11 @@ module Jasmine ) end - def wrap_in_server - self.class.ensure_server + def wrap_in_server(run_targets) + self.class.ensure_server(options) + IndexHandler.index = run_targets.last - Jasmine::Headless.warn "HTTP powered specs! Located at #{self.class.server_uri}" + Jasmine::Headless.warn "HTTP powered specs! Located at #{run_targets.join(' ')}" yield end diff --git a/lib/jasmine/headless/template_writer.rb b/lib/jasmine/headless/template_writer.rb index cbc5ac4..81562a5 100644 --- a/lib/jasmine/headless/template_writer.rb +++ b/lib/jasmine/headless/template_writer.rb @@ -24,7 +24,9 @@ module Jasmine::Headless output.unshift([filtered_tests_filename, files_list.filtered_files_to_html ]) if files_list.filtered? output.each do |name, files| - File.open(name, 'w') { |fh| fh.print template_for(files) } + template = template_for(files) + + File.open(name, 'wb') { |fh| fh.print template } end output.collect(&:first) diff --git a/skel/template.html.erb b/skel/template.html.erb index c02247b..e3ba9ff 100644 --- a/skel/template.html.erb +++ b/skel/template.html.erb @@ -34,6 +34,35 @@ break; } } + + var getLastModified = function(callback) { + var http = new XMLHttpRequest(); + var header; + + http.open('HEAD', window.location.href, false); + + http.onreadystatechange = function() { + callback(http.getResponseHeader('Last-Modified')); + }; + http.send(); + } + + getLastModified(function(currentLastModified) { + var checker; + checker = function() { + setTimeout(function() { + getLastModified(function(newLastModified) { + if (currentLastModified != newLastModified) { + setTimeout(function() { window.location.reload(); }, 1000); + } else { + checker(); + } + }); + }, 3000); + }; + + checker(); + }); } jasmine.getEnv().execute(); diff --git a/spec/lib/jasmine/headless/runner_spec.rb b/spec/lib/jasmine/headless/runner_spec.rb index f42c4d7..b042d95 100644 --- a/spec/lib/jasmine/headless/runner_spec.rb +++ b/spec/lib/jasmine/headless/runner_spec.rb @@ -70,7 +70,7 @@ describe Jasmine::Headless::Runner do end def self.it_should_have_basics - it { should include('file://' + File.expand_path(target)) } + it { should include(target) } end context 'colors' do @@ -100,18 +100,6 @@ describe Jasmine::Headless::Runner do it_should_have_basics it { should include("-q") } end - - context 'server' do - let(:server_uri) { 'server uri' } - - before do - options[:use_server] = true - - described_class.stubs(:server_uri).returns(server_uri) - end - - it { should include(server_uri + File.expand_path(target)) } - end end describe '#runner_filename' do @@ -226,4 +214,34 @@ describe Jasmine::Headless::Runner do end end end + + describe '#absolute_run_targets' do + let(:opts) { {} } + + subject { runner.absolute_run_targets(targets) } + + let(:targets) { [ target ] } + let(:target) { 'target' } + + before do + runner.stubs(:options).returns(:use_server => use_server) + end + + context 'server' do + let(:use_server) { true } + let(:server_spec_path) { 'server spec path' } + + before do + described_class.stubs(:server_spec_path).returns(server_spec_path) + end + + it { should == [ server_spec_path + target ] } + end + + context 'no server' do + let(:use_server) { false } + + it { should == [ 'file://' + File.expand_path(target) ] } + end + end end