Skip to content

Commit

Permalink
Jasmine::Server now responsible for starting Rack
Browse files Browse the repository at this point in the history
  • Loading branch information
ragaskar committed Jul 14, 2012
1 parent ef10991 commit 420d5d8
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 134 deletions.
1 change: 1 addition & 0 deletions lib/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'dependencies',
'runner_config',
'config',
'application',
'server',
'selenium_driver',
'rspec_formatter',
Expand Down
40 changes: 40 additions & 0 deletions lib/jasmine/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rack'
require 'rack/utils'
require 'jasmine-core'
require 'rack/jasmine/runner'
require 'rack/jasmine/focused_suite'
require 'rack/jasmine/redirect'
require 'rack/jasmine/cache_control'
require 'ostruct'

module Jasmine
class Application
def self.app(config = Jasmine::RunnerConfig.new)
page = Jasmine::Page.new(config)
Rack::Builder.app do
use Rack::Head
use Rack::Jasmine::CacheControl
if Jasmine::Dependencies.rails_3_asset_pipeline?
map('/assets') do
run Rails.application.assets
end
end

map('/run.html') { run Rack::Jasmine::Redirect.new('/') }
map('/__suite__') { run Rack::Jasmine::FocusedSuite.new(config) }

#TODO: These path mappings should come from the config.
map('/__JASMINE_ROOT__') { run Rack::File.new(Jasmine::Core.path) }
map(config.spec_path) { run Rack::File.new(config.spec_dir) }
map(config.root_path) { run Rack::File.new(config.project_root) }

map('/') do
run Rack::Cascade.new([
Rack::URLMap.new('/' => Rack::File.new(config.src_dir)),
Rack::Jasmine::Runner.new(page)
])
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/jasmine/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def rails3?
safe_gem_check("rails", ">= 3.0")
end

def legacy_rack?
!Rack.constants.include?(:Server)
end

def rails_3_asset_pipeline?
rails3? && Rails.respond_to?(:application) && Rails.application.respond_to?(:assets) && Rails.application.assets
end
Expand Down
13 changes: 1 addition & 12 deletions lib/jasmine/rspec_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ def report_spec(spec_id)
puts out unless out.empty?
end

def start_server(port = 8888)
if defined? Rack::Server # Rack ~>1.0 compatibility
server = Rack::Server.new(:Port => port, :AccessLog => [])
server.instance_variable_set(:@app, Jasmine.app(@config)) # workaround for Rack bug, when Rack > 1.2.1 is released Rack::Server.start(:app => Jasmine.app(self)) will work
server.start
else
handler = Rack::Handler.get('webrick')
handler.run(Jasmine.app(@config), :Port => port, :AccessLog => [])
end
end

private

def eval_js(script)
Expand Down Expand Up @@ -188,7 +177,7 @@ def start_jasmine_server
@jasmine_server_port = jasmine_port
t = Thread.new do
begin
start_server(@jasmine_server_port)
Jasmine::Server.new(@jasmine_server_port, Jasmine::Application.app(@config)).start
rescue ChildProcess::TimeoutError; end
#ignore bad exits
end
Expand Down
46 changes: 14 additions & 32 deletions lib/jasmine/server.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
require 'rack'
require 'rack/utils'
require 'jasmine-core'
require 'rack/jasmine/runner'
require 'rack/jasmine/focused_suite'
require 'rack/jasmine/redirect'
require 'rack/jasmine/cache_control'
require 'ostruct'

module Jasmine
def self.app(runner_config)
page = Jasmine::Page.new(runner_config)
Rack::Builder.app do
use Rack::Head
use Rack::Jasmine::CacheControl
if Jasmine::Dependencies.rails_3_asset_pipeline?
map('/assets') do
run Rails.application.assets
end
end

map('/run.html') { run Rack::Jasmine::Redirect.new('/') }
map('/__suite__') { run Rack::Jasmine::FocusedSuite.new(runner_config) }

#TODO: These path mappings should come from the runner_config.
map('/__JASMINE_ROOT__') { run Rack::File.new(Jasmine::Core.path) }
map(runner_config.spec_path) { run Rack::File.new(runner_config.spec_dir) }
map(runner_config.root_path) { run Rack::File.new(runner_config.project_root) }
class Server
def initialize(port = 8888, application = Jasmine::Application.app)
@port = port
@application = application
end

map('/') do
run Rack::Cascade.new([
Rack::URLMap.new('/' => Rack::File.new(runner_config.src_dir)),
Rack::Jasmine::Runner.new(page)
])
def start
if Jasmine::Dependencies.legacy_rack?
handler = Rack::Handler.get('webrick')
handler.run(@application, :Port => @port, :AccessLog => [])
else
server = Rack::Server.new(:Port => @port, :AccessLog => [])
# workaround for Rack bug, when Rack > 1.2.1 is released Rack::Server.start(:app => Jasmine.app(self)) will work
server.instance_variable_set(:@app, @application)
server.start
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jasmine/tasks/jasmine.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ namespace :jasmine do
task :server => "jasmine:require" do
jasmine_config_overrides = File.join(Jasmine::Config.new.project_root, 'spec', 'javascripts' ,'support' ,'jasmine_config.rb')
require jasmine_config_overrides if File.exist?(jasmine_config_overrides)

