2009-11-19 10:26:35 +00:00
require 'test_helper'
2008-08-23 17:00:46 +00:00
require 'fileutils'
require 'compass'
2010-11-14 03:19:41 +00:00
require 'compass/logger'
require 'sass/plugin'
2008-08-23 17:00:46 +00:00
class CompassTest < Test :: Unit :: TestCase
2011-04-28 03:18:52 +00:00
2008-08-23 17:00:46 +00:00
def setup
2009-08-25 21:18:58 +00:00
Compass . reset_configuration!
2008-12-04 20:35:00 +00:00
end
2008-08-23 17:00:46 +00:00
def teardown
2011-04-28 03:18:52 +00:00
[ :blueprint , :empty , :compass , :image_urls , :relative ] . each do | project_name |
:: FileUtils . rm_rf tempfile_path ( project_name )
2008-12-04 20:35:00 +00:00
end
end
2008-08-23 17:00:46 +00:00
2011-02-17 07:30:11 +00:00
def test_on_stylesheet_saved_callback
saved = false
2011-02-20 18:38:00 +00:00
path = nil
2011-02-20 18:16:11 +00:00
config = nil
before_compile = Proc . new do | config |
config . on_stylesheet_saved { | filepath | path = filepath ; saved = true }
end
within_project ( :blueprint , before_compile )
2011-02-17 07:30:11 +00:00
assert saved , " Stylesheet callback didn't get called "
2011-02-20 18:38:00 +00:00
assert path . is_a? ( String ) , " Path is not a string. Got: #{ path . class . name } "
2011-02-17 07:30:11 +00:00
end
# no project with errors exists to test aginst - leep of FAITH!
# *chriseppstein flogs himself*
# def test_on_stylesheet_error_callback
# error = false
# file = nil
# Compass.configuration.on_stylesheet_error {|filename, message| file = filename; error = true }
# within_project(:error) { } #requires a block but we don't need to pass anything - sdavis
# assert error, "Project did not throw a compile error"
# assert file.is_a?(String), "Filename was not a string"
# end
2009-05-19 18:27:29 +00:00
def test_empty_project
# With no sass files, we should have no css files.
2009-05-08 15:08:26 +00:00
within_project ( :empty ) do | proj |
2009-05-19 18:27:29 +00:00
return unless proj . css_path && File . exists? ( proj . css_path )
2009-05-08 15:08:26 +00:00
Dir . new ( proj . css_path ) . each do | f |
2008-12-04 20:35:00 +00:00
fail " This file should not have been generated: #{ f } " unless f == " . " || f == " .. "
end
2008-08-23 17:00:46 +00:00
end
end
2008-12-06 01:48:26 +00:00
def test_blueprint
2009-05-08 15:08:26 +00:00
within_project ( :blueprint ) do | proj |
each_css_file ( proj . css_path ) do | css_file |
2008-12-06 01:48:26 +00:00
assert_no_errors css_file , :blueprint
2008-08-23 17:00:46 +00:00
end
2010-05-31 20:48:25 +00:00
each_sass_file do | sass_file |
assert_renders_correctly sass_file
end
2008-08-23 17:00:46 +00:00
end
end
2009-05-08 15:08:26 +00:00
2008-12-17 18:07:54 +00:00
def test_compass
2009-05-08 15:08:26 +00:00
within_project ( 'compass' ) do | proj |
each_css_file ( proj . css_path ) do | css_file |
2008-12-17 18:07:54 +00:00
assert_no_errors css_file , 'compass'
end
2010-05-31 20:48:25 +00:00
each_sass_file do | sass_file |
2010-12-08 21:04:04 +00:00
assert_renders_correctly sass_file , :ignore_charset = > true
2010-05-31 20:48:25 +00:00
end
2008-12-17 18:07:54 +00:00
end
end
2009-05-08 15:08:26 +00:00
2009-06-26 18:36:31 +00:00
def test_image_urls
within_project ( 'image_urls' ) do | proj |
each_css_file ( proj . css_path ) do | css_file |
assert_no_errors css_file , 'image_urls'
end
2010-05-31 20:48:25 +00:00
each_sass_file do | sass_file |
assert_renders_correctly sass_file
end
2009-06-26 18:36:31 +00:00
end
end
2010-05-31 20:48:25 +00:00
def test_relative
2009-11-21 21:29:16 +00:00
within_project ( 'relative' ) do | proj |
each_css_file ( proj . css_path ) do | css_file |
assert_no_errors css_file , 'relative'
end
2010-05-31 20:48:25 +00:00
each_sass_file do | sass_file |
assert_renders_correctly sass_file
end
2009-11-21 21:29:16 +00:00
end
end
2009-05-08 15:08:26 +00:00
private
def assert_no_errors ( css_file , project_name )
file = css_file [ ( tempfile_path ( project_name ) . size + 1 ) .. - 1 ]
msg = " Syntax Error found in #{ file } . Results saved into #{ save_path ( project_name ) } / #{ file } "
2008-08-23 17:00:46 +00:00
assert_equal 0 , open ( css_file ) . readlines . grep ( / Sass::SyntaxError / ) . size , msg
end
2009-05-08 15:08:26 +00:00
2008-12-04 20:35:00 +00:00
def assert_renders_correctly ( * arguments )
options = arguments . last . is_a? ( Hash ) ? arguments . pop : { }
2008-12-17 18:07:54 +00:00
for name in arguments
2009-05-08 15:08:26 +00:00
actual_result_file = " #{ tempfile_path ( @current_project ) } / #{ name } .css "
expected_result_file = " #{ result_path ( @current_project ) } / #{ name } .css "
2010-12-08 21:04:04 +00:00
actual_lines = File . read ( actual_result_file )
actual_lines . gsub! ( / ^@charset[^;]+; / , '' ) if options [ :ignore_charset ]
actual_lines = actual_lines . split ( " \n " ) . reject { | l | l =~ / \ A \ Z / }
expected_lines = ERB . new ( File . read ( expected_result_file ) ) . result ( binding )
expected_lines . gsub! ( / ^@charset[^;]+; / , '' ) if options [ :ignore_charset ]
expected_lines = expected_lines . split ( " \n " ) . reject { | l | l =~ / \ A \ Z / }
2008-12-17 18:07:54 +00:00
expected_lines . zip ( actual_lines ) . each_with_index do | pair , line |
message = " template: #{ name } \n line: #{ line + 1 } "
2011-04-28 03:18:52 +00:00
if pair . first == pair . last
assert ( true )
else
assert false , diff_as_string ( pair . first . inspect , pair . last . inspect )
end
2008-12-17 18:07:54 +00:00
end
if expected_lines . size < actual_lines . size
assert ( false , " #{ actual_lines . size - expected_lines . size } Trailing lines found in #{ actual_result_file } .css: #{ actual_lines [ expected_lines . size .. - 1 ] . join ( '\n' ) } " )
end
2008-12-04 20:35:00 +00:00
end
end
2009-05-08 15:08:26 +00:00
2011-02-20 18:16:11 +00:00
def within_project ( project_name , config_block = nil )
2009-05-08 15:08:26 +00:00
@current_project = project_name
2009-08-25 21:18:58 +00:00
Compass . add_configuration ( configuration_file ( project_name ) ) if File . exists? ( configuration_file ( project_name ) )
2009-05-08 15:08:26 +00:00
Compass . configuration . project_path = project_path ( project_name )
2009-11-30 04:31:10 +00:00
Compass . configuration . environment = :production
2009-05-08 15:08:26 +00:00
args = Compass . configuration . to_compiler_arguments ( :logger = > Compass :: NullLogger . new )
2011-02-20 18:16:11 +00:00
if config_block
config_block . call ( Compass . configuration )
end
2009-05-19 18:27:29 +00:00
if Compass . configuration . sass_path && File . exists? ( Compass . configuration . sass_path )
compiler = Compass :: Compiler . new * args
compiler . run
end
2011-02-20 18:16:11 +00:00
yield Compass . configuration if block_given?
2008-08-23 17:00:46 +00:00
rescue
2011-02-17 07:30:11 +00:00
save_output ( project_name )
2008-08-23 17:00:46 +00:00
raise
end
2011-02-17 07:30:11 +00:00
2010-05-31 20:48:25 +00:00
def each_css_file ( dir , & block )
Dir . glob ( " #{ dir } /**/*.css " ) . each ( & block )
end
def each_sass_file ( sass_dir = nil )
sass_dir || = template_path ( @current_project )
Dir . glob ( " #{ sass_dir } /**/*.s[ac]ss " ) . each do | sass_file |
yield sass_file [ ( sass_dir . length + 1 ) .. - 6 ]
2008-08-23 17:00:46 +00:00
end
end
def save_output ( dir )
2009-05-08 15:08:26 +00:00
FileUtils . rm_rf ( save_path ( dir ) )
FileUtils . cp_r ( tempfile_path ( dir ) , save_path ( dir ) ) if File . exists? ( tempfile_path ( dir ) )
2008-08-23 17:00:46 +00:00
end
2009-05-08 15:08:26 +00:00
def project_path ( project_name )
absolutize ( " fixtures/stylesheets/ #{ project_name } " )
2008-08-23 17:00:46 +00:00
end
2009-05-08 15:08:26 +00:00
def configuration_file ( project_name )
File . join ( project_path ( project_name ) , " config.rb " )
2008-12-04 20:35:00 +00:00
end
2009-05-08 15:08:26 +00:00
def tempfile_path ( project_name )
File . join ( project_path ( project_name ) , " tmp " )
2008-08-23 17:00:46 +00:00
end
2011-02-17 07:30:11 +00:00
2009-05-08 15:08:26 +00:00
def template_path ( project_name )
File . join ( project_path ( project_name ) , " sass " )
2008-08-23 17:00:46 +00:00
end
2011-02-17 07:30:11 +00:00
2009-05-08 15:08:26 +00:00
def result_path ( project_name )
File . join ( project_path ( project_name ) , " css " )
2008-08-23 17:00:46 +00:00
end
2011-02-17 07:30:11 +00:00
2009-05-08 15:08:26 +00:00
def save_path ( project_name )
File . join ( project_path ( project_name ) , " saved " )
2008-08-23 17:00:46 +00:00
end
2009-11-19 10:26:35 +00:00
end