From 5d43eb21937ed972eaefc1469f255cc85b73bfe9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 12 Jan 2012 15:02:32 -0500 Subject: [PATCH] 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