module Jasmine

Constants

VERSION

Public Class Methods

config() click to toggle source
# File lib/jasmine/config.rb, line 60
def self.config
  initialize_config
  @config
end
configure(&block) click to toggle source
# File lib/jasmine/config.rb, line 4
def self.configure(&block)
  block.call(self.config)
end
find_unused_port() click to toggle source
# File lib/jasmine/base.rb, line 15
def self.find_unused_port
  socket = open_socket_on_unused_port
  port = socket.addr[1]
  socket.close
  port
end
initialize_config() click to toggle source
# File lib/jasmine/config.rb, line 8
def self.initialize_config
  return if @config
  @config = Jasmine::Configuration.new
  core_config = Jasmine::CoreConfiguration.new

  @config.add_path_mapper(Jasmine::PathMapper.method(:new))

  @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)

  if Jasmine::Dependencies.rails_3_asset_pipeline?
    @config.add_path_mapper(lambda { |config|
      asset_expander = Jasmine::AssetExpander.new(
        Jasmine::AssetPipelineUtility.method(:bundled_asset_factory),
        Jasmine::AssetPipelineUtility.method(:asset_path_for)
      )
      Jasmine::AssetPipelineMapper.new(config, asset_expander.method(:expand))
    })
    @config.add_rack_path('/assets', lambda {
      # In order to have asset helpers like asset_path and image_path, we need to require 'action_view/base'.  This
      # triggers run_load_hooks on action_view which, in turn, causes sprockets/railtie to load the Sprockets asset
      # helpers.  Alternatively, you can include the helpers yourself without loading action_view/base:
      Rails.application.assets.context_class.instance_eval do
        include ::Sprockets::Helpers::IsolatedHelper
        include ::Sprockets::Helpers::RailsHelper
      end
      Rails.application.assets
    })
  end

end
load_configuration_from_yaml(path = nil) click to toggle source
# File lib/jasmine/config.rb, line 65
def self.load_configuration_from_yaml(path = nil)
  path ||= File.join(Dir.pwd, 'spec', 'javascripts', 'support', 'jasmine.yml')
  if File.exist?(path)
    yaml_loader = lambda do |filepath|
      YAML::load(ERB.new(File.read(filepath)).result(binding)) if File.exist?(filepath)
    end
    yaml_config = Jasmine::YamlConfigParser.new(path, Dir.pwd, Jasmine::PathExpander.method(:expand), yaml_loader)
    Jasmine.configure do |config|
      config.jasmine_dir = yaml_config.jasmine_dir if yaml_config.jasmine_dir
      config.jasmine_files = lambda { yaml_config.jasmine_files } if yaml_config.jasmine_files.any?
      config.jasmine_css_files = lambda { yaml_config.jasmine_css_files } if yaml_config.jasmine_css_files.any?
      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
    require yaml_config.spec_helper if File.exist?(yaml_config.spec_helper)
  end
end
open_socket_on_unused_port() click to toggle source

this seemingly-over-complex method is necessary to get an open port on at least some of our Macs

# File lib/jasmine/base.rb, line 6
def self.open_socket_on_unused_port
  infos = Socket::getaddrinfo("localhost", nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
  families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]

  return TCPServer.open('0.0.0.0', 0) if families.has_key?('AF_INET')
  return TCPServer.open('::', 0) if families.has_key?('AF_INET6')
  return TCPServer.open(0)
end
root(*paths) click to toggle source
# File lib/jasmine/base.rb, line 50
def self.root(*paths)
  File.expand_path(File.join(File.dirname(__FILE__), *paths))
end
runner_filepath() click to toggle source
# File lib/jasmine/base.rb, line 42
def self.runner_filepath
  File.expand_path(File.join(File.dirname(__FILE__), "run_specs.rb"))
end
runner_template() click to toggle source
# File lib/jasmine/base.rb, line 46
def self.runner_template
  File.read(File.join(File.dirname(__FILE__), "run.html.erb"))
end
server_is_listening_on(hostname, port) click to toggle source
# File lib/jasmine/base.rb, line 22
def self.server_is_listening_on(hostname, port)
  require 'socket'
  begin
    socket = TCPSocket.open(hostname, port)
  rescue Errno::ECONNREFUSED
    return false
  end
  socket.close
  true
end
wait_for_listener(port, name = "required process", seconds_to_wait = 20) click to toggle source
# File lib/jasmine/base.rb, line 33
def self.wait_for_listener(port, name = "required process", seconds_to_wait = 20)
  time_out_at = Time.now + seconds_to_wait
  until server_is_listening_on "localhost", port
    sleep 0.1
    puts "Waiting for #{name} on #{port}..."
    raise "#{name} didn't show up on port #{port} after #{seconds_to_wait} seconds." if Time.now > time_out_at
  end
end