From 8c48248a725e66508f0bfc1c0d82c1ee0e36247d Mon Sep 17 00:00:00 2001 From: Scott Davis Date: Wed, 27 Jul 2011 02:39:24 -0400 Subject: [PATCH] more importer tests --- Gemfile.lock | 2 +- lib/compass/sass_extensions/sprites/image.rb | 15 ++++++++++++- .../sass_extensions/sprites/layout_methods.rb | 2 +- .../sass_extensions/sprites/sprite_map.rb | 2 ++ test/units/sprites/image_test.rb | 22 ++++++++++++++++++- test/units/sprites/sprite_map_test.rb | 17 ++++++++++++++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 5df9882b..7b6092b5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - compass (0.12.0.alpha.0.71494ce) + compass (0.12.0.alpha.0.0984b48) chunky_png (~> 1.2) fssm (>= 0.2.7) sass (~> 3.1) diff --git a/lib/compass/sass_extensions/sprites/image.rb b/lib/compass/sass_extensions/sprites/image.rb index 3a3e57cf..fc842b3d 100644 --- a/lib/compass/sass_extensions/sprites/image.rb +++ b/lib/compass/sass_extensions/sprites/image.rb @@ -17,7 +17,16 @@ module Compass # The Full path to the image def file - File.join(Compass.configuration.images_path, relative_file) + @file ||= find_file + end + + def find_file + Compass.configuration.sprite_load_path.each do |path| + f = File.join(path, relative_file) + if File.exists?(f) + return f + end + end end # Width of the image @@ -25,6 +34,10 @@ module Compass dimensions.first end + def size + @size ||= File.size(file) + end + # Height of the image def height dimensions.last diff --git a/lib/compass/sass_extensions/sprites/layout_methods.rb b/lib/compass/sass_extensions/sprites/layout_methods.rb index 48235588..eddfd088 100644 --- a/lib/compass/sass_extensions/sprites/layout_methods.rb +++ b/lib/compass/sass_extensions/sprites/layout_methods.rb @@ -16,7 +16,7 @@ module Compass calculate_horizontal_positions calculate_width else - @images.sort! {|a,b| File.size(b.file) <=> File.size(a.file)} #put small images first + @images.sort! {|a,b| b.size <=> b.size} #put small images first calculate_width calulate_vertical_postions calculate_height diff --git a/lib/compass/sass_extensions/sprites/sprite_map.rb b/lib/compass/sass_extensions/sprites/sprite_map.rb index 8c7f670f..e8c2e2e8 100644 --- a/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/lib/compass/sass_extensions/sprites/sprite_map.rb @@ -23,7 +23,9 @@ module Compass end def self.relative_name(sprite) + sprite = File.expand_path(sprite) Compass.configuration.sprite_load_path.each do |path| + path = File.expand_path(path) if sprite.include?(path) return sprite.gsub("#{path}/", "") end diff --git a/test/units/sprites/image_test.rb b/test/units/sprites/image_test.rb index 24b635b1..a3342138 100644 --- a/test/units/sprites/image_test.rb +++ b/test/units/sprites/image_test.rb @@ -15,7 +15,7 @@ class SpritesImageTest < Test::Unit::TestCase end let(:sprite_filename) { 'squares/ten-by-ten.png' } - let(:sprite_path) { File.join(@images_src_path, sprite_filename) } + let(:sprite_path) { File.join(@images_tmp_path, sprite_filename) } let(:sprite_name) { File.basename(sprite_filename, '.png') } @@ -91,4 +91,24 @@ class SpritesImageTest < Test::Unit::TestCase assert_equal 0, img.offset end + + test 'gets name for sprite in search path' do + Compass.reset_configuration! + uri = 'foo/*.png' + other_folder = File.join(@images_tmp_path, '../other-temp') + FileUtils.mkdir_p other_folder + FileUtils.mkdir_p File.join(other_folder, 'foo') + %w(my bar).each do |file| + FileUtils.touch(File.join(other_folder, "foo/#{file}.png")) + end + config = Compass::Configuration::Data.new('config') + config.images_path = @images_tmp_path + config.sprite_load_path = [@images_tmp_path, other_folder] + Compass.add_configuration(config, "sprite_config") + image = Compass::SassExtensions::Sprites::Image.new(sprite_map_test(options), "foo/my.png", options) + assert_equal File.join(other_folder, 'foo/my.png'), image.file + assert_equal 0, image.size + FileUtils.rm_rf other_folder + end + end diff --git a/test/units/sprites/sprite_map_test.rb b/test/units/sprites/sprite_map_test.rb index abb64542..319cc32e 100644 --- a/test/units/sprites/sprite_map_test.rb +++ b/test/units/sprites/sprite_map_test.rb @@ -121,5 +121,22 @@ class SpriteMapTest < Test::Unit::TestCase assert_equal sizes.max, File.size(@base.images.last.file) end + test "should get correct relative_name" do + Compass.reset_configuration! + uri = 'foo/*.png' + other_folder = File.join(@images_tmp_path, '../other-temp') + FileUtils.mkdir_p other_folder + FileUtils.mkdir_p File.join(other_folder, 'foo') + %w(my bar).each do |file| + FileUtils.touch(File.join(other_folder, "foo/#{file}.png")) + end + config = Compass::Configuration::Data.new('config') + config.images_path = @images_tmp_path + config.sprite_load_path = [@images_tmp_path, other_folder] + Compass.add_configuration(config, "sprite_config") + assert_equal 'foo/my.png', Compass::SassExtensions::Sprites::SpriteMap.relative_name(File.join(other_folder, 'foo/my.png')) + FileUtils.rm_rf other_folder + end + end \ No newline at end of file