Skip to content

Commit

Permalink
[Sass] Dramatically speed up plugin tests.
Browse files Browse the repository at this point in the history
Remove the need to sleep by setting the mtimes of all files back a millisecond
before each test, so touch works as expected.
  • Loading branch information
nex3 committed Jan 24, 2010
1 parent 579a659 commit 91260f7
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions test/sass/plugin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def setup
FileUtils.mkdir tempfile_loc(nil,"more_")
set_plugin_opts
Sass::Plugin.update_stylesheets
reset_mtimes
end

def teardown
Expand All @@ -30,23 +31,21 @@ def teardown

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
Expand Down Expand Up @@ -108,7 +107,7 @@ def test_merb_update
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
Expand All @@ -129,9 +128,8 @@ def test_cached_dependencies_update
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
Expand Down Expand Up @@ -162,7 +160,7 @@ def assert_renders_correctly(*arguments)
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")
Expand All @@ -172,6 +170,24 @@ def assert_stylesheet_updated(name)
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"
Expand Down Expand Up @@ -209,11 +225,6 @@ def set_plugin_opts(overrides = {})
: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
Expand Down

0 comments on commit 91260f7

Please sign in to comment.