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-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-10-26 12:45:23 +00:00
|
|
|
class << self
|
2011-11-18 03:16:04 +00:00
|
|
|
def vendor_asset_paths
|
|
|
|
return @vendor_asset_paths if @vendor_asset_paths
|
|
|
|
|
2011-10-26 12:45:23 +00:00
|
|
|
require 'rubygems'
|
|
|
|
|
|
|
|
raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:map)
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
@vendor_asset_paths = []
|
|
|
|
|
|
|
|
Gem::Specification.map { |spec|
|
|
|
|
path = File.join(spec.gem_dir, 'vendor/assets/javascripts')
|
|
|
|
|
|
|
|
File.directory?(path) ? path : nil
|
|
|
|
}.compact
|
2011-10-26 12:45:23 +00:00
|
|
|
end
|
2011-11-18 16:50:48 +00:00
|
|
|
|
|
|
|
def reset!
|
|
|
|
@vendor_asset_paths = nil
|
|
|
|
end
|
2011-10-26 12:45:23 +00:00
|
|
|
end
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
DEFAULT_FILES = %w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html}
|
2011-06-10 14:04:21 +00:00
|
|
|
|
2011-09-02 15:31:58 +00:00
|
|
|
PLEASE_WAIT_IM_WORKING_TIME = 2
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def initialize(options = {})
|
|
|
|
@options = options
|
2011-11-18 03:16:04 +00:00
|
|
|
|
|
|
|
@files = []
|
|
|
|
@filtered_files = []
|
|
|
|
|
2011-11-18 16:50:48 +00:00
|
|
|
DEFAULT_FILES.each { |file| add_dependency('require', file, nil) }
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-06-23 13:29:07 +00:00
|
|
|
@spec_outside_scope = false
|
2011-11-18 03:16:04 +00:00
|
|
|
@spec_files = []
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
use_config! if config?
|
|
|
|
end
|
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
def files
|
|
|
|
@files.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_files
|
|
|
|
@filtered_files.to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def spec_files
|
|
|
|
@spec_files.to_a
|
|
|
|
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
|
|
|
|
|
|
|
|
@search_paths = [ Jasmine::Core.path ]
|
|
|
|
@search_paths += [ src_dir ].flatten.collect { |dir| File.expand_path(dir) }
|
|
|
|
@search_paths << File.expand_path(spec_dir)
|
|
|
|
@search_paths += self.class.vendor_asset_paths
|
|
|
|
|
|
|
|
@search_paths
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-06-23 13:29:07 +00:00
|
|
|
def has_spec_outside_scope?
|
|
|
|
@spec_outside_scope
|
|
|
|
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
|
|
|
|
@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-11-18 03:16:04 +00:00
|
|
|
def add_dependencies(file, source_root)
|
2011-11-18 16:50:48 +00:00
|
|
|
TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) }
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
2011-11-19 17:45:40 +00:00
|
|
|
EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$}
|
2011-11-18 21:00:24 +00:00
|
|
|
|
2011-11-18 16:50:48 +00:00
|
|
|
def add_dependency(type, file, source_root)
|
|
|
|
case type
|
|
|
|
when 'require'
|
|
|
|
if result = find_dependency(file)
|
|
|
|
add_file(*result)
|
|
|
|
end
|
|
|
|
when 'require_tree'
|
2011-11-18 21:00:24 +00:00
|
|
|
Dir[File.join(source_root, file, '**/*')].find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }.sort.each do |tree_path|
|
2011-11-18 16:50:48 +00:00
|
|
|
if result = find_dependency(tree_path.gsub(%r{^#{source_root}/}, ''))
|
|
|
|
add_file(*result)
|
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_dependency(file)
|
|
|
|
search_paths.each do |dir|
|
2011-11-20 00:15:38 +00:00
|
|
|
Dir[File.join(dir, "#{file}*")].find_all { |path| File.file?(path) }.each do |path|
|
|
|
|
root = path.gsub(%r{^#{dir}/}, '')
|
|
|
|
|
|
|
|
ok = (root == file)
|
|
|
|
ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] }
|
|
|
|
|
|
|
|
return [ File.expand_path(path), File.expand_path(dir) ] if ok
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
|
|
|
|
false
|
2011-11-17 21:18:25 +00:00
|
|
|
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-06-10 14:04:21 +00:00
|
|
|
files.collect { |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-18 21:00:24 +00:00
|
|
|
search_paths.collect do |path|
|
|
|
|
if file[path]
|
|
|
|
Jasmine::Headless::TestFile.new(file, path).to_html
|
|
|
|
end
|
|
|
|
end.compact.first
|
2011-09-02 15:31:58 +00:00
|
|
|
}.flatten.compact.reject(&:empty?)
|
2011-06-10 18:18:35 +00:00
|
|
|
end
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def spec_filter
|
2011-10-17 13:25:11 +00:00
|
|
|
return @spec_filter if @spec_filter
|
|
|
|
|
|
|
|
@spec_filter = begin
|
|
|
|
if @options[:only]
|
|
|
|
@options[:only].collect { |path| expanded_dir(path) }.flatten
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
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-06-10 14:04:21 +00:00
|
|
|
def use_config!
|
|
|
|
@filtered_files = @files.dup
|
|
|
|
|
2011-11-17 21:18:25 +00:00
|
|
|
@config = @options[:config].dup
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
%w{src_files stylesheets helpers spec_files}.each do |searches|
|
2011-11-17 21:18:25 +00:00
|
|
|
if data = @config[searches]
|
2011-11-18 03:16:04 +00:00
|
|
|
add_files(data.flatten, searches)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_files(searches, type)
|
|
|
|
searches.each do |search|
|
2011-11-20 00:15:38 +00:00
|
|
|
[ @config[SEARCH_ROOTS[type]] || Dir.pwd ].flatten.each do |dir|
|
|
|
|
dir = File.expand_path(dir)
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-11-20 00:15:38 +00:00
|
|
|
path = File.expand_path(File.join(dir, search))
|
2011-11-18 03:16:04 +00:00
|
|
|
|
2011-11-20 00:15:38 +00:00
|
|
|
found_files = expanded_dir(path) - files
|
2011-11-17 21:18:25 +00:00
|
|
|
|
2011-11-20 00:15:38 +00:00
|
|
|
found_files.each do |file|
|
|
|
|
type == 'spec_files' ? add_spec_file(file) : add_file(file, dir)
|
|
|
|
end
|
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'
|
|
|
|
spec_filter.each do |file|
|
|
|
|
@spec_outside_scope ||= add_spec_file(file)
|
|
|
|
end
|
|
|
|
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-11-18 21:00:24 +00:00
|
|
|
Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[EXTENSION_FILTER] }
|
2011-10-17 13:25:11 +00:00
|
|
|
end
|
2011-11-17 21:18:25 +00:00
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def add_file(file, source_root)
|
|
|
|
add_dependencies(file, source_root)
|
2011-11-17 21:18:25 +00:00
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
@files << file if !@files.include?(file)
|
|
|
|
@filtered_files << file if !@filtered_files.include?(file)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_spec_file(file)
|
2011-11-18 03:16:04 +00:00
|
|
|
add_dependencies(file, spec_dir)
|
2011-11-17 21:18:25 +00:00
|
|
|
|
|
|
|
if !@files.include?(file)
|
2011-11-18 03:16:04 +00:00
|
|
|
@files << file if !@files.include?(file)
|
2011-11-17 21:18:25 +00:00
|
|
|
|
|
|
|
if include_spec_file?(file)
|
2011-11-18 03:16:04 +00:00
|
|
|
@filtered_files << file if !@filtered_files.include?(file)
|
|
|
|
@spec_files << file if !@spec_files.include?(file) && spec_filter.empty? || spec_filter.include?(file)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_spec_file?(file)
|
|
|
|
spec_filter.empty? || spec_filter.include?(file)
|
|
|
|
end
|
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def src_dir
|
|
|
|
config_dir_or_pwd('src_dir')
|
|
|
|
end
|
|
|
|
|
|
|
|
def spec_dir
|
|
|
|
config_dir_or_pwd('spec_dir')
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_dir_or_pwd(dir)
|
|
|
|
found_dir = Dir.pwd
|
|
|
|
|
|
|
|
if @options[:config]
|
|
|
|
found_dir = @options[:config][dir] || found_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
found_dir
|
|
|
|
end
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|