diff --git a/Rakefile b/Rakefile index 50da90e..399568c 100644 --- a/Rakefile +++ b/Rakefile @@ -5,6 +5,8 @@ PROTOTYPE_ROOT = File.expand_path(File.dirname(__FILE__)) PROTOTYPE_SRC_DIR = File.join(PROTOTYPE_ROOT, 'src') PROTOTYPE_DIST_DIR = File.join(PROTOTYPE_ROOT, 'dist') PROTOTYPE_PKG_DIR = File.join(PROTOTYPE_ROOT, 'pkg') +PROTOTYPE_TEST_DIR = File.join(PROTOTYPE_ROOT, 'test') +PROTOTYPE_TMP_DIR = File.join(PROTOTYPE_TEST_DIR, 'unit', 'tmp') PROTOTYPE_VERSION = '1.6.0.2' task :default => [:dist, :dist_helper, :package, :clean_package_source] @@ -50,23 +52,20 @@ task :test => [:dist, :test_units] require 'test/lib/jstest' desc "Runs all the JavaScript unit tests and collects the results" -JavaScriptTestTask.new(:test_units) do |t| +JavaScriptTestTask.new(:test_units => [:build_unit_tests]) do |t| testcases = ENV['TESTCASES'] tests_to_run = ENV['TESTS'] && ENV['TESTS'].split(',') browsers_to_test = ENV['BROWSERS'] && ENV['BROWSERS'].split(',') - tmp_dir = "test/unit/tmp" t.mount("/dist") t.mount("/test") - Dir.mkdir(tmp_dir) unless File.exist?(tmp_dir) + Dir.mkdir(PROTOTYPE_TMP_DIR) unless File.exist?(PROTOTYPE_TMP_DIR) - Dir["test/unit/*_test.js"].each do |file| - TestBuilder.new(file).render - test_file = File.basename(file, ".js") - test_name = test_file.sub("_test", "") + Dir["test/unit/tmp/*_test.html"].each do |file| + test_name = File.basename(file).sub("_test.html", "") unless tests_to_run && !tests_to_run.include?(test_name) - t.run("/#{tmp_dir}/#{test_file}.html", testcases) + t.run("/#{file}", testcases) end end @@ -74,3 +73,20 @@ JavaScriptTestTask.new(:test_units) do |t| t.browser(browser.to_sym) unless browsers_to_test && !browsers_to_test.include?(browser) end end + +task :build_unit_tests do + Dir[File.join('test', 'unit', '*_test.js')].each do |file| + PageBuilder.new(file, 'prototype.erb').render + end +end + +task :clean_package_source do + rm_rf File.join(PROTOTYPE_PKG_DIR, "prototype-#{PROTOTYPE_VERSION}") +end + +desc 'Generates an empty tmp directory for building tests.' +task :clean_tmp do + puts 'Generating an empty tmp directory for building tests.' + FileUtils.rm_rf(PROTOTYPE_TMP_DIR) if File.exist?(PROTOTYPE_TMP_DIR) + Dir.mkdir(PROTOTYPE_TMP_DIR) +end diff --git a/test/test.css b/test/lib/assets/test.css similarity index 100% rename from test/test.css rename to test/lib/assets/test.css diff --git a/test/lib/unittest.js b/test/lib/assets/unittest.js similarity index 100% rename from test/lib/unittest.js rename to test/lib/assets/unittest.js diff --git a/test/lib/jstest.rb b/test/lib/jstest.rb index 78fa737..6cfa52d 100644 --- a/test/lib/jstest.rb +++ b/test/lib/jstest.rb @@ -426,17 +426,17 @@ class TestSuiteResults end end -class TestBuilder - UNITTEST_DIR = File.expand_path('test') - FIXTURES_DIR = File.join(UNITTEST_DIR, 'unit', 'fixtures') - TMP_DIR = File.join(UNITTEST_DIR, 'unit', 'tmp') - TEMPLATE = File.join(UNITTEST_DIR, 'lib', 'template.erb') +class PageBuilder + UNITTEST_DIR = File.expand_path('test') + FIXTURES_DIR = File.join(UNITTEST_DIR, 'unit', 'fixtures') + TMP_DIR = File.join(UNITTEST_DIR, 'unit', 'tmp') + TEMPLATES_DIR = File.join(UNITTEST_DIR, 'lib', 'templates') - def initialize(filename, template = TEMPLATE) + def initialize(filename, template = 'default.erb') @filename = filename - @template = template + @template = File.join(self.class::TEMPLATES_DIR, template) @js_filename = File.basename(@filename) - @basename = @js_filename.sub("_test.js", "") + @basename = @js_filename.sub('_test.js', '') end def html_fixtures @@ -452,18 +452,25 @@ class TestBuilder end def render - @title = @basename.gsub("_", " ").strip.capitalize + @title = @basename.gsub('_', ' ').strip.capitalize @html_fixtures = html_fixtures - @js_fixtures_filename = external_fixtures("js") - @css_fixtures_filename = external_fixtures("css") + @js_fixtures_filename = external_fixtures('js') + @css_fixtures_filename = external_fixtures('css') - File.open(destination, "w+") do |file| - file << ERB.new(IO.read(@template), nil, "%").result(binding) + File.open(destination, 'w+') do |file| + file << ERB.new(IO.read(@template), nil, '%').result(binding) end end def destination - filename = File.basename(@filename, ".js") - File.join(TMP_DIR, "#{filename}.html") + name_file(:ext => 'html') + end + + def name_file(options = {}) + prefix = options[:prefix] ? "#{options[:prefix]}_" : "" + suffix = options[:suffix] ? "_#{options[:suffix]}" : "" + ext = options[:ext] ? options[:ext] : "js" + filename = File.basename(@filename, '.js') + File.join(TMP_DIR, "#{prefix}#{filename}#{suffix}.#{ext}") end end diff --git a/test/lib/templates/default.erb b/test/lib/templates/default.erb new file mode 100644 index 0000000..70d8fb0 --- /dev/null +++ b/test/lib/templates/default.erb @@ -0,0 +1,35 @@ + + +
+