Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Refactor Jasmine gem
Browse files Browse the repository at this point in the history
- Configuration of Rack server moves into actual Jasmine config
- Mapping of served files moved out of configuration
- Simplify Rack application and run.html.erb template
- Remove cruft: many fixtures no longer used. Jasmine Redirect
  (previously redirected run.html to /) removed.
- Jasmine bootstrap moved out of runner template, into js file.
- Add ability to customize jasmine/boot files.
- Wrap Jasmine::Core calls in CoreConfiguration
- Fix jasmine.yml (was missing spec_file, helper file defaults).
- Regression: asset pipeline not supported after this refactor.
- Break jasmine.yml processing out into a separate class.
Davis W. Frank & Rajan Agaskar committed Nov 28, 2012
1 parent d3c1bea commit 76b7cde
Showing 36 changed files with 877 additions and 895 deletions.
13 changes: 11 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -29,10 +29,19 @@ task :default => :spec

namespace :jasmine do
require "jasmine-core"
require './spec/jasmine_self_test_config'
task :server do
port = ENV['JASMINE_PORT'] || 8888
JasmineSelfTestConfig.new.start_server(port)
Jasmine.configure do |config|
root = File.expand_path(File.join(File.dirname(__FILE__), ".."))
config.src_dir = File.join(root, 'src')
config.spec_dir = Jasmine::Core.path
config.spec_files = lambda { (Jasmine::Core.html_spec_files + Jasmine::Core.core_spec_files).map {|f| File.join(config.spec_dir, f) } }
end

config = Jasmine.config

server = Jasmine::Server.new(8888, Jasmine::Application.app(config))
server.start

puts "your tests are here:"
puts " http://localhost:#{port}/"
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ stylesheets:
# - helpers/**/*.js
#
helpers:
- helpers/**/*.js
- 'helpers/**/*.js'

# spec_files
#
@@ -53,7 +53,7 @@ helpers:
# EXAMPLE:
#
# spec_files:
# - **/*[sS]pec.js
# - '**/*[sS]pec.js'
#
spec_files:
- '**/*[sS]pec.js'
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ stylesheets:
# - helpers/**/*.js
#
helpers:

- 'helpers/**/*.js'
# spec_files
#
# Return an array of filepaths relative to spec_dir to include.
@@ -49,6 +49,7 @@ helpers:
# - **/*[sS]pec.js
#
spec_files:
- '**/*[sS]pec.js'

# src_dir
#
9 changes: 8 additions & 1 deletion lib/jasmine.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
jasmine_files = ['base',
'dependencies',
'runner_config',
'core_configuration',
'configuration',
'config',
'application',
'server',
'selenium_driver',
'rspec_formatter',
'command_line_tool',
'page',
'path_mapper',
'asset_pipeline_mapper',
'sprockets_mapper',
'results_processor',
'results',
'path_expander',
'yaml_config_parser',
File.join('runners', 'http')]

jasmine_files.each do |file|
require File.join('jasmine', file)
end
# jasmine_rack_files.each do |file|
# require File.join('rack', 'jasmine', file)
# end

require File.join('jasmine', "railtie") if Jasmine::Dependencies.rails3?

39 changes: 6 additions & 33 deletions lib/jasmine/application.rb
Original file line number Diff line number Diff line change
@@ -3,46 +3,19 @@
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)
if Jasmine::Dependencies.rails_3_asset_pipeline?
config.src_mapper = Jasmine::AssetPipelineMapper.new
def self.app(config, builder = Rack::Builder.new)
config.rack_apps.each do |(app, config_block)|
builder.use(app, &config_block)
end
Rack::Builder.app do
use Rack::Head
use Rack::Jasmine::CacheControl
if Jasmine::Dependencies.rails_3_asset_pipeline?
map('/assets') do
#load the Sprockets asset helpers
Rails.application.assets.context_class.instance_eval do
include ::Sprockets::Helpers::IsolatedHelper
include ::Sprockets::Helpers::RailsHelper
end
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
config.rack_path_map.each do |path, handler|
builder.map(path) { run handler.call }
end
builder
end
end
end
4 changes: 4 additions & 0 deletions lib/jasmine/base.rb
Original file line number Diff line number Diff line change
@@ -47,4 +47,8 @@ def self.runner_template
File.read(File.join(File.dirname(__FILE__), "run.html.erb"))
end

def self.root(*paths)
File.expand_path(File.join(File.dirname(__FILE__), *paths))
end

end
162 changes: 48 additions & 114 deletions lib/jasmine/config.rb
Original file line number Diff line number Diff line change
@@ -1,122 +1,56 @@
module Jasmine
class Config
attr_accessor :src_mapper

require 'yaml'
require 'erb'
require 'json'

