Class: Jasmine::Config

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

Defined Under Namespace

Modules: SeleniumServerForkHackForRSpec

Instance Method Summary collapse

Instance Method Details

#browserObject



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

def browser
  ENV["JASMINE_BROWSER"] || 'firefox'
end

#css_filesObject



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

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

#eval_js(script) ⇒ Object



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

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

#external_selenium_server_portObject



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

def external_selenium_server_port
  ENV['SELENIUM_SERVER_PORT'] && ENV['SELENIUM_SERVER_PORT'].to_i > 0 ? ENV['SELENIUM_SERVER_PORT'].to_i : nil
end

#helpersObject



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

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

#jasmine_hostObject



10
11
12
# File 'lib/jasmine/config.rb', line 10

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

#js_files(spec_filter = nil) ⇒ Object



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

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

#match_files(dir, patterns) ⇒ Object



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

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

#project_rootObject



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

def project_root
  Dir.pwd
end

#root_pathObject



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

def root_path
  "/__root__"
end

#runObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/jasmine/config.rb', line 64

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



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

def simple_config
  config = File.exist?(simple_config_file) ? YAML::load(ERB.new(File.read(simple_config_file)).result(binding)) : 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



154
155
156
157
158
159
160
# File 'lib/jasmine/config.rb', line 154

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

#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



93
94
95
# File 'lib/jasmine/config.rb', line 93

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



146
147
148
149
150
151
152
# File 'lib/jasmine/config.rb', line 146

def src_files
  if simple_config['src_files']
    match_files(src_dir, simple_config['src_files'])
  else
    []
  end
end

#startObject



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

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

#start_jasmine_serverObject



33
34
35
36
37
38
39
40
# File 'lib/jasmine/config.rb', line 33

def start_jasmine_server
  @jasmine_server_port = Jasmine::find_unused_port
  Thread.new do
    start_server(@jasmine_server_port)
  end
  Jasmine::wait_for_listener(@jasmine_server_port, "jasmine server")
  puts "jasmine server started."
end

#start_selenium_serverObject



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

def start_selenium_server
  @selenium_server_port = external_selenium_server_port
  if @selenium_server_port.nil?
    @selenium_server_port = Jasmine::find_unused_port
    require 'selenium_rc'
    SeleniumRC::Server.send(:include, SeleniumServerForkHackForRSpec)
    SeleniumRC::Server.boot("localhost", @selenium_server_port, :args => [windows? ? ">NUL" : "> /dev/null"])
  else
    Jasmine::wait_for_listener(@selenium_server_port, "selenium server")
  end
end

#start_server(port = 8888) ⇒ Object



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

def start_server(port = 8888)
  handler = Rack::Handler.default
  handler.run Jasmine.app(self), :Port => port, :AccessLog => []
end

#start_serversObject



59
60
61
62
# File 'lib/jasmine/config.rb', line 59

def start_servers
  start_jasmine_server
  start_selenium_server
end

#stopObject



29
30
31
# File 'lib/jasmine/config.rb', line 29

def stop
  @client.disconnect
end

#stylesheetsObject



162
163
164
165
166
167
168
# File 'lib/jasmine/config.rb', line 162

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

#windows?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/jasmine/config.rb', line 42

def windows?
  require 'rbconfig'
  ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end