Pass a Proc to SimpleServer.start instead of an array to defer spec file finding.

This commit is contained in:
Aaron Peckham & Christian Williams 2009-08-10 13:44:02 -07:00
parent bf3e3c60e5
commit 6e9cd25150
2 changed files with 58 additions and 8 deletions

View File

@ -20,14 +20,14 @@ module Jasmine
end end
class RunAdapter class RunAdapter
def initialize(spec_files) def initialize(spec_files_or_proc)
p "spec_files: #{spec_files}" @spec_files_or_proc = spec_files_or_proc
@spec_files = spec_files
end end
def call(env) def call(env)
spec_files = @spec_files spec_files = @spec_files_or_proc
spec_files = spec_files.call if spec_files.respond_to?(:call)
body = ERB.new(File.read(File.join(File.dirname(__FILE__), "run.html"))).result(binding) body = ERB.new(File.read(File.join(File.dirname(__FILE__), "run.html"))).result(binding)
[ [
200, 200,
@ -38,11 +38,11 @@ module Jasmine
end end
class SimpleServer class SimpleServer
def self.start(port, spec_dir, mappings) def self.start(port, spec_files_or_proc, mappings)
require 'thin' require 'thin'
config = { config = {
'/run.html' => Jasmine::RunAdapter.new(spec_dir) '/run.html' => Jasmine::RunAdapter.new(spec_files_or_proc)
} }
mappings.each do |from, to| mappings.each do |from, to|
config[from] = Rack::File.new(to) config[from] = Rack::File.new(to)
@ -50,7 +50,6 @@ module Jasmine
app = Rack::URLMap.new(config) app = Rack::URLMap.new(config)
server_port = Jasmine::find_unused_port
Thin::Server.start('0.0.0.0', port, app) Thin::Server.start('0.0.0.0', port, app)
end end
end end

View File

@ -0,0 +1,51 @@
require 'spec'
require 'open-uri'
require File.dirname(__FILE__) + '/jasmine_runner'
describe Jasmine::SimpleServer do
before do
@port = Jasmine::find_unused_port
end
after do
Process.kill "TERM", -@jasmine_server_pid if @jasmine_server_pid
end
it "should start and print script tags" do
@jasmine_server_pid = fork do
Process.setpgrp
Jasmine::SimpleServer.start(@port, ["file1", "file2"], {})
exit! 0
end
Jasmine::Runner.new(nil, nil, nil).wait_for_listener(@port)
run_html = open("http://localhost:#{@port}/run.html").read
run_html.should =~ /<script src="file1"/
run_html.should =~ /<script src="file2"/
end
it "should take a proc that returns a list of spec files" do
spec_fileses = [["file1", "file2"], ["file1", "file2", "file3"]]
spec_files_proc = lambda do
spec_fileses.shift
end
@jasmine_server_pid = fork do
Process.setpgrp
Jasmine::SimpleServer.start(@port, spec_files_proc, {})
exit! 0
end
Jasmine::Runner.new(nil, nil, nil).wait_for_listener(@port)
run_html = open("http://localhost:#{@port}/run.html").read
run_html.should =~ /<script src="file1"/
run_html.should =~ /<script src="file2"/
run_html = open("http://localhost:#{@port}/run.html").read
run_html.should =~ /<script src="file1"/
run_html.should =~ /<script src="file2"/
run_html.should =~ /<script src="file3"/
end
end