#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../../lib/masterview' ) require 'masterview/extras/watcher.rb' class TestTemplateWatcher < Test::Unit::TestCase MVTestDir = File.expand_path( File.join(File.dirname(__FILE__), '..' ) ) TestFixturesDir = "#{MVTestDir}/fixtures/templates" TemplateSrcDir = "#{MVTestDir}/tmp/templates_src" TemplateFilenamePattern = '*.html' TemplateDstDir = "#{MVTestDir}/tmp/views" DEBUG_TRACE = false if DEBUG_TRACE STDOUT.puts "\nTEST TEMPLATE CHANGE-DETECTION" STDOUT.puts "\nTest case root directories:" STDOUT.puts "...MvTestDir=#{MVTestDir}" STDOUT.puts "...TestFixturesDir=#{TestFixturesDir}" STDOUT.puts "...TemplateSrcDir=#{TemplateSrcDir}" STDOUT.puts "...TemplateDstDir=#{TemplateDstDir}" end # a template file that we'll monitor to verify change detection TestSubjectFileShortName = 'product.html' TestFixtureFile = "#{TestFixturesDir}/product.html" TestSubjectFile = "#{TemplateSrcDir}/#{TestSubjectFileShortName}" def setup unless File.exists?(TestSubjectFile) # isolate a copy of the observed template file in our templates source test dir # so we can bash its file timestamp to test changed-file detection FileUtils.mkdir_p TemplateSrcDir unless File.directory?(TemplateSrcDir) FileUtils.copy TestFixtureFile, TestSubjectFile, :preserve => false #WAS: true STDOUT.puts "\n========== test setup ==================" if DEBUG_TRACE STDOUT.puts "...installed test fixture #{TestFixtureFile}" if DEBUG_TRACE STDOUT.puts "...#{TestSubjectFileShortName} template timestamp=#{File.mtime(TestSubjectFile)}" if DEBUG_TRACE end @template_mio_tree = MasterView::FileMIOTree.new(TemplateSrcDir, '.html') @output_mio_tree = MasterView::FileMIOTree.new(TemplateDstDir, '.rhtml') end def test_a_parse_template_on_startup STDOUT.puts "\n========== test_a_parse_template_on_startup ==========" if DEBUG_TRACE STDOUT.puts "...#{TestSubjectFileShortName} template timestamp=#{File.mtime(TestSubjectFile)}" if DEBUG_TRACE STDOUT.puts "...clearing dir #{TemplateDstDir}" if DEBUG_TRACE && File.exists?(TemplateDstDir) FileUtils.remove_dir(TemplateDstDir, true) files_processed = MasterView::TemplateWatcher.check_updated(@template_mio_tree, TemplateFilenamePattern) do |mio| MasterView::Parser.parse_mio( mio, @output_mio_tree ) end assert_equal [ TestSubjectFileShortName ], files_processed assert File.exist?(TemplateDstDir+'/layouts/product.rhtml') assert File.exist?(TemplateDstDir+'/product/_form.rhtml') assert File.exist?(TemplateDstDir+'/product/edit.rhtml') assert File.exist?(TemplateDstDir+'/product/list.rhtml') assert File.exist?(TemplateDstDir+'/product/new.rhtml') assert File.exist?(TemplateDstDir+'/product/show.rhtml') STDOUT.puts "...files_processed [ #{files_processed.join(', ')} ]" if DEBUG_TRACE end def test_b_check_template_no_change STDOUT.puts "\n========== test_b_check_template_no_change =============" if DEBUG_TRACE STDOUT.puts "...#{TestSubjectFileShortName} template timestamp=#{File.mtime(TestSubjectFile)}" if DEBUG_TRACE assert_equal true, File.directory?(TemplateDstDir) files_processed = parse_changed_templates assert_equal [], files_processed STDOUT.puts "...files_processed [ #{files_processed.join(', ')} ]" if DEBUG_TRACE end def test_c_reparse_changed_template STDOUT.puts "\n========== test_c_reparse_changed_template =============" if DEBUG_TRACE STDOUT.puts "...#{TestSubjectFileShortName} template timestamp=#{File.mtime(TestSubjectFile)}" if DEBUG_TRACE # touch gets permission-denied error on Win32, so work around sleep 1 # make sure noticeable time passes if RUBY_PLATFORM =~ /mswin32/ # re-copy the test fixture to get a new timestamp STDOUT.puts "...forcing update of template #{TestSubjectFileShortName}" if DEBUG_TRACE #FileUtils.copy TestFixtureFile, TestSubjectFile, :preserve => false File.open( TestSubjectFile, 'a' ){ |file| file.write("\n ") } else FileUtils.touch TestSubjectFile end STDOUT.puts "...#{TestSubjectFileShortName} template timestamp=#{File.mtime(TestSubjectFile)}" if DEBUG_TRACE files_processed = parse_changed_templates assert_equal [ TestSubjectFileShortName ], files_processed STDOUT.puts "...files_processed [ #{files_processed.join(', ')} ]" if DEBUG_TRACE end def parse_changed_templates files_processed = MasterView::TemplateWatcher.check_updated(@template_mio_tree, TemplateFilenamePattern) do |mio| MasterView::Parser.parse_mio( mio, @output_mio_tree ) end files_processed end end