From 91260f7dbc6c37d13c6e7ddd1f8cf54822770539 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sat, 23 Jan 2010 16:06:31 -0800 Subject: [PATCH] [Sass] Dramatically speed up plugin tests. Remove the need to sleep by setting the mtimes of all files back a millisecond before each test, so touch works as expected. --- test/sass/plugin_test.rb | 45 +++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb index c1553a8f..ca537874 100755 --- a/test/sass/plugin_test.rb +++ b/test/sass/plugin_test.rb @@ -14,6 +14,7 @@ class SassPluginTest < Test::Unit::TestCase FileUtils.mkdir tempfile_loc(nil,"more_") set_plugin_opts Sass::Plugin.update_stylesheets + reset_mtimes end def teardown @@ -30,23 +31,21 @@ class SassPluginTest < Test::Unit::TestCase def test_no_update File.delete(tempfile_loc('basic')) - assert Sass::Plugin.stylesheet_needs_update?('basic', template_loc, tempfile_loc) + assert_needs_update 'basic' Sass::Plugin.update_stylesheets assert_stylesheet_updated 'basic' end def test_update_needed_when_modified - sleep 1 - FileUtils.touch(template_loc('basic')) - assert Sass::Plugin.stylesheet_needs_update?('basic', template_loc, tempfile_loc) + touch 'basic' + assert_needs_update 'basic' Sass::Plugin.update_stylesheets assert_stylesheet_updated 'basic' end def test_update_needed_when_dependency_modified - sleep 1 - FileUtils.touch(template_loc('basic')) - assert Sass::Plugin.stylesheet_needs_update?('import', template_loc, tempfile_loc) + touch 'basic' + assert_needs_update 'import' Sass::Plugin.update_stylesheets assert_stylesheet_updated 'basic' end @@ -108,7 +107,7 @@ class SassPluginTest < Test::Unit::TestCase set_plugin_opts File.delete(tempfile_loc('basic')) - assert Sass::Plugin.stylesheet_needs_update?('basic', template_loc, tempfile_loc) + assert_needs_update 'basic' if defined?(MerbHandler) MerbHandler.new('.').process nil, nil @@ -129,9 +128,8 @@ class SassPluginTest < Test::Unit::TestCase FileUtils.mv(template_loc("basic"), template_loc("basic", "more_")) set_plugin_opts :load_paths => [result_loc, template_loc(nil, "more_")] - sleep 1 - FileUtils.touch(template_loc("basic", "more_")) - assert Sass::Plugin.stylesheet_needs_update?("import", template_loc, tempfile_loc) + touch 'basic', 'more_' + assert_needs_update "import" Sass::Plugin.update_stylesheets assert_renders_correctly("import") ensure @@ -162,7 +160,7 @@ class SassPluginTest < Test::Unit::TestCase end def assert_stylesheet_updated(name) - assert !Sass::Plugin.stylesheet_needs_update?(name, template_loc, tempfile_loc) + assert_doesnt_need_update name # Make sure it isn't an exception expected_lines = File.read(result_loc(name)).split("\n") @@ -172,6 +170,24 @@ class SassPluginTest < Test::Unit::TestCase end end + def assert_needs_update(name) + assert(Sass::Plugin.stylesheet_needs_update?(name, template_loc, tempfile_loc), + "Expected #{template_loc(name)} to need an update.") + end + + def assert_doesnt_need_update(name) + assert(!Sass::Plugin.stylesheet_needs_update?(name, template_loc, tempfile_loc), + "Expected #{template_loc(name)} not to need an update.") + end + + def touch(*args) + FileUtils.touch(template_loc(*args)) + end + + def reset_mtimes + Dir["{#{template_loc},#{tempfile_loc}}/**/*.{css,sass}"].each {|f| File.utime(Time.now, Time.now - 1, f)} + end + def template_loc(name = nil, prefix = nil) if name absolutize "#{prefix}templates/#{name}.sass" @@ -209,11 +225,6 @@ class SassPluginTest < Test::Unit::TestCase :always_update => true, }.merge(overrides) end - - def wait_a_tick - time = Time.now - loop {break if Time.now.sec != time.sec} - end end module Sass::Plugin