Class: Jasmine::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jasmine/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



5
6
7
8
9
10
11
12
13
# File 'lib/jasmine/config.rb', line 5

def initialize(options = {})
  require 'selenium_rc'
  @selenium_jar_path = SeleniumRC::Server.allocate.jar_path
  @options = options

  @browser = options[:browser] ? options.delete(:browser) : 'firefox'
  @selenium_pid = nil
  @jasmine_server_pid = nil
end

Instance Method Details

#css_filesObject



106
107
108
# File 'lib/jasmine/config.rb', line 106

def css_files
  stylesheets
end

#eval_js(script) ⇒ Object



70
71
72
# File 'lib/jasmine/config.rb', line 70

def eval_js(script)
  @client.eval_js(script)
end

#js_filesObject



102
103
104
# File 'lib/jasmine/config.rb', line 102

def js_files
  src_files + spec_files.collect {|f| File.join(spec_path, f) }
end

#mappingsObject



95
96
97
98
99
100
# File 'lib/jasmine/config.rb', line 95

def mappings
  {
    spec_path => spec_dir,
    root_path => project_root
  }
end

#match_files(dir, patterns) ⇒ Object



74
75
76
77
78
79
# File 'lib/jasmine/config.rb', line 74

def match_files(dir, patterns)
  dir = File.expand_path(dir)
  patterns.collect do |pattern|
    Dir.glob(File.join(dir, pattern)).collect {|f| f.sub("#{dir}/", "/")}.sort
  end.flatten
end

#project_rootObject



114
115
116
# File 'lib/jasmine/config.rb', line 114

def project_root
  Dir.pwd
end

#root_pathObject



91
92
93
# File 'lib/jasmine/config.rb', line 91

def root_path
  "/__root__"
end

#runObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/jasmine/config.rb', line 59

def run
  begin
    start
    puts "servers are listening on their ports -- running the test script..."
    tests_passed = @client.run
  ensure
    stop
  end
  return tests_passed
end

#simple_configObject



81
82
83
84
# File 'lib/jasmine/config.rb', line 81

def simple_config
  config = File.exist?(simple_config_file) ? File.open(simple_config_file) { |yf| YAML::load( yf ) } : false
  config || {}
end

#simple_config_fileObject



118
119
120
# File 'lib/jasmine/config.rb', line 118

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

#spec_dirObject



130
131
132
133
134
135
136
# File 'lib/jasmine/config.rb', line 130

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

#spec_filesObject



149
150
151
152
153
154
155
# File 'lib/jasmine/config.rb', line 149

def spec_files
  files = match_files(spec_dir, "**/*.js")
  if simple_config['spec_files']
    files = match_files(spec_dir, simple_config['spec_files'])
  end
  files
end

#spec_files_full_pathsObject



110
111
112
# File 'lib/jasmine/config.rb', line 110

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

#spec_pathObject



87
88
89
# File 'lib/jasmine/config.rb', line 87

def spec_path
  "/__spec__"
end

#src_dirObject



122
123
124
125
126
127
128
# File 'lib/jasmine/config.rb', line 122

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

#src_filesObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/jasmine/config.rb', line 138

def src_files
  if simple_config['src_files']
    if simple_config['src_base_url']
      return simple_config['src_files'].map {|f| File.join(simple_config['src_base_url'], f)}
    else
      return match_files(src_dir, simple_config['src_files'])
    end
  end
  return []
end

#startObject



19
20
21
22
23
# File 'lib/jasmine/config.rb', line 19

def start
  start_servers
  @client = Jasmine::SeleniumDriver.new("localhost", @selenium_server_port, "*#{@browser}", "http://localhost:#{@jasmine_server_port}/")
  @client.connect
end

#start_server(port = 8888) ⇒ Object



15
16
17
# File 'lib/jasmine/config.rb', line 15

def start_server(port = 8888)
  Jasmine::Server.new(port, self).start
end

#start_serversObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jasmine/config.rb', line 30

def start_servers
  @jasmine_server_port = Jasmine::find_unused_port
  @selenium_server_port = Jasmine::find_unused_port

  server = Jasmine::Server.new(@jasmine_server_port, self)

  @selenium_pid = fork do
    Process.setpgrp
    exec "java -jar #{@selenium_jar_path} -port #{@selenium_server_port} > /dev/null 2>&1"
  end
  puts "selenium started.  pid is #{@selenium_pid}"

  @jasmine_server_pid = fork do
    Process.setpgrp
    server.start
    exit! 0
  end
  puts "jasmine server started.  pid is #{@jasmine_server_pid}"

  Jasmine::wait_for_listener(@selenium_server_port, "selenium server")
  Jasmine::wait_for_listener(@jasmine_server_port, "jasmine server")
end

#stopObject



25
26
27
28
# File 'lib/jasmine/config.rb', line 25

def stop
  @client.disconnect
  stop_servers
end

#stop_serversObject



53
54
55
56
57
# File 'lib/jasmine/config.rb', line 53

def stop_servers
  puts "shutting down the servers..."
  Jasmine::kill_process_group(@selenium_pid) if @selenium_pid
  Jasmine::kill_process_group(@jasmine_server_pid) if @jasmine_server_pid
end

#stylesheetsObject



157
158
159
160
161
162
163
# File 'lib/jasmine/config.rb', line 157

def stylesheets
  files = []
  if simple_config['stylesheets']
    files = match_files(src_dir, simple_config['stylesheets'])
  end
  files
end