From 5d43eb21937ed972eaefc1469f255cc85b73bfe9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Jan 2012 15:02:32 -0500 Subject: [PATCH 1/8] let's run with a server, like all the cool kids do nowadays\! --- ext/jasmine-webkit-specrunner/Runner.cpp | 2 +- features/bin/with_server.feature | 7 +++ lib/jasmine/headless/options.rb | 9 +++- lib/jasmine/headless/runner.rb | 62 ++++++++++++++++++++++-- spec/lib/jasmine/headless/runner_spec.rb | 12 +++++ 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 features/bin/with_server.feature diff --git a/ext/jasmine-webkit-specrunner/Runner.cpp b/ext/jasmine-webkit-specrunner/Runner.cpp index 39941d8..cc20f49 100644 --- a/ext/jasmine-webkit-specrunner/Runner.cpp +++ b/ext/jasmine-webkit-specrunner/Runner.cpp @@ -71,7 +71,7 @@ void Runner::loadSpec() outputFiles.enqueue(outputFile); } - page.mainFrame()->load(QUrl::fromLocalFile(runnerFiles.dequeue())); + page.mainFrame()->load(runnerFiles.dequeue()); ticker.start(); } diff --git a/features/bin/with_server.feature b/features/bin/with_server.feature new file mode 100644 index 0000000..6e9fa03 --- /dev/null +++ b/features/bin/with_server.feature @@ -0,0 +1,7 @@ +Feature: Bin - With Server + Scenario: Run using an HTTP server + Given there is no existing "spec/report.txt" file + When I run `bin/jasmine-headless-webkit --use-server -j spec/jasmine/success/success.yml -f File:spec/report.txt` + Then the exit status should be 0 + And the report file "spec/report.txt" should have 1 total, 0 failures, no console usage + diff --git a/lib/jasmine/headless/options.rb b/lib/jasmine/headless/options.rb index 49e8202..8bc7c1e 100644 --- a/lib/jasmine/headless/options.rb +++ b/lib/jasmine/headless/options.rb @@ -18,7 +18,8 @@ module Jasmine :enable_cache => true, :files => [], :reporters => [ [ 'Console' ] ], - :quiet => false + :quiet => false, + :use_server => false } DEFAULTS_FILE = File.join(Dir.pwd, '.jasmine-headless-webkit') @@ -75,6 +76,8 @@ module Jasmine @options[:seed] = arg.to_i when '--format', '-f' add_reporter(arg) + when '--use-server' + @options[:use_server] = true when '--out' add_reporter_file(arg) when '-h', '--help' @@ -107,6 +110,7 @@ module Jasmine [ '--seed', GetoptLong::REQUIRED_ARGUMENT ], [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ], [ '--out', GetoptLong::REQUIRED_ARGUMENT ], + [ '--use-server', GetoptLong::NO_ARGUMENT ], [ '-h', '--help', GetoptLong::NO_ARGUMENT ], [ '-q', '--quiet', GetoptLong::NO_ARGUMENT ] ) @@ -165,8 +169,9 @@ module Jasmine [ '--runner-out ', 'Write runner to specified filename' ], [ '-j, --jasmine-config ', 'Jasmine Yaml config to use' ], [ '--no-full-run', 'Do not perform a full spec run after a successful targeted spec run' ], + [ '--use-server', 'Load tests from an HTTP server instead of from filesystem' ], [ '-l, --list', 'List files in the order they will be required' ], - [ '--seed', 'Random order seed for spec file ordering' ], + [ '--seed ', 'Random order seed for spec file ordering' ], [ '-f, --format >', 'Specify an output reporter and possibly output filename' ], [ '--out ', 'Specify output filename for last defined reporter' ], [ '-q, --quiet', "Silence most non-test related warnings" ], diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index fdb4b5f..558c907 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -7,7 +7,6 @@ require 'yaml' require 'erb' require 'sprockets' - module Jasmine module Headless class Runner @@ -63,7 +62,11 @@ module Jasmine command << "-r #{file}" end - command += targets.flatten.collect { |target| File.expand_path(target) } + command += targets.flatten.collect do |target| + target = File.expand_path(target) + target = server_uri + target if options[:use_server] + target + end command.compact.join(' ') end @@ -83,7 +86,13 @@ module Jasmine end end - system jasmine_command(run_targets) + runner = lambda { system jasmine_command(run_targets) } + + if options[:use_server] + wrap_in_server(&runner) + else + runner.call + end @_status = $?.exitstatus ensure @@ -111,7 +120,54 @@ module Jasmine ) end + def wrap_in_server + require 'rack' + require 'webrick' + require 'thread' + require 'net/http' + + server = Thread.new do + responder = lambda do |env| + file = Pathname(env['PATH_INFO']) + + if file.file? + [ 200, { 'Content-Type' => 'text/html' }, [ file.read ] ] + else + [ 404, {}, [ 'Not found' ] ] + end + end + + Rack::Handler::WEBrick.run(responder, :Port => server_port, :Logger => Logger.new(StringIO.new), :AccessLog => [ nil, nil ]) + end + + while true do + begin + Net::HTTP.get(URI(server_uri)) + break + rescue Errno::ECONNREFUSED => e + end + + sleep 0.1 + end + + yield + + Thread.kill(server) + end + private + def server_port + @server_port ||= 21000 + rand(1000) + end + + def server_interface + '127.0.0.1' + end + + def server_uri + "http://#{server_interface}:#{server_port}" + end + def jasmine_config_data raise JasmineConfigNotFound.new("Jasmine config not found. I tried #{@options[:jasmine_config]}.") if !File.file?(@options[:jasmine_config]) diff --git a/spec/lib/jasmine/headless/runner_spec.rb b/spec/lib/jasmine/headless/runner_spec.rb index dcd4650..46b4e94 100644 --- a/spec/lib/jasmine/headless/runner_spec.rb +++ b/spec/lib/jasmine/headless/runner_spec.rb @@ -100,6 +100,18 @@ 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 + + runner.stubs(:server_uri).returns(server_uri) + end + + it { should include(server_uri + File.expand_path(target)) } + end end describe '#runner_filename' do -- 2.45.2 From 09842c0539a64acee9bebae64e59e431bdaeac25 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Jan 2012 16:47:36 -0500 Subject: [PATCH 2/8] fix a few verbose reporter bugs --- lib/jasmine/headless/runner.rb | 2 ++ vendor/assets/coffeescripts/prolog.coffee | 19 +++++++++++-------- .../jasmine.HeadlessReporter.Verbose.coffee | 13 ++++++++++--- vendor/assets/javascripts/prolog.js | 18 ++++++++++-------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index 558c907..4f3245e 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -150,6 +150,8 @@ module Jasmine sleep 0.1 end + Jasmine::Headless.warn "HTTP powered specs!" + yield Thread.kill(server) diff --git a/vendor/assets/coffeescripts/prolog.coffee b/vendor/assets/coffeescripts/prolog.coffee index 078be7c..a1fcd2e 100644 --- a/vendor/assets/coffeescripts/prolog.coffee +++ b/vendor/assets/coffeescripts/prolog.coffee @@ -80,15 +80,18 @@ if window.JHW puts msg JHW.createCoffeeScriptFileException = (e) -> - if e and e.sourceURL and window.CoffeeScriptToFilename + if e and e.sourceURL filename = e.sourceURL.split('/').pop() - if realFilename = window.CoffeeScriptToFilename[filename] - e = { - name: e.name, - message: e.message, - lineNumber: "~" + String(e.line), - sourceURL: realFilename - } + + e = + name: e.name + message: e.message + sourceURL: e.sourceURL + lineNumber: e.line + + if window.CoffeeScriptToFilename and realFilename = window.CoffeeScriptToFilename[filename] + e.sourceURL = realFilename + e.lineNumber = "~" + String(e.line) e diff --git a/vendor/assets/javascripts/jasmine.HeadlessReporter.Verbose.coffee b/vendor/assets/javascripts/jasmine.HeadlessReporter.Verbose.coffee index 1d1d0b0..591796f 100644 --- a/vendor/assets/javascripts/jasmine.HeadlessReporter.Verbose.coffee +++ b/vendor/assets/javascripts/jasmine.HeadlessReporter.Verbose.coffee @@ -1,6 +1,8 @@ #= require jasmine.HeadlessReporter.ConsoleBase # class jasmine.HeadlessReporter.Verbose extends jasmine.HeadlessReporter.ConsoleBase + @prereport = false + displaySuccess: (spec) => this.displaySpec(spec, 'green') @@ -46,12 +48,17 @@ class jasmine.HeadlessReporter.Verbose extends jasmine.HeadlessReporter.ConsoleB colorLine: (line, color) => line.foreground(color) + reportSpecStarting: (spec) => + if jasmine.HeadlessReporter.Verbose.prereport + this.puts(spec.getSpecSplitName().join(' ')) + reportException: (e) => e = JHW.createCoffeeScriptFileException(e) - - output = e.message + if e.sourceURL && e.lineNumber - output = "#{e.sourceURL}:~#{e.lineNumber} #{output}" + output = "#{e.sourceURL}:#{e.lineNumber} #{e.message}" + else + output = e.message ? e this.puts(output.foreground('yellow')) diff --git a/vendor/assets/javascripts/prolog.js b/vendor/assets/javascripts/prolog.js index 3ada0bb..e977e42 100644 --- a/vendor/assets/javascripts/prolog.js +++ b/vendor/assets/javascripts/prolog.js @@ -84,15 +84,17 @@ }; JHW.createCoffeeScriptFileException = function(e) { var filename, realFilename; - if (e && e.sourceURL && window.CoffeeScriptToFilename) { + if (e && e.sourceURL) { filename = e.sourceURL.split('/').pop(); - if (realFilename = window.CoffeeScriptToFilename[filename]) { - e = { - name: e.name, - message: e.message, - lineNumber: "~" + String(e.line), - sourceURL: realFilename - }; + e = { + name: e.name, + message: e.message, + sourceURL: e.sourceURL, + lineNumber: e.line + }; + if (window.CoffeeScriptToFilename && (realFilename = window.CoffeeScriptToFilename[filename])) { + e.sourceURL = realFilename; + e.lineNumber = "~" + String(e.line); } } return e; -- 2.45.2 From 8de1bd8cf012522d00df5063c3bbe16bca23ed72 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Jan 2012 18:07:08 -0500 Subject: [PATCH 3/8] clean up some fixes --- ext/jasmine-webkit-specrunner/Runner.cpp | 4 +++- lib/jasmine/headless/runner.rb | 6 +++++- spec/lib/jasmine/headless/runner_spec.rb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/jasmine-webkit-specrunner/Runner.cpp b/ext/jasmine-webkit-specrunner/Runner.cpp index cc20f49..66943a5 100644 --- a/ext/jasmine-webkit-specrunner/Runner.cpp +++ b/ext/jasmine-webkit-specrunner/Runner.cpp @@ -71,7 +71,9 @@ void Runner::loadSpec() outputFiles.enqueue(outputFile); } - page.mainFrame()->load(runnerFiles.dequeue()); + QString runnerFile = runnerFiles.dequeue(); + + page.mainFrame()->load(runnerFile); ticker.start(); } diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index 4f3245e..251ed08 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -64,7 +64,11 @@ module Jasmine command += targets.flatten.collect do |target| target = File.expand_path(target) - target = server_uri + target if options[:use_server] + if options[:use_server] + target = server_uri + target + else + target = "file://" + target + end target end diff --git a/spec/lib/jasmine/headless/runner_spec.rb b/spec/lib/jasmine/headless/runner_spec.rb index 46b4e94..1def973 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(target) } + it { should include('file://' + File.expand_path(target)) } end context 'colors' do -- 2.45.2 From f51f041b85d5e69baf339cb46c254cc152bc24cc Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Jan 2012 18:40:25 -0500 Subject: [PATCH 4/8] webrick seems to need log write locations or things get weird --- lib/jasmine/headless/runner.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index 251ed08..12c50c9 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -141,7 +141,15 @@ module Jasmine end end - Rack::Handler::WEBrick.run(responder, :Port => server_port, :Logger => Logger.new(StringIO.new), :AccessLog => [ nil, nil ]) + Rack::Handler::WEBrick.run( + responder, + :Port => server_port, + :Logger => Logger.new(StringIO.new), + :AccessLog => [ + [ StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT ], + [ StringIO.new, WEBrick::AccessLog::REFERER_LOG_FORMAT ] + ] + ) end while true do -- 2.45.2 From d3c3e7bd7dc59515999af664420eb79ce9e71cc5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 13 Jan 2012 07:45:27 -0500 Subject: [PATCH 5/8] don't reinvent the wheel --- lib/jasmine/headless/runner.rb | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index 12c50c9..b8413e0 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -125,24 +125,14 @@ module Jasmine end def wrap_in_server - require 'rack' require 'webrick' require 'thread' + require 'rack' require 'net/http' server = Thread.new do - responder = lambda do |env| - file = Pathname(env['PATH_INFO']) - - if file.file? - [ 200, { 'Content-Type' => 'text/html' }, [ file.read ] ] - else - [ 404, {}, [ 'Not found' ] ] - end - end - Rack::Handler::WEBrick.run( - responder, + Rack::File.new('/'), :Port => server_port, :Logger => Logger.new(StringIO.new), :AccessLog => [ -- 2.45.2 From 47b8d10d5659102b30b74cdf2dae3b80bd54e020 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 17 Jan 2012 13:32:57 -0500 Subject: [PATCH 6/8] server runner updated --- config/cucumber.yml | 2 + ext/jasmine-webkit-specrunner/Runner.h | 2 + .../jasmine-webkit-specrunner.pro | 19 --- lib/jasmine/headless/runner.rb | 123 +++++++++++------- spec/lib/jasmine/headless/runner_spec.rb | 22 +++- 5 files changed, 103 insertions(+), 65 deletions(-) create mode 100644 config/cucumber.yml delete mode 100644 ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro diff --git a/config/cucumber.yml b/config/cucumber.yml new file mode 100644 index 0000000..a061551 --- /dev/null +++ b/config/cucumber.yml @@ -0,0 +1,2 @@ +default: -r features + diff --git a/ext/jasmine-webkit-specrunner/Runner.h b/ext/jasmine-webkit-specrunner/Runner.h index 8d7269e..3f1f01b 100644 --- a/ext/jasmine-webkit-specrunner/Runner.h +++ b/ext/jasmine-webkit-specrunner/Runner.h @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include "Page.h" diff --git a/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro b/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro deleted file mode 100644 index e6710d1..0000000 --- a/ext/jasmine-webkit-specrunner/jasmine-webkit-specrunner.pro +++ /dev/null @@ -1,19 +0,0 @@ -###################################################################### -# Automatically generated by qmake (2.01a) Tue Aug 2 10:37:48 2011 -###################################################################### - -TEMPLATE = app -TARGET = -DEPENDPATH += . HeadlessSpecRunner Test -INCLUDEPATH += . HeadlessSpecRunner Test - -# Input -HEADERS += HeadlessSpecRunner/ConsoleOutput.h \ - HeadlessSpecRunner/Page.h \ - HeadlessSpecRunner/Runner.h \ - Test/Page_test.h -SOURCES += specrunner.cpp \ - HeadlessSpecRunner/ConsoleOutput.cpp \ - HeadlessSpecRunner/Page.cpp \ - HeadlessSpecRunner/Runner.cpp \ - Test/Page_test.cpp diff --git a/lib/jasmine/headless/runner.rb b/lib/jasmine/headless/runner.rb index b8413e0..964aef6 100644 --- a/lib/jasmine/headless/runner.rb +++ b/lib/jasmine/headless/runner.rb @@ -25,9 +25,81 @@ module Jasmine attr_reader :options - class << self - def run(options = {}) - new(options).run + def self.run(options = {}) + new(options).run + end + + def self.server_port + return @server_port if @server_port + + count = 100 + begin + port = select_server_port + + socket = TCPSocket.new(server_interface, port) + socket.close + + count -= 1 + + raise "Could not create server port after 100 attempts!" if count == 0 + rescue Errno::ECONNREFUSED + @server_port = port + + break + ensure + begin + socket.close if socket + rescue IOError + end + end while true + + @server_port + end + + def self.select_server_port + 21000 + rand(10000) + end + + def self.server_interface + '127.0.0.1' + end + + def self.server_uri + "http://#{server_interface}:#{server_port}" + end + + def self.ensure_server + return if @server + + require 'webrick' + require 'thread' + require 'rack' + require 'net/http' + + port = server_port + + @server = Thread.new do + Jasmine::Headless.warn "Powering up!" + + Rack::Handler::WEBrick.run( + Rack::File.new('/'), + :Port => port, + :Logger => Logger.new(StringIO.new), + :AccessLog => [ + [ StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT ], + [ StringIO.new, WEBrick::AccessLog::REFERER_LOG_FORMAT ] + ] + ) + end + + while true do + begin + Net::HTTP.get(URI(server_uri)) + break + rescue Errno::ECONNREFUSED => e + end + + sleep 0.1 end end @@ -65,7 +137,7 @@ module Jasmine command += targets.flatten.collect do |target| target = File.expand_path(target) if options[:use_server] - target = server_uri + target + target = self.class.server_uri + target else target = "file://" + target end @@ -125,53 +197,14 @@ module Jasmine end def wrap_in_server - require 'webrick' - require 'thread' - require 'rack' - require 'net/http' + self.class.ensure_server - server = Thread.new do - Rack::Handler::WEBrick.run( - Rack::File.new('/'), - :Port => server_port, - :Logger => Logger.new(StringIO.new), - :AccessLog => [ - [ StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT ], - [ StringIO.new, WEBrick::AccessLog::REFERER_LOG_FORMAT ] - ] - ) - end - - while true do - begin - Net::HTTP.get(URI(server_uri)) - break - rescue Errno::ECONNREFUSED => e - end - - sleep 0.1 - end - - Jasmine::Headless.warn "HTTP powered specs!" + Jasmine::Headless.warn "HTTP powered specs! Located at #{self.class.server_uri}" yield - - Thread.kill(server) end private - def server_port - @server_port ||= 21000 + rand(1000) - end - - def server_interface - '127.0.0.1' - end - - def server_uri - "http://#{server_interface}:#{server_port}" - end - def jasmine_config_data raise JasmineConfigNotFound.new("Jasmine config not found. I tried #{@options[:jasmine_config]}.") if !File.file?(@options[:jasmine_config]) diff --git a/spec/lib/jasmine/headless/runner_spec.rb b/spec/lib/jasmine/headless/runner_spec.rb index 1def973..f42c4d7 100644 --- a/spec/lib/jasmine/headless/runner_spec.rb +++ b/spec/lib/jasmine/headless/runner_spec.rb @@ -107,7 +107,7 @@ describe Jasmine::Headless::Runner do before do options[:use_server] = true - runner.stubs(:server_uri).returns(server_uri) + described_class.stubs(:server_uri).returns(server_uri) end it { should include(server_uri + File.expand_path(target)) } @@ -206,4 +206,24 @@ describe Jasmine::Headless::Runner do subject.options[:reporters].should == reporters end end + + describe '.server_port' do + before do + described_class.instance_variable_set(:@server_port, nil) + end + + context 'port in use' do + require 'socket' + + before do + described_class.stubs(:select_server_port).returns(5000, 5001) + end + + it 'should try another port' do + server = TCPServer.new(described_class.server_interface, 5000) + + described_class.server_port.should == 5001 + end + end + end end -- 2.45.2 From 0d4550524fa1822eb814f4a2f7f5d5df4f175b94 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 18 Jan 2012 18:04:36 -0500 Subject: [PATCH 7/8] 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 -- 2.45.2 From 75dce5b66bdb89d74b4fd7cb886805068a10572a Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sun, 22 Jan 2012 18:15:45 -0500 Subject: [PATCH 8/8] Ability to register custom template engines via Jasmine::Headless.register_engine hook --- lib/jasmine/headless/files_list.rb | 38 ++++++++++++++++++++ spec/lib/jasmine/headless/files_list_spec.rb | 15 ++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index eef115a..7844c7f 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -38,6 +38,7 @@ module Jasmine::Headless def reset! @asset_paths = nil + @registered_engines = {} # register haml-sprockets and handlebars_assets if it's available... %w{haml-sprockets handlebars_assets}.each do |library| @@ -67,6 +68,21 @@ module Jasmine::Headless register_engine '.css', Jasmine::Headless::CSSTemplate register_engine '.jst', Jasmine::Headless::JSTTemplate end + + end + + def registered_engines + @registered_engines ||= {} + end + + def register_engine(file_extension, template_class) + registered_engines[file_extension] = template_class + end + + def register_engines! + registered_engines.each do |file_extension, template_class| + Sprockets.register_engine file_extension, template_class + end end def default_files @@ -95,11 +111,21 @@ module Jasmine::Headless @required_files = UniqueAssetList.new @potential_files_to_filter = [] + register_engines! + load_initial_assets use_config if config? end + def register_engines! + begin + require spec_helper + rescue LoadError + end + self.class.register_engines! + end + def load_initial_assets self.class.default_files.each do |file| begin @@ -301,5 +327,17 @@ module Jasmine::Headless end end end + + def spec_helper + File.join(spec_dir, "helpers", "spec_helper") + end end end + +module Jasmine::Headless + extend self + + def register_engine(file_extension, template_class) + Jasmine::Headless::FilesList.register_engine(file_extension, template_class) + end +end \ No newline at end of file diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index bd24076..6bddb71 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -218,6 +218,21 @@ describe Jasmine::Headless::FilesList do files_list.files.any? { |file| file['.erb'] }.should be_false end end + + describe "#register_engine!" do + + before(:each) do + Jasmine::Headless::FilesList.reset! + end + + it "should register code added via configure blocks" do + template_class = mock() + described_class.register_engine ".foo", template_class + Sprockets.expects(:register_engine).with(".foo", template_class) + described_class.new + end + + end end end -- 2.45.2