crazy crazy changes for server running and outside browser usage and other *magic*

This commit is contained in:
John Bintz 2012-01-18 18:04:36 -05:00
parent 47b8d10d56
commit 0d4550524f
5 changed files with 130 additions and 32 deletions

View File

@ -19,7 +19,8 @@ module Jasmine
:files => [], :files => [],
:reporters => [ [ 'Console' ] ], :reporters => [ [ 'Console' ] ],
:quiet => false, :quiet => false,
:use_server => false :use_server => false,
:server_port => nil
} }
DEFAULTS_FILE = File.join(Dir.pwd, '.jasmine-headless-webkit') DEFAULTS_FILE = File.join(Dir.pwd, '.jasmine-headless-webkit')
@ -78,6 +79,8 @@ module Jasmine
add_reporter(arg) add_reporter(arg)
when '--use-server' when '--use-server'
@options[:use_server] = true @options[:use_server] = true
when '--server-port'
@options[:server_port] = arg.to_i
when '--out' when '--out'
add_reporter_file(arg) add_reporter_file(arg)
when '-h', '--help' when '-h', '--help'
@ -111,6 +114,7 @@ module Jasmine
[ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ], [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
[ '--out', GetoptLong::REQUIRED_ARGUMENT ], [ '--out', GetoptLong::REQUIRED_ARGUMENT ],
[ '--use-server', GetoptLong::NO_ARGUMENT ], [ '--use-server', GetoptLong::NO_ARGUMENT ],
[ '--server-port', GetoptLong::REQUIRED_ARGUMENT ],
[ '-h', '--help', GetoptLong::NO_ARGUMENT ], [ '-h', '--help', GetoptLong::NO_ARGUMENT ],
[ '-q', '--quiet', GetoptLong::NO_ARGUMENT ] [ '-q', '--quiet', GetoptLong::NO_ARGUMENT ]
) )

View File

@ -9,6 +9,24 @@ require 'sprockets'
module Jasmine module Jasmine
module Headless 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 class Runner
JASMINE_DEFAULTS = { JASMINE_DEFAULTS = {
'spec_files' => [ '**/*[sS]pec.js' ], 'spec_files' => [ '**/*[sS]pec.js' ],
@ -32,6 +50,8 @@ module Jasmine
def self.server_port def self.server_port
return @server_port if @server_port return @server_port if @server_port
require 'socket'
count = 100 count = 100
begin begin
port = select_server_port port = select_server_port
@ -56,6 +76,10 @@ module Jasmine
@server_port @server_port
end end
def self.server_port=(port)
@server_port = port
end
def self.select_server_port def self.select_server_port
21000 + rand(10000) 21000 + rand(10000)
end end
@ -68,7 +92,11 @@ module Jasmine
"http://#{server_interface}:#{server_port}" "http://#{server_interface}:#{server_port}"
end end
def self.ensure_server def self.server_spec_path
self.server_uri + '/__JHW__/'
end
def self.ensure_server(options)
return if @server return if @server
require 'webrick' require 'webrick'
@ -81,8 +109,20 @@ module Jasmine
@server = Thread.new do @server = Thread.new do
Jasmine::Headless.warn "Powering up!" 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::Handler::WEBrick.run(
Rack::File.new('/'), app,
:Port => port, :Port => port,
:Logger => Logger.new(StringIO.new), :Logger => Logger.new(StringIO.new),
:AccessLog => [ :AccessLog => [
@ -134,16 +174,7 @@ module Jasmine
command << "-r #{file}" command << "-r #{file}"
end end
command += targets.flatten.collect do |target| command += targets
target = File.expand_path(target)
if options[:use_server]
target = self.class.server_uri + target
else
target = "file://" + target
end
target
end
command.compact.join(' ') command.compact.join(' ')
end end
@ -152,9 +183,11 @@ module Jasmine
Jasmine::Headless.show_warnings = !@options[:quiet] Jasmine::Headless.show_warnings = !@options[:quiet]
FilesList.reset! FilesList.reset!
self.class.server_port = options[:server_port]
@_targets = template_writer.write @_targets = template_writer.write
run_targets = @_targets.dup run_targets = absolute_run_targets(@_targets.dup)
if run_targets.length == 2 if run_targets.length == 2
if (!@options[:full_run] && files_list.filtered?) || files_list.has_spec_outside_scope? 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) } runner = lambda { system jasmine_command(run_targets) }
if options[:use_server] if options[:use_server]
wrap_in_server(&runner) wrap_in_server(run_targets, &runner)
else else
runner.call runner.call
end end
@ -177,6 +210,17 @@ module Jasmine
end end
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 def runner_filename
options[:runner_output_filename] || begin options[:runner_output_filename] || begin
if (runner_output = jasmine_config['runner_output']) && !runner_output.empty? if (runner_output = jasmine_config['runner_output']) && !runner_output.empty?
@ -196,10 +240,11 @@ module Jasmine
) )
end end
def wrap_in_server def wrap_in_server(run_targets)
self.class.ensure_server 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 yield
end end

View File

@ -24,7 +24,9 @@ module Jasmine::Headless
output.unshift([filtered_tests_filename, files_list.filtered_files_to_html ]) if files_list.filtered? output.unshift([filtered_tests_filename, files_list.filtered_files_to_html ]) if files_list.filtered?
output.each do |name, files| 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 end
output.collect(&:first) output.collect(&:first)

View File

@ -34,6 +34,35 @@
break; 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(); jasmine.getEnv().execute();

View File

@ -70,7 +70,7 @@ describe Jasmine::Headless::Runner do
end end
def self.it_should_have_basics def self.it_should_have_basics
it { should include('file://' + File.expand_path(target)) } it { should include(target) }
end end
context 'colors' do context 'colors' do
@ -100,18 +100,6 @@ describe Jasmine::Headless::Runner do
it_should_have_basics it_should_have_basics
it { should include("-q") } it { should include("-q") }
end 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 end
describe '#runner_filename' do describe '#runner_filename' do
@ -226,4 +214,34 @@ describe Jasmine::Headless::Runner do
end end
end 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 end