2011-07-19 14:41:23 +00:00
|
|
|
require 'jasmine-core'
|
2011-09-02 15:31:58 +00:00
|
|
|
require 'time'
|
2011-09-12 19:12:58 +00:00
|
|
|
require 'multi_json'
|
2011-11-20 16:56:25 +00:00
|
|
|
require 'set'
|
2011-11-20 18:35:30 +00:00
|
|
|
require 'sprockets'
|
2011-11-21 15:32:49 +00:00
|
|
|
require 'sprockets/engines'
|
2011-06-10 14:04:21 +00:00
|
|
|
|
2011-11-16 20:28:02 +00:00
|
|
|
module Jasmine::Headless
|
2011-06-10 14:04:21 +00:00
|
|
|
class FilesList
|
2011-12-06 14:31:23 +00:00
|
|
|
include FileChecker
|
|
|
|
|
2011-10-26 12:45:23 +00:00
|
|
|
class << self
|
2011-12-08 14:13:45 +00:00
|
|
|
def asset_paths
|
|
|
|
return @asset_paths if @asset_paths
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-10-26 12:45:23 +00:00
|
|
|
require 'rubygems'
|
|
|
|
|
2011-11-25 21:18:06 +00:00
|
|
|
raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:each)
|
2011-10-26 12:45:23 +00:00
|
|
|
|
2011-12-08 14:13:45 +00:00
|
|
|
@asset_paths = []
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-12-08 14:13:45 +00:00
|
|
|
Gem::Specification.each { |gemspec| @asset_paths += get_paths_from_gemspec(gemspec) }
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-12-08 14:13:45 +00:00
|
|
|
@asset_paths
|
|
|
|
end
|
2011-11-25 21:18:06 +00:00
|
|
|
|
2011-12-08 14:13:45 +00:00
|
|
|
def get_paths_from_gemspec(gemspec)
|
|
|
|
%w{vendor lib app}.collect do |dir|
|
|
|
|
path = File.join(gemspec.gem_dir, dir, "assets/javascripts")
|
|
|
|
|
|
|
|
if File.directory?(path) && !@asset_paths.include?(path)
|
|
|
|
path
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end.compact
|
2011-10-26 12:45:23 +00:00
|
|
|
end
|
2011-11-18 16:50:48 +00:00
|
|
|
|
|
|
|
def reset!
|
2011-12-08 14:13:45 +00:00
|
|
|
@asset_paths = nil
|
2011-11-21 15:32:49 +00:00
|
|
|
|
2011-12-24 14:53:52 +00:00
|
|
|
# register haml-sprockets and handlebars_assets if it's available...
|
|
|
|
%w{haml-sprockets handlebars_assets}.each do |library|
|
2011-11-21 15:32:49 +00:00
|
|
|
begin
|
|
|
|
require library
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-29 14:32:27 +00:00
|
|
|
if ENV['JHW_ENV']
|
|
|
|
begin
|
|
|
|
require 'bundler'
|
2011-12-24 14:53:52 +00:00
|
|
|
|
2011-12-29 14:32:27 +00:00
|
|
|
Bundler.require(ENV['JHW_ENV'].to_sym)
|
|
|
|
rescue LoadError
|
2011-12-24 14:53:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
# ...and unregister ones we don't want/need
|
|
|
|
Sprockets.instance_eval do
|
2011-12-06 14:31:23 +00:00
|
|
|
EXCLUDED_FORMATS.each do |extension|
|
2011-12-05 23:49:52 +00:00
|
|
|
register_engine ".#{extension}", Jasmine::Headless::NilTemplate
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
register_engine '.coffee', Jasmine::Headless::CoffeeTemplate
|
2011-11-22 15:36:42 +00:00
|
|
|
register_engine '.js', Jasmine::Headless::JSTemplate
|
|
|
|
register_engine '.css', Jasmine::Headless::CSSTemplate
|
2011-11-22 17:14:00 +00:00
|
|
|
register_engine '.jst', Jasmine::Headless::JSTTemplate
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
2011-11-18 16:50:48 +00:00
|
|
|
end
|
2011-10-26 12:45:23 +00:00
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def default_files
|
2011-12-12 17:22:32 +00:00
|
|
|
%w{jasmine.js jasmine-html jasmine.css jasmine-extensions
|
|
|
|
intense headless_reporter_result jasmine.HeadlessReporter
|
2011-12-29 23:37:23 +00:00
|
|
|
jsDump beautify-html}
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
2011-12-06 14:31:23 +00:00
|
|
|
|
|
|
|
def extension_filter
|
|
|
|
extensions = (%w{.js .css} + Sprockets.engine_extensions)
|
|
|
|
|
|
|
|
%r{(#{extensions.join('|')})$}
|
|
|
|
end
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
2011-06-10 14:04:21 +00:00
|
|
|
|
2011-09-02 15:31:58 +00:00
|
|
|
PLEASE_WAIT_IM_WORKING_TIME = 2
|
|
|
|
|
2011-12-29 23:37:23 +00:00
|
|
|
attr_reader :options, :required_files, :potential_files_to_filter
|
2011-11-21 15:32:49 +00:00
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def initialize(options = {})
|
|
|
|
@options = options
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-12-30 16:34:30 +00:00
|
|
|
Kernel.srand(options[:seed]) if options[:seed]
|
2011-11-28 16:47:05 +00:00
|
|
|
|
2011-11-25 21:18:06 +00:00
|
|
|
@required_files = UniqueAssetList.new
|
2011-11-21 15:32:49 +00:00
|
|
|
@potential_files_to_filter = []
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-12-30 16:34:30 +00:00
|
|
|
load_initial_assets
|
|
|
|
|
|
|
|
use_config if config?
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_initial_assets
|
2011-11-21 15:32:49 +00:00
|
|
|
self.class.default_files.each do |file|
|
2011-12-01 15:37:35 +00:00
|
|
|
begin
|
2011-12-30 16:34:30 +00:00
|
|
|
add_path(file)
|
2011-12-01 15:37:35 +00:00
|
|
|
rescue InvalidUniqueAsset => e
|
|
|
|
raise StandardError.new("Not an asset: #{file}")
|
|
|
|
end
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-12-30 16:34:30 +00:00
|
|
|
(options[:reporters] || []).each do |reporter, identifier, file|
|
|
|
|
add_path("jasmine.HeadlessReporter.#{reporter}")
|
|
|
|
end
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
def files
|
2011-11-25 21:18:06 +00:00
|
|
|
required_files.flatten.collect { |asset| asset.pathname.to_s }.uniq
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def spec_files
|
2011-11-22 15:36:42 +00:00
|
|
|
filter_for_requested_specs(
|
|
|
|
files.find_all { |file| spec_dir.any? { |dir| file[dir] } }
|
|
|
|
)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def filtered_files
|
|
|
|
filter_for_requested_specs(files)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def search_paths
|
2011-11-20 00:15:38 +00:00
|
|
|
return @search_paths if @search_paths
|
|
|
|
|
2011-12-01 15:37:35 +00:00
|
|
|
@search_paths = [ Jasmine::Core.path, Jasmine::Headless.root.join('vendor/assets/javascripts').to_s ]
|
2011-12-08 14:13:45 +00:00
|
|
|
@search_paths += self.class.asset_paths
|
2011-11-21 15:32:49 +00:00
|
|
|
@search_paths += src_dir.collect { |dir| File.expand_path(dir) }
|
2011-12-01 21:52:01 +00:00
|
|
|
@search_paths += asset_paths.collect { |dir| File.expand_path(dir) }
|
2011-11-21 15:32:49 +00:00
|
|
|
@search_paths += spec_dir.collect { |dir| File.expand_path(dir) }
|
2011-11-20 00:15:38 +00:00
|
|
|
|
|
|
|
@search_paths
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-11-22 15:36:42 +00:00
|
|
|
def sprockets_environment
|
|
|
|
return @sprockets_environment if @sprockets_environment
|
|
|
|
|
|
|
|
@sprockets_environment = Sprockets::Environment.new
|
2011-11-25 21:18:06 +00:00
|
|
|
search_paths.each { |path| @sprockets_environment.append_path(path) }
|
2011-11-22 15:36:42 +00:00
|
|
|
|
2011-11-22 17:14:00 +00:00
|
|
|
@sprockets_environment.unregister_postprocessor('application/javascript', Sprockets::SafetyColons)
|
2011-11-22 15:36:42 +00:00
|
|
|
@sprockets_environment
|
|
|
|
end
|
|
|
|
|
2011-06-23 13:29:07 +00:00
|
|
|
def has_spec_outside_scope?
|
2011-11-21 15:32:49 +00:00
|
|
|
if is_outside_scope = !spec_filter.empty?
|
|
|
|
is_outside_scope = spec_dir.any? do |dir|
|
|
|
|
spec_file_searches.any? do |search|
|
2011-12-02 23:37:14 +00:00
|
|
|
!spec_files.any? do |file|
|
|
|
|
target = File.join(dir, search)
|
2011-12-02 23:47:17 +00:00
|
|
|
File.fnmatch?(target, file) || File.fnmatch?(target.gsub(%{^**/}, ''), file)
|
2011-12-02 23:37:14 +00:00
|
|
|
end
|
2011-11-21 15:32:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-22 15:36:42 +00:00
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
is_outside_scope
|
2011-06-23 13:29:07 +00:00
|
|
|
end
|
|
|
|
|
2011-06-10 15:02:26 +00:00
|
|
|
def filtered?
|
|
|
|
files != filtered_files
|
|
|
|
end
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def files_to_html
|
2011-06-10 15:02:26 +00:00
|
|
|
to_html(files)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_files_to_html
|
|
|
|
to_html(filtered_files)
|
|
|
|
end
|
|
|
|
|
2011-07-13 18:42:47 +00:00
|
|
|
def spec_file_line_numbers
|
2011-11-21 15:32:49 +00:00
|
|
|
@spec_file_line_numbers ||= Hash[spec_files.collect { |file|
|
2011-11-17 21:18:25 +00:00
|
|
|
if File.exist?(file)
|
2011-09-01 14:39:29 +00:00
|
|
|
if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty?
|
2011-07-13 18:42:47 +00:00
|
|
|
[ file, lines ]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
}.compact]
|
|
|
|
end
|
|
|
|
|
2011-06-10 15:02:26 +00:00
|
|
|
private
|
|
|
|
def to_html(files)
|
2011-09-02 15:31:58 +00:00
|
|
|
alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
files.collect do |file|
|
2011-09-02 15:31:58 +00:00
|
|
|
if alert_time && alert_time < Time.now
|
|
|
|
puts "Rebuilding cache, please wait..."
|
|
|
|
alert_time = nil
|
|
|
|
end
|
|
|
|
|
2011-11-22 17:14:00 +00:00
|
|
|
sprockets_environment.find_asset(file, :bundle => false).body
|
2011-11-25 21:18:06 +00:00
|
|
|
end.compact.reject(&:empty?)
|
2011-06-10 18:18:35 +00:00
|
|
|
end
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def spec_filter
|
2011-11-25 21:18:06 +00:00
|
|
|
@spec_filter ||= (@options[:only] && @options[:only].collect { |path| expanded_dir(path) }.flatten) || []
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
SEARCH_ROOTS = {
|
|
|
|
'src_files' => 'src_dir',
|
|
|
|
'stylesheets' => 'src_dir',
|
|
|
|
'helpers' => 'spec_dir',
|
|
|
|
'spec_files' => 'spec_dir'
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:34:30 +00:00
|
|
|
def use_config
|
2011-11-17 21:18:25 +00:00
|
|
|
@config = @options[:config].dup
|
2011-11-21 15:32:49 +00:00
|
|
|
@searches = {}
|
|
|
|
@potential_files_to_filter = []
|
2011-11-17 21:18:25 +00:00
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
%w{src_files stylesheets helpers spec_files}.each do |type|
|
|
|
|
if data = @config[type]
|
2011-11-25 21:18:06 +00:00
|
|
|
add_files(@searches[type] = data.flatten, type, send(SEARCH_ROOTS[type]))
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def add_files(patterns, type, dirs)
|
2011-11-25 21:18:06 +00:00
|
|
|
dirs.product(patterns).each do |search|
|
2011-12-01 13:59:05 +00:00
|
|
|
files = expanded_dir(File.join(*search))
|
2011-11-25 23:11:21 +00:00
|
|
|
|
2011-11-29 19:48:55 +00:00
|
|
|
files.sort! { |a, b| Kernel.rand(3) - 1 } if type == 'spec_files'
|
2011-11-25 23:11:21 +00:00
|
|
|
|
|
|
|
files.each do |path|
|
2011-12-01 13:59:05 +00:00
|
|
|
add_path(path, type)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-16 20:28:02 +00:00
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
if type == 'spec_files'
|
2011-11-22 15:36:42 +00:00
|
|
|
spec_filter.each { |path| add_path(path, type) }
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def config?
|
|
|
|
@options[:config]
|
|
|
|
end
|
2011-10-17 13:25:11 +00:00
|
|
|
|
|
|
|
def expanded_dir(path)
|
2011-12-06 14:31:23 +00:00
|
|
|
Dir[path].find_all { |file|
|
|
|
|
file[extension_filter] && !alert_if_bad_format?(file)
|
|
|
|
}.collect {
|
|
|
|
|file| File.expand_path(file)
|
|
|
|
}.find_all {
|
|
|
|
|path| File.file?(path)
|
|
|
|
}
|
2011-10-17 13:25:11 +00:00
|
|
|
end
|
2011-11-17 21:18:25 +00:00
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def extension_filter
|
2011-12-06 14:31:23 +00:00
|
|
|
self.class.extension_filter
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
2011-12-30 16:34:30 +00:00
|
|
|
def add_path(path, type = nil)
|
2011-11-22 15:51:46 +00:00
|
|
|
asset = sprockets_environment.find_asset(path)
|
2011-11-22 15:36:42 +00:00
|
|
|
|
|
|
|
@required_files << asset
|
|
|
|
|
|
|
|
if type == 'spec_files'
|
|
|
|
@potential_files_to_filter << path
|
|
|
|
end
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def src_dir
|
2011-11-29 14:45:23 +00:00
|
|
|
@src_dir ||= config_dir_or_pwd('src_dir')
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def spec_dir
|
2011-11-29 14:45:23 +00:00
|
|
|
@spec_dir ||= config_dir_or_pwd('spec_dir')
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-12-01 21:52:01 +00:00
|
|
|
def asset_paths
|
|
|
|
@asset_paths ||= config_dir_or_pwd('asset_paths')
|
|
|
|
end
|
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
def spec_file_searches
|
|
|
|
@searches['spec_files']
|
|
|
|
end
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def config_dir_or_pwd(dir)
|
2011-11-25 21:18:06 +00:00
|
|
|
found_dir = (@options[:config] && @options[:config][dir]) || Dir.pwd
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-11-21 15:32:49 +00:00
|
|
|
[ found_dir ].flatten.collect { |dir| File.expand_path(dir) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_for_requested_specs(files)
|
|
|
|
files.find_all do |file|
|
|
|
|
if potential_files_to_filter.include?(file)
|
|
|
|
spec_filter.empty? || spec_filter.any? { |pattern| File.fnmatch?(pattern, file) }
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
end
|