Module: Jasmine

Defined in:
lib/jasmine/base.rb,
lib/jasmine/config.rb,
lib/jasmine/spec_builder.rb,
lib/jasmine/selenium_driver.rb,
lib/jasmine/command_line_tool.rb,
lib/jasmine/server.rb

Defined Under Namespace

Classes: CommandLineTool, Config, FocusedSuite, JsAlert, Redirect, RunAdapter, SeleniumDriver, SpecBuilder

Class Method Summary collapse

Class Method Details

.app(config) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jasmine/server.rb', line 101

def self.app(config)
  Rack::Builder.app do
    use Rack::Head

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

    map('/__JASMINE_ROOT__') { run Rack::File.new(Jasmine.root) }
    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)),
        Jasmine::RunAdapter.new(config)
      ])
    end
  end
end

.cachebust(files, root_dir = "", replace = nil, replace_with = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jasmine/base.rb', line 47

def self.cachebust(files, root_dir="", replace=nil, replace_with=nil)
  require 'digest/md5'
  files.collect do |file_name|
    real_file_name = replace && replace_with ? file_name.sub(replace, replace_with) : file_name
    begin
      digest = Digest::MD5.hexdigest(File.read("#{root_dir}#{real_file_name}"))
    rescue
      digest = "MISSING-FILE"
    end
    "#{file_name}?cachebust=#{digest}"
  end
end

.find_unused_portObject



20
21
22
23
24
25
# File 'lib/jasmine/base.rb', line 20

def self.find_unused_port
  socket = open_socket_on_unused_port
  port = socket.addr[1]
  socket.close
  port
end

.open_socket_on_unused_portObject

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



11
12
13
14
15
16
17
18
# File 'lib/jasmine/base.rb', line 11

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

.rootObject



6
7
8
# File 'lib/jasmine/base.rb', line 6

def self.root
  ENV["JASMINE_ROOT"] || File.expand_path(File.join(File.dirname(__FILE__), '../../jasmine'))
end

.server_is_listening_on(hostname, port) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/jasmine/base.rb', line 27

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 = 10) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/jasmine/base.rb', line 38

def self.wait_for_listener(port, name = "required process", seconds_to_wait = 10)
  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