Skip to content

Commit 9a72fa7

Browse files
committed
Merge pull request testdouble#67 from wireframe/runner-refactor
extract JasmineRails::Runner to support alternative non-rake test runner...
2 parents 2d87e84 + 838ca69 commit 9a72fa7

File tree

3 files changed

+59
-43
lines changed

3 files changed

+59
-43
lines changed
File renamed without changes.

lib/jasmine_rails/runner.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'jasmine_rails/offline_asset_paths'
2+
3+
module JasmineRails
4+
module Runner
5+
class << self
6+
# Run the Jasmine testsuite via phantomjs CLI
7+
# raises an exception if any errors are encountered while running the testsuite
8+
def run(spec_filter = nil)
9+
override_rails_config do
10+
include_offline_asset_paths_helper
11+
html = get_spec_runner(spec_filter)
12+
runner_path = Rails.root.join('spec/tmp/runner.html')
13+
File.open(runner_path, 'w') {|f| f << html.gsub('/assets', './assets')}
14+
15+
phantomjs_runner_path = File.join(File.dirname(__FILE__), '..', 'assets', 'javascripts', 'jasmine-runner.js')
16+
run_cmd %{phantomjs "#{phantomjs_runner_path}" "file://#{runner_path.to_s}?spec=#{spec_filter}"}
17+
end
18+
end
19+
20+
private
21+
def include_offline_asset_paths_helper
22+
if Rails::VERSION::MAJOR >= 4
23+
Sprockets::Rails::Helper.send :include, JasmineRails::OfflineAssetPaths
24+
else
25+
ActionView::AssetPaths.send :include, JasmineRails::OfflineAssetPaths
26+
end
27+
end
28+
29+
def override_rails_config
30+
config = Rails.application.config
31+
32+
original_asssets_debug = config.assets.debug
33+
config.assets.debug = false
34+
yield
35+
config.assets.debug = original_asssets_debug
36+
end
37+
38+
def get_spec_runner(spec_filter)
39+
app = ActionController::Integration::Session.new(Rails.application)
40+
path = JasmineRails.route_path
41+
JasmineRails::OfflineAssetPaths.disabled = false
42+
app.get path, :console => 'true', :spec => spec_filter
43+
JasmineRails::OfflineAssetPaths.disabled = true
44+
raise "Jasmine runner at '#{path}' returned a #{app.response.status} error: #{app.response.message}" unless app.response.success?
45+
app.response.body
46+
end
47+
48+
def run_cmd(cmd)
49+
puts "Running `#{cmd}`"
50+
unless system(cmd)
51+
raise "Error executing command: #{cmd}"
52+
end
53+
end
54+
end
55+
end
56+
end

lib/tasks/jasmine-rails_tasks.rake

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@ namespace :spec do
22

33
desc "run test with phantomjs"
44
task :javascript => [:environment] do
5-
override_rails_config do
6-
include_offline_asset_paths_helper
7-
spec_filter = ENV['SPEC']
8-
html = get_spec_runner(spec_filter)
9-
runner_path = Rails.root.join('spec/tmp/runner.html')
10-
File.open(runner_path, 'w') {|f| f << html.gsub('/assets', './assets')}
11-
12-
run_cmd %{phantomjs "#{File.join(File.dirname(__FILE__), 'runner.js')}" "file://#{runner_path.to_s}?spec=#{spec_filter}"}
13-
end
14-
end
15-
16-
def include_offline_asset_paths_helper
17-
require 'jasmine_rails/offline_asset_paths'
18-
if Rails::VERSION::MAJOR >= 4
19-
Sprockets::Rails::Helper.send :include, JasmineRails::OfflineAssetPaths
20-
else
21-
ActionView::AssetPaths.send :include, JasmineRails::OfflineAssetPaths
22-
end
23-
end
24-
25-
def override_rails_config
26-
config = Rails.application.config
27-
28-
original_asssets_debug = config.assets.debug
29-
config.assets.debug = false
30-
yield
31-
config.assets.debug = original_asssets_debug
32-
end
33-
34-
def get_spec_runner(spec_filter)
35-
app = ActionController::Integration::Session.new(Rails.application)
36-
path = JasmineRails.route_path
37-
app.get path, :console => 'true', :spec => spec_filter
38-
JasmineRails::OfflineAssetPaths.disabled = true
39-
raise "Jasmine runner at '#{path}' returned a #{app.response.status} error: #{app.response.message}" unless app.response.success?
40-
app.response.body
41-
end
42-
43-
def run_cmd(cmd)
44-
puts "Running `#{cmd}`"
45-
unless system(cmd)
46-
raise "Error executing command: #{cmd}"
47-
end
5+
require 'jasmine_rails/runner'
6+
spec_filter = ENV['SPEC']
7+
JasmineRails::Runner.run spec_filter
488
end
499

5010
# alias

0 commit comments

Comments
 (0)