2011-05-02 20:35:35 +00:00
|
|
|
module JasmineMixin
|
|
|
|
JASMINE_PROGRAM = File.expand_path('../../../bin/jasmine-headless-webkit', __FILE__)
|
|
|
|
|
2011-05-06 21:07:44 +00:00
|
|
|
JAVASCRIPT_EXTENSIONS = %w{js coffee}
|
2011-05-02 21:53:57 +00:00
|
|
|
|
2011-05-02 20:35:35 +00:00
|
|
|
def self.included(klass)
|
|
|
|
klass::ALL_HOOKS << [ :run_jasmine, :ran_jasmine ]
|
|
|
|
end
|
|
|
|
|
2011-05-06 20:32:43 +00:00
|
|
|
attr_accessor :is_jasmine_running, :jasmine_to_run, :jasmine_ran_once
|
2011-05-02 20:35:35 +00:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
super()
|
|
|
|
setup_jasmine_project_mappings
|
2011-05-06 20:32:43 +00:00
|
|
|
jasmine_ran_once = false
|
2011-05-02 20:35:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_to_green
|
|
|
|
begin
|
|
|
|
reset_jasmine(:no)
|
|
|
|
super if find_files_to_test
|
|
|
|
|
|
|
|
reset_jasmine(:yes)
|
2011-05-06 20:32:43 +00:00
|
|
|
self.last_mtime = Time.at(0) if !options[:no_full_after_start] && !jasmine_ran_once
|
2011-05-02 20:35:35 +00:00
|
|
|
run_jasmine if find_files_to_test
|
|
|
|
|
|
|
|
self.is_jasmine_running = :all
|
|
|
|
wait_for_changes unless all_jasmine_good
|
|
|
|
end until all_jasmine_good
|
|
|
|
|
|
|
|
reset_jasmine(:all)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_jasmine(method)
|
|
|
|
self.files_to_test = new_hash_of_arrays
|
|
|
|
self.is_jasmine_running = method
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_jasmine
|
|
|
|
hook :run_jasmine
|
|
|
|
|
|
|
|
self.jasmine_to_run = :all
|
|
|
|
|
|
|
|
if mtime = find_files_to_test
|
|
|
|
self.last_mtime = mtime
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
system make_jasmine_cmd
|
|
|
|
|
|
|
|
self.jasmine_to_run = ($?.exitstatus == 0) ? :none : :all
|
|
|
|
end
|
|
|
|
|
|
|
|
hook :ran_jasmine
|
2011-05-06 20:32:43 +00:00
|
|
|
|
|
|
|
jasmine_ran_once = true
|
2011-05-02 20:35:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_jasmine_good
|
|
|
|
self.jasmine_to_run == :none
|
|
|
|
self.files_to_test = new_hash_of_arrays
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_files
|
|
|
|
Hash[super.find_all { |file, mtime|
|
2011-05-02 21:53:57 +00:00
|
|
|
is_js = (file[%r{\.(#{JAVASCRIPT_EXTENSIONS.join('|')})$}] != nil)
|
|
|
|
|
2011-05-02 20:35:35 +00:00
|
|
|
case self.is_jasmine_running
|
|
|
|
when :all
|
|
|
|
true
|
|
|
|
when :no
|
2011-05-02 21:53:57 +00:00
|
|
|
!is_js
|
2011-05-02 20:35:35 +00:00
|
|
|
when :yes
|
2011-05-02 21:53:57 +00:00
|
|
|
is_js
|
2011-05-02 20:35:35 +00:00
|
|
|
end
|
|
|
|
}]
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_jasmine_cmd
|
2011-05-03 13:55:42 +00:00
|
|
|
"#{JASMINE_PROGRAM}"
|
2011-05-02 20:35:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_jasmine_project_mappings
|
2011-05-06 21:07:44 +00:00
|
|
|
add_mapping(%r{spec/javascripts/.*_spec\.(js|coffee)}) { |filename, _|
|
2011-05-02 20:35:35 +00:00
|
|
|
filename
|
|
|
|
}
|
|
|
|
|
|
|
|
add_mapping(%r{public/javascripts/(.*)\.js}) { |_, m|
|
2011-05-07 13:16:23 +00:00
|
|
|
files_matching(%r{spec/javascripts/#{m[1]}_spec\..*$})
|
2011-05-06 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_mapping(%r{app/coffeescripts/(.*)\.coffee}) { |_, m|
|
2011-05-07 13:16:23 +00:00
|
|
|
files_matching(%r{spec/javascripts/#{m[1]}_spec\..*$})
|
2011-05-02 20:35:35 +00:00
|
|
|
}
|
|
|
|
end
|
2011-05-02 21:53:57 +00:00
|
|
|
|
|
|
|
def add_javascript_extensions(*extensions)
|
|
|
|
self.class::JAVASCRIPT_EXTENSIONS << extensions
|
|
|
|
end
|
2011-05-02 20:35:35 +00:00
|
|
|
end
|