2009-11-19 10:26:35 +00:00
require 'test_helper'
2008-08-23 17:00:46 +00:00
require 'fileutils'
require 'compass'
class CompassTest < Test :: Unit :: TestCase
2009-01-19 15:53:51 +00:00
include Compass :: TestCaseHelper
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
2009-11-21 21:29:16 +00:00
teardown_fixtures :blueprint , :empty , :compass , :image_urls , :relative
2008-08-23 17:00:46 +00:00
end
2009-05-08 15:08:26 +00:00
def teardown_fixtures ( * project_names )
project_names . 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
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
2008-12-06 01:48:26 +00:00
assert_renders_correctly :typography
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
assert_renders_correctly :reset , :layout , :utilities
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
assert_renders_correctly :screen
end
end
2009-11-21 21:29:16 +00:00
def test_image_urls
within_project ( 'relative' ) do | proj |
each_css_file ( proj . css_path ) do | css_file |
assert_no_errors css_file , 'relative'
end
assert_renders_correctly :screen
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 "
2008-12-17 18:07:54 +00:00
actual_lines = File . read ( actual_result_file ) . split ( " \n " )
2009-11-24 16:29:02 +00:00
expected_lines = ERB . new ( File . read ( expected_result_file ) ) . result ( binding ) . split ( " \n " )
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 } "
assert_equal ( pair . first , pair . last , message )
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
def within_project ( project_name )
@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 )
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
2009-05-08 15:08:26 +00:00
yield Compass . configuration
2008-08-23 17:00:46 +00:00
rescue
2009-05-08 15:08:26 +00:00
save_output ( project_name )
2008-08-23 17:00:46 +00:00
raise
end
def each_css_file ( dir )
Dir . glob ( " #{ dir } /**/*.css " ) . each do | css_file |
yield css_file
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
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
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
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