This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
/
jasmine-rails.rb
160 lines (135 loc) · 4.81 KB
/
jasmine-rails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "jasmine_rails/engine"
module JasmineRails
DEFAULT_TMP_DIR = 'tmp/jasmine'
CONSOLE_REPORTERS = {'console' => ['jasmine-console-shims.js',
'jasmine-console-reporter.js']}
class << self
# return the relative path to access the spec runner
# for the host Rails application
# ex: /jasmine
def route_path
route = Rails.application.routes.named_routes[:jasmine_rails]
raise 'JasmineRails::Engine has not been mounted. Try adding `mount JasmineRails::Engine => "/specs" if defined?(JasmineRails)` to routes.rb' unless route
path = route.path
# Rails 3.1 support -- TODO: safe to delete for 3.2?
if path.is_a?(String)
path
else
path.spec.to_s
end
end
def spec_dir
paths = jasmine_config['spec_dir'] || 'spec/javascripts'
[paths].flatten.map { |path| Dir.glob path }.flatten.collect { |path| Rails.root.join(path) }
end
def include_dir
paths = jasmine_config['include_dir']
[paths].flatten.compact.collect { |path| Rails.root.join(path) }
end
def tmp_dir
path = jasmine_config['tmp_dir'] || JasmineRails::DEFAULT_TMP_DIR
Rails.root.join(path)
end
def reporter_files(types_string)
types = types_string.to_s.split(',')
reporters = jasmine_config['reporters'] || {}
reporters = reporters.merge(JasmineRails::CONSOLE_REPORTERS)
reporters.values_at(*types).compact.flatten
end
# returns list of all files to be included into the jasmine testsuite
# includes:
# * application src_files
# * spec helpers
# * spec_files
def spec_files
files = []
files += filter_files src_dir, jasmine_config['src_files']
spec_dir.each do |dir|
files += filter_files dir, jasmine_config['helpers']
files += filter_files dir, jasmine_config['spec_files']
end
# Sprockets 4 wants "logical paths" not to include file extensions
if defined?(Sprockets) && Sprockets::VERSION.to_f >= 4
files = files.map { |f| f.chomp(File.extname f) }
end
files
end
# return a list of any additional CSS files to be included into the jasmine testsuite
def css_files
files = []
files += filter_files css_dir, jasmine_config['css_files']
spec_dir.each do |dir|
files += filter_files dir, jasmine_config['css_files']
end
files
end
# iterate over all directories used as part of the testsuite (including subdirectories)
def each_spec_dir(&block)
spec_dir.each do |dir|
each_dir dir.to_s, &block
end
each_dir src_dir.to_s, &block
each_dir css_dir.to_s, &block
end
# clear out cached jasmine config file
# it would be nice to automatically flush when the jasmine.yml file changes instead
# of having this programatic API
def reload_jasmine_config
@config = nil
end
# force ssl when loading the test runner. Set to true if your app forces SSL
def force_ssl
jasmine_config['force_ssl'] || false
end
# use the phantom command from the phantom gem. Set to false if you want to manage your own phantom executable
def use_phantom_gem?
jasmine_config['use_phantom_gem'].nil? || jasmine_config['use_phantom_gem'] == true
end
# @return [Array<String>]
def phantom_options
jasmine_config['phantom_options'].to_s.split(/\s+/).map(&:strip)
end
private
def css_dir
path = jasmine_config['css_dir'] || 'app/assets/stylesheets'
Rails.root.join(path)
end
def src_dir
path = jasmine_config['src_dir'] || 'app/assets/javascripts'
Rails.root.join(path)
end
def jasmine_config
@config ||= begin
config_file_name = ENV['JASMINE_CONFIG'] || 'jasmine.yml'
path = Rails.root.join('config', config_file_name)
path = Rails.root.join('spec', 'javascripts', 'support', config_file_name) unless File.exist?(path)
initialize_jasmine_config_if_absent(path)
require 'yaml'
YAML.load_file(path)
end
end
def each_dir(root, &block)
yield root
Dir[root + '/*'].each do |file|
if File.directory?(file)
each_dir(file, &block)
end
end
end
def filter_files(root_dir, patterns)
files = patterns.to_a.collect do |pattern|
Dir.glob(root_dir.join(pattern)).sort
end
files = files.flatten
files = files.collect {|f| f.gsub(root_dir.to_s + '/', '') }
files || []
end
private
def initialize_jasmine_config_if_absent(path)
return if File.exist?(path)
Rails.logger.warn("Initializing jasmine.yml configuration")
FileUtils.mkdir_p(File.dirname(path))
FileUtils.cp(File.join(File.dirname(__FILE__), 'generators', 'jasmine_rails', 'templates', 'jasmine.yml'), path)
end
end
end