Created
February 22, 2011 11:30
-
-
Save stevebartholomew/838542 to your computer and use it in GitHub Desktop.
Quick & Dirty JS test runner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quick and dirty JS test runner | |
# | |
# Required files: | |
# {testdir} | |
# /index.html (rails specific sample below) | |
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | |
# "http://www.w3.org/TR/html4/loose.dtd"> | |
# <html> | |
# <head> | |
# <link rel="stylesheet" href="<%= JS_TEST_BASE %>/qunit.css" type="text/css" media="screen" /> | |
# | |
# <script src="<%= File.expand_path(File.join(JS_BASE, '..', '..')) %>/app/javascripts/application.js"></script> | |
# <% Dir[File.join(JS_BASE, '**', '*.js')].each do |script|%> | |
# <script src="<%= script %>"></script> | |
# <% end %> | |
# <script type="text/javascript" src="<%= JS_TEST_BASE %>/qunit.js"></script> | |
# | |
# | |
# <script> | |
# $(document).ready(function(){ | |
# <%= js_test %> | |
# }); | |
# </script> | |
# | |
# </head> | |
# <body> | |
# <h1 id="qunit-header">QUnit example</h1> | |
# <h2 id="qunit-banner"></h2> | |
# <div id="qunit-testrunner-toolbar"></div> | |
# <h2 id="qunit-userAgent"></h2> | |
# <ol id="qunit-tests"></ol> | |
# <div id="qunit-fixture"> | |
# <%= html_fixture %> | |
# </div> | |
# </body> | |
# </html> | |
# | |
# For tests: | |
# {testdir} | |
# /widgets/my_widget_test.js | |
# /widgets/my_widget_fixture.html | |
# | |
JS_TEST_BASE = File.expand_path("test/javascripts") | |
JS_BASE = File.expand_path("public/app") | |
namespace :test do | |
task :js do | |
files_to_run = [] | |
tests_to_run = [] | |
test_base = File.expand_path(JS_TEST_BASE) | |
if ENV['TEST'] | |
tests_to_run << File.expand_path(ENV['TEST']) | |
else | |
Dir["#{test_base}/**/*_test.js"].each do |full_path| | |
tests_to_run << full_path | |
end | |
end | |
timestamp = Time.now.strftime("%Y%m%d%H%M%s") | |
tests_to_run.each do |full_path| | |
file = File.basename(full_path) | |
test_name = file.match(/([a-z]+)\_test/)[0] | |
dir = full_path.gsub(/#{file}/, "") | |
relative_test_dir = full_path.gsub(/#{test_base}\//, "").gsub(/\/#{file}/, "") | |
html_fixture_path = File.join(dir, file.gsub(/_test\.js/, ".html")) | |
test_template = ERB.new(File.read("#{test_base}/index.html")) | |
if File.exists?(html_fixture_path) | |
html_fixture = ERB.new(File.read(html_fixture_path)).result(binding) | |
else | |
html_fixture = "" | |
end | |
js_test = File.read(full_path) | |
test_output = test_template.result(binding) | |
runner_dir = "#{test_base}/tmp/#{relative_test_dir}" | |
if !File.exists?(runner_dir) | |
FileUtils.mkdir_p(runner_dir) | |
end | |
runner_file = "#{test_base}/tmp/#{relative_test_dir}/#{test_name}.html" | |
File.open(runner_file, "w") do |file| | |
file.puts test_output | |
end | |
files_to_run << runner_file | |
end | |
puts "Running: #{files_to_run.join(', ')}" | |
system("open #{files_to_run.join(' ')}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment