Module: Jasmine

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

Defined Under Namespace

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

Class Method Summary collapse

Class Method Details

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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jasmine/base.rb', line 51

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

.kill_process_group(process_group_id, signal = "TERM") ⇒ Object



47
48
49
# File 'lib/jasmine/base.rb', line 47

def self.kill_process_group(process_group_id, signal="TERM")
  Process.kill signal, -process_group_id # negative pid means kill process group. (see man 2 kill)
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
  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