def match_files(dir, patterns)
dir = File.expand_path(dir)
negative, positive = patterns.partition {|pattern| /^!/ =~ pattern}
chosen, negated = [positive, negative].collect do |patterns|
patterns.collect do |pattern|
matches = Dir.glob(File.join(dir, pattern.gsub(/^!/,'')))
matches.empty? && !(pattern =~ /\*|^\!/) ? pattern : matches.collect {|f| f.sub("#{dir}/", "")}.sort
end.flatten.uniq
end
chosen - negated
end

def simple_config
config = File.exist?(simple_config_file) ? YAML::load(ERB.new(File.read(simple_config_file)).result(binding)) : false
config || {}
end


def spec_path
"/__spec__"
end

def root_path
"/__root__"
end

def js_files(spec_filter = nil)
spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) }
end

def user_stylesheets
stylesheets.collect {|f| "/" + f }
end

def spec_files_full_paths
spec_files.collect {|spec_file| File.join(spec_dir, spec_file) }
end

def project_root
Dir.pwd
end

def simple_config_file
File.join(project_root, 'spec/javascripts/support/jasmine.yml')
end

def src_dir
if simple_config['src_dir']
File.join(project_root, simple_config['src_dir'])
else
project_root
end
end

def spec_dir
if simple_config['spec_dir']
File.join(project_root, simple_config['spec_dir'])
else
File.join(project_root, 'spec/javascripts')
end
end

def helpers
if simple_config['helpers']
match_files(spec_dir, simple_config['helpers'])
else
match_files(spec_dir, ["helpers/**/*.js"])
end
end

def src_files
return [] unless simple_config['src_files']
def self.configure(&block)
block.call(self.config)
end

if self.src_mapper
self.src_mapper.files(simple_config['src_files'])
else
match_files(src_dir, simple_config['src_files'])
end
end
def self.initialize_config
return if @config
@config = Jasmine::Configuration.new
core_config = Jasmine::CoreConfiguration.new

@config.add_path_mapper(Jasmine::PathMapper)
@config.jasmine_path = jasmine_path = "/__jasmine__"
@config.src_path = src_path = "/"
@config.spec_path = spec_path = "/__spec__"
@config.boot_path = boot_path = "/__boot__"

@config.jasmine_dir = core_config.path
@config.boot_dir = core_config.boot_path
@config.boot_files = lambda { core_config.boot_files }
@config.jasmine_files = lambda { core_config.js_files }
@config.jasmine_css_files = lambda { core_config.css_files }

@config.add_rack_path(jasmine_path, lambda { Rack::File.new(config.jasmine_dir) })
@config.add_rack_path(boot_path, lambda { Rack::File.new(config.boot_dir) })
@config.add_rack_path(spec_path, lambda { Rack::File.new(config.spec_dir) })
@config.add_rack_path(src_path, lambda {
Rack::Cascade.new([
Rack::URLMap.new('/' => Rack::File.new(config.src_dir)),
Rack::Jasmine::Runner.new(Jasmine::Page.new(config))
])
})

@config.add_rack_app(Rack::Head)
@config.add_rack_app(Rack::Jasmine::CacheControl)
end

def spec_files
if simple_config['spec_files']
match_files(spec_dir, simple_config['spec_files'])
else
match_files(spec_dir, ["**/*[sS]pec.js"])
end
end
def self.config
initialize_config
@config
end

def stylesheets
if simple_config['stylesheets']
match_files(src_dir, simple_config['stylesheets'])
else
[]
def self.load_configuration_from_yaml(path = nil)
path ||= File.join(Dir.pwd, 'spec', 'javascripts', 'support', 'jasmine.yml')
if File.exist?(path)
yaml_config = Jasmine::YamlConfigParser.new(path, Dir.pwd, Jasmine::PathExpander.method(:expand), YAML.method(:load_file))
Jasmine.configure do |config|
config.src_files = lambda { yaml_config.src_files }
config.spec_files = lambda { yaml_config.helpers + yaml_config.spec_files }
config.css_files = lambda { yaml_config.css_files }
config.src_dir = yaml_config.src_dir
config.spec_dir = yaml_config.spec_dir
end
end

def jasmine_host
ENV["JASMINE_HOST"] || 'http://localhost'
end

def port
@port ||= ENV["JASMINE_PORT"] || Jasmine.find_unused_port
end

def jasmine_stylesheets
::Jasmine::Core.css_files.map {|f| "/__JASMINE_ROOT__/#{f}"}
end

def jasmine_javascripts
::Jasmine::Core.js_files.map {|f| "/__JASMINE_ROOT__/#{f}" }
end
end

end
Loading

0 comments on commit 76b7cde

Please sign in to comment.