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