port = ENV['JASMINE_PORT'] || 8888
puts "your tests are here:"
puts " http://localhost:#{port}/"
Jasmine::SpecBuilder.new(Jasmine::RunnerConfig.new).start_server(port)
Jasmine::Server.new(port).start
end
end

Expand Down
99 changes: 99 additions & 0 deletions spec/application_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'spec_helper'
require 'rack/test'

describe "Jasmine::Application" do
include Rack::Test::Methods

def app
@root = File.join(File.dirname(__FILE__))
runner_config = double("config",
:project_root => @root,
:spec_dir => File.join(@root, "fixture", "spec"),
:spec_path => "/__spec__",
:root_path => "/__root__",
:css_files => [],
:jasmine_files => [],
:js_files => ["path/file1.js", "path/file2.js"],
:src_dir => File.join(@root, "fixture", "src"),
:src_files => ["file1.js"],
:spec_files => ["example_spec.js"])
Jasmine::Application.app(runner_config)
end

it "includes no-cache headers for specs" do
get "/__spec__/example_spec.js"
last_response.headers.should have_key("Cache-Control")
last_response.headers["Cache-Control"].should == "max-age=0, private, must-revalidate"
end

it "should serve static files from spec dir under __spec__" do
get "/__spec__/example_spec.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/spec/example_spec.js"))
end

it "should serve static files from root dir under __root__" do
get "/__root__/fixture/src/example.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
end

it "should serve static files from src dir under /" do
get "/example.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
end

it "should serve Jasmine static files under /__JASMINE_ROOT__/" do
get "/__JASMINE_ROOT__/jasmine.css"
last_response.status.should == 200
last_response.content_type.should == "text/css"
last_response.body.should == File.read(File.join(Jasmine::Core.path, "jasmine.css"))
end

it "should serve focused suites when prefixing spec files with /__suite__/" do
pending "Temporarily removing this feature (maybe permanent)"
Dir.stub!(:glob).and_return { |glob_string| [glob_string] }
get "/__suite__/file2.js"
last_response.status.should == 200
last_response.content_type.should == "text/html"
last_response.body.should include("\"/__spec__/file2.js")
end

it "should redirect /run.html to /" do
get "/run.html"
last_response.status.should == 302
last_response.location.should == "/"
end

it "should 404 non-existent files" do
get "/some-non-existent-file"
last_response.should be_not_found
end

describe "/ page" do
it "should load each js file in order" do
get "/"
last_response.status.should == 200
last_response.body.should include("path/file1.js")
last_response.body.should include("path/file2.js")
end

it "should return an empty 200 for HEAD requests to /" do
head "/"
last_response.status.should == 200
last_response.headers['Content-Type'].should == 'text/html'
last_response.body.should == ''
end

it "should tell the browser not to cache any assets" do
head "/"
['Pragma'].each do |key|
last_response.headers[key].should == 'no-cache'
end
end
end
end
17 changes: 14 additions & 3 deletions spec/dependencies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Rails
context "when rails 3 is present and the application pipeline is in use" do
before do
Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
application.stub(:assets).and_return(rails_application_assets)
application.stub(:assets).and_return(rails_application_assets)
end
let(:rails3_present) { true }
let(:respond_to_application) { true }
Expand All @@ -95,7 +95,7 @@ module Rails
context "when rails 3 is present and the application pipeline is not in use" do
before do
Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
application.stub(:assets).and_return(rails_application_assets)
application.stub(:assets).and_return(rails_application_assets)
end
let(:rails3_present) { true }
let(:respond_to_application) { true }
Expand All @@ -105,7 +105,7 @@ module Rails
context "when rails 3 is present but not loaded" do
before do
Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
application.stub(:assets).and_return(rails_application_assets)
application.stub(:assets).and_return(rails_application_assets)
end
let(:rails3_present) { true }
let(:respond_to_application) { false }
Expand Down Expand Up @@ -232,5 +232,16 @@ module Rails
end
end

describe "legacy_rack?" do
it "should return false if Rack::Server exists" do
Rack.stub(:constants).and_return([:Server])
Jasmine::Dependencies.legacy_rack?.should be_false
end
it "should return true if Rack::Server does not exist" do
Rack.stub(:constants).and_return([])
Jasmine::Dependencies.legacy_rack?.should be_true
end
end
end

end
Loading

0 comments on commit 420d5d8

Please sign in to comment.