2006-11-28 20:33:22 +00:00
#!/usr/bin/env ruby
2008-06-08 09:54:36 -07:00
require File . dirname ( __FILE__ ) + '/../test_helper'
2006-12-04 02:47:37 +00:00
require 'sass/plugin'
2008-06-08 09:54:36 -07:00
require 'fileutils'
2006-11-28 20:33:22 +00:00
class SassPluginTest < Test :: Unit :: TestCase
2007-05-03 09:04:20 +00:00
@@templates = %w{
2008-10-12 19:03:06 -07:00
complex script parent_ref import alt
2007-05-03 09:04:20 +00:00
subdir / subdir subdir / nested_subdir / nested_subdir
}
2006-12-04 02:47:37 +00:00
2006-11-28 20:33:22 +00:00
def setup
2008-07-14 11:32:08 -07:00
FileUtils . mkdir tempfile_loc
FileUtils . mkdir tempfile_loc ( nil , " more_ " )
2007-11-17 19:55:26 +00:00
set_plugin_opts
2006-12-04 02:47:37 +00:00
Sass :: Plugin . update_stylesheets
2006-11-28 20:33:22 +00:00
end
2007-08-28 05:33:57 +00:00
2006-12-03 23:17:45 +00:00
def teardown
2009-04-15 19:35:13 -07:00
clean_up_sassc
2008-07-14 11:32:08 -07:00
FileUtils . rm_r tempfile_loc
FileUtils . rm_r tempfile_loc ( nil , " more_ " )
2006-12-03 23:17:45 +00:00
end
2006-11-28 20:39:33 +00:00
2006-12-04 02:47:37 +00:00
def test_templates_should_render_correctly
@@templates . each { | name | assert_renders_correctly ( name ) }
2006-11-28 20:39:33 +00:00
end
2007-08-28 05:33:57 +00:00
2006-11-28 20:39:33 +00:00
def test_no_update
2006-12-04 02:47:37 +00:00
File . delete ( tempfile_loc ( 'basic' ) )
2008-07-14 11:32:08 -07:00
assert Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2006-12-04 02:47:37 +00:00
Sass :: Plugin . update_stylesheets
2008-07-14 11:32:08 -07:00
assert ! Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2006-12-04 02:47:37 +00:00
end
2007-08-28 05:33:57 +00:00
2007-09-02 01:48:25 +00:00
def test_update_needed_when_modified
sleep ( 1 )
FileUtils . touch ( template_loc ( 'basic' ) )
2008-07-14 11:32:08 -07:00
assert Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2007-09-02 01:48:25 +00:00
Sass :: Plugin . update_stylesheets
2008-07-14 11:32:08 -07:00
assert ! Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2007-09-02 01:48:25 +00:00
end
def test_update_needed_when_dependency_modified
sleep ( 1 )
FileUtils . touch ( template_loc ( 'basic' ) )
2008-07-14 11:32:08 -07:00
assert Sass :: Plugin . stylesheet_needs_update? ( 'import' , template_loc , tempfile_loc )
2007-09-02 01:48:25 +00:00
Sass :: Plugin . update_stylesheets
2008-07-14 11:32:08 -07:00
assert ! Sass :: Plugin . stylesheet_needs_update? ( 'import' , template_loc , tempfile_loc )
2007-09-02 01:48:25 +00:00
end
2007-11-17 19:55:26 +00:00
def test_full_exception_handling
2007-02-06 05:24:13 +00:00
File . delete ( tempfile_loc ( 'bork' ) )
Sass :: Plugin . update_stylesheets
2006-12-22 06:59:38 +00:00
File . open ( tempfile_loc ( 'bork' ) ) do | file |
2008-10-12 19:03:06 -07:00
assert_equal ( " /* \n Sass::SyntaxError: Undefined variable: \" !bork \" . \n on line 2 of #{ template_loc ( 'bork' ) } \n \n 1: bork \n 2: :bork= !bork " , file . read . split ( " \n " ) [ 0 ... 6 ] . join ( " \n " ) )
2006-12-22 06:59:38 +00:00
end
File . delete ( tempfile_loc ( 'bork' ) )
2006-12-04 02:47:37 +00:00
end
2007-02-06 05:24:13 +00:00
2007-11-17 19:55:26 +00:00
def test_nonfull_exception_handling
Sass :: Plugin . options [ :full_exception ] = false
2007-02-06 05:24:13 +00:00
File . delete ( tempfile_loc ( 'bork' ) )
Sass :: Plugin . update_stylesheets
assert_equal ( " /* Internal stylesheet error */ " , File . read ( tempfile_loc ( 'bork' ) ) )
File . delete ( tempfile_loc ( 'bork' ) )
2007-11-17 19:55:26 +00:00
Sass :: Plugin . options [ :full_exception ] = true
2007-02-06 05:24:13 +00:00
end
2008-06-19 16:10:50 -07:00
def test_two_template_directories
2008-07-14 11:32:08 -07:00
set_plugin_opts :template_location = > {
template_loc = > tempfile_loc ,
template_loc ( nil , 'more_' ) = > tempfile_loc ( nil , 'more_' )
}
2008-06-19 16:10:50 -07:00
Sass :: Plugin . update_stylesheets
2008-08-09 09:43:15 -07:00
[ 'more1' , 'more_import' ] . each { | name | assert_renders_correctly ( name , :prefix = > 'more_' ) }
2008-06-19 16:10:50 -07:00
end
2007-08-28 05:33:57 +00:00
2008-08-09 09:44:40 -07:00
def test_two_template_directories_with_line_annotations
set_plugin_opts :line_comments = > true ,
:style = > :nested ,
:template_location = > {
template_loc = > tempfile_loc ,
template_loc ( nil , 'more_' ) = > tempfile_loc ( nil , 'more_' )
}
Sass :: Plugin . update_stylesheets
assert_renders_correctly ( 'more1_with_line_comments' , 'more1' , :prefix = > 'more_' )
end
2007-11-17 19:55:26 +00:00
def test_rails_update
2006-12-04 02:47:37 +00:00
File . delete ( tempfile_loc ( 'basic' ) )
2008-07-14 11:32:08 -07:00
assert Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2007-08-28 05:33:57 +00:00
2006-12-04 02:47:37 +00:00
ActionController :: Base . new . process
2007-08-28 05:33:57 +00:00
2008-07-14 11:32:08 -07:00
assert ! Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2006-11-28 20:39:33 +00:00
end
2007-11-17 19:55:26 +00:00
def test_merb_update
begin
require 'merb'
rescue LoadError
puts " \n merb couldn't be loaded, skipping a test "
return
end
require 'sass/plugin/merb'
2009-01-09 10:58:30 +01:00
if defined? ( MerbHandler )
MerbHandler . send ( :define_method , :process_without_sass ) { | * args | }
else
Merb :: Rack :: Application . send ( :define_method , :call_without_sass ) { | * args | }
end
2007-11-17 19:55:26 +00:00
set_plugin_opts
File . delete ( tempfile_loc ( 'basic' ) )
2009-01-09 15:29:36 -08:00
assert Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2007-11-17 19:55:26 +00:00
2009-01-09 10:58:30 +01:00
if defined? ( MerbHandler )
MerbHandler . new ( '.' ) . process nil , nil
else
Merb :: Rack :: Application . new . call ( :: Rack :: MockRequest . env_for ( '/' ) )
end
2007-11-17 19:55:26 +00:00
2009-01-09 15:29:36 -08:00
assert ! Sass :: Plugin . stylesheet_needs_update? ( 'basic' , template_loc , tempfile_loc )
2007-11-17 19:55:26 +00:00
end
2007-08-28 05:33:57 +00:00
def test_doesnt_render_partials
assert ! File . exists? ( tempfile_loc ( '_partial' ) )
end
2006-11-28 20:39:33 +00:00
private
2007-08-28 05:33:57 +00:00
2008-08-09 09:43:15 -07:00
def assert_renders_correctly ( * arguments )
options = arguments . last . is_a? ( Hash ) ? arguments . pop : { }
prefix = options [ :prefix ]
result_name = arguments . shift
tempfile_name = arguments . shift || result_name
expected_lines = File . read ( result_loc ( result_name , prefix ) ) . split ( " \n " )
actual_lines = File . read ( tempfile_loc ( tempfile_name , prefix ) ) . split ( " \n " )
2009-04-21 14:12:43 -07:00
if actual_lines . first == " /* " && expected_lines . first != " /* "
assert ( false , actual_lines [ 0 .. actual_lines . enum_with_index . find { | l , i | l == " */ " } . last ] . join ( " \n " ) )
end
2008-07-14 11:32:08 -07:00
expected_lines . zip ( actual_lines ) . each_with_index do | pair , line |
2008-08-09 09:43:15 -07:00
message = " template: #{ result_name } \n line: #{ line + 1 } "
2006-12-03 23:17:45 +00:00
assert_equal ( pair . first , pair . last , message )
end
2008-07-27 12:46:57 -07:00
if expected_lines . size < actual_lines . size
2008-08-09 09:43:15 -07:00
assert ( false , " #{ actual_lines . size - expected_lines . size } Trailing lines found in #{ tempfile_name } .css: #{ actual_lines [ expected_lines . size .. - 1 ] . join ( '\n' ) } " )
2008-07-27 12:46:57 -07:00
end
2006-12-03 23:17:45 +00:00
end
2007-08-28 05:33:57 +00:00
2008-07-14 11:32:08 -07:00
def template_loc ( name = nil , prefix = nil )
if name
absolutize " #{ prefix } templates/ #{ name } .sass "
else
absolutize " #{ prefix } templates "
end
2007-09-02 01:48:25 +00:00
end
2008-07-14 11:32:08 -07:00
def tempfile_loc ( name = nil , prefix = nil )
if name
absolutize " #{ prefix } tmp/ #{ name } .css "
else
absolutize " #{ prefix } tmp "
end
end
def result_loc ( name = nil , prefix = nil )
if name
absolutize " #{ prefix } results/ #{ name } .css "
else
absolutize " #{ prefix } results "
end
2006-11-28 20:39:33 +00:00
end
2007-08-28 05:33:57 +00:00
2008-07-14 11:32:08 -07:00
def absolutize ( file )
" #{ File . dirname ( __FILE__ ) } / #{ file } "
2006-12-04 02:47:37 +00:00
end
2007-11-17 19:55:26 +00:00
2008-07-14 11:32:08 -07:00
def set_plugin_opts ( overrides = { } )
2007-11-17 19:55:26 +00:00
Sass :: Plugin . options = {
2008-07-14 11:32:08 -07:00
:template_location = > template_loc ,
:css_location = > tempfile_loc ,
2007-11-17 19:55:26 +00:00
:style = > :compact ,
2008-07-14 11:32:08 -07:00
:load_paths = > [ result_loc ] ,
2007-11-17 19:55:26 +00:00
:always_update = > true ,
2008-07-14 11:32:08 -07:00
} . merge ( overrides )
2007-11-17 19:55:26 +00:00
end
2006-12-04 02:47:37 +00:00
end
module Sass::Plugin
class << self
public :stylesheet_needs_update?
2006-11-28 20:33:22 +00:00
end
end
2006-12-04 02:47:37 +00:00
2006-12-22 06:59:38 +00:00
class Sass :: Engine
alias_method :old_render , :render
2007-08-28 05:33:57 +00:00
2006-12-22 06:59:38 +00:00
def render
raise " bork bork bork! " if @template [ 0 ] == " {bork now!} "
old_render
end
end
2006-12-04 02:47:37 +00:00
class ActionController :: Base
2008-08-30 02:40:59 +02:00
undef :sass_old_process
2006-12-04 02:47:37 +00:00
def sass_old_process ( * args ) ; end
end