diff --git a/.travis.yml b/.travis.yml index f797cba1e9..86849d5593 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,15 +16,21 @@ sudo: false env: - CI_TEST=core - CI_TEST=cli + - CI_TEST=sprites gemfile: - core/Gemfile - cli/gemfiles/sass_3_3.gemfile - cli/gemfiles/listen_2.gemfile + - sprites/Gemfile script: cd $CI_TEST && bundle exec rake matrix: + allow_failures: + - gemfile: cli/gemfiles/listen_2.gemfile + env: CI_TEST=cli + rvm: rbx exclude: - gemfile: cli/gemfiles/sass_3_3.gemfile env: CI_TEST=core diff --git a/Guardfile b/Guardfile deleted file mode 100644 index c1258d0cfd..0000000000 --- a/Guardfile +++ /dev/null @@ -1,16 +0,0 @@ -group :tests do - guard :test do - watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" } - watch(%r{^test/.+_test\.rb$}) - watch(%r{^test/units/.+_test\.rb$}) - watch('test/test_helper.rb') { "test" } - end -end - -group :features do - guard :cucumber do - watch(%r{^features/.+\.feature$}) - watch(%r{^features/support/.+$}) { 'features' } - watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' } - end -end \ No newline at end of file diff --git a/Rakefile b/Rakefile index 137b321065..5aa8bd39a2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ -GEMS = ['core', 'cli', 'import-once'] +GEMS = ['core', 'cli', 'import-once', 'sprites'] task :default => %w[test] @@ -10,7 +10,19 @@ task :test do Rake::Task["test_cleanup"].invoke if ok end end +desc "run cli sass 3.3 tests" +task "sass_3_3_test" do + sh "cd cli && (BUNDLE_GEMFILE=gemfiles/sass_3_3.gemfile bundle install --quiet && bundle exec rake test) && cd .." do |ok, res| + Rake::Task["test_cleanup"].invoke if ok + end +end +desc "run cli listen 2 tests" +task "listen_2_test" do + sh "cd cli && (BUNDLE_GEMFILE=gemfiles/listen_2.gemfile bundle install --quiet && bundle exec rake test) && cd .." do |ok, res| + Rake::Task["test_cleanup"].invoke if ok + end +end desc "Clean up all test files" task :test_cleanup do diff --git a/cli/Gemfile b/cli/Gemfile index 7cd7182671..d4c2c82126 100644 --- a/cli/Gemfile +++ b/cli/Gemfile @@ -8,13 +8,14 @@ unless ENV['PKG'] gem "compass-core", :path => "../core" unless defined?(CI) gem "compass-import-once", :path => "../import-once" unless defined?(CI) gem 'sass-globbing', "~> 1.1.1" + gem "compass-sprites", :path => "../sprites" unless defined?(CI) gem "cucumber", "~> 1.2.1" gem "rspec", "~> 2.0.0" gem "compass-validator", "3.0.1" gem "css_parser", "~> 1.0.1" gem "rubyzip", "0.9.9" - gem 'mocha', '0.11.4' gem 'minitest', '2.12.1' + gem "mocha", "~> 0.14.0", :require => false gem 'diff-lcs', '~> 1.1.2' gem 'rake' gem 'json', '~> 1.7.7', :platforms => :ruby_18 diff --git a/cli/compass.gemspec b/cli/compass.gemspec index aff849ad7b..ebabee19bd 100644 --- a/cli/compass.gemspec +++ b/cli/compass.gemspec @@ -17,7 +17,6 @@ Gem::Specification.new do |gemspec| gemspec.add_dependency 'sass', '>= 3.3.13', '< 3.5' gemspec.add_dependency 'compass-core', "~> #{File.read(File.join(File.dirname(__FILE__),"..","core","VERSION")).strip}" gemspec.add_dependency 'compass-import-once', "~> #{File.read(File.join(File.dirname(__FILE__),"..","import-once","VERSION")).strip}" - gemspec.add_dependency 'chunky_png', '~> 1.2' gemspec.add_dependency 'rb-fsevent', '>= 0.9.3' gemspec.add_dependency 'rb-inotify', '>= 0.9' diff --git a/cli/features/step_definitions/command_line_steps.rb b/cli/features/step_definitions/command_line_steps.rb index a33b1e90e6..807baf877a 100644 --- a/cli/features/step_definitions/command_line_steps.rb +++ b/cli/features/step_definitions/command_line_steps.rb @@ -4,9 +4,9 @@ require 'test_helper' require 'compass/exec' -include Compass::TestCaseHelper -include Compass::CommandLineHelper -include Compass::IoHelper +include Compass::Test::TestCaseHelper +include Compass::Test::CommandLineHelper +include Compass::Test::IoHelper Before do Compass.reset_configuration! diff --git a/cli/gemfiles/listen_2.gemfile b/cli/gemfiles/listen_2.gemfile index bc47ebd872..b6a6e24a8e 100644 --- a/cli/gemfiles/listen_2.gemfile +++ b/cli/gemfiles/listen_2.gemfile @@ -6,6 +6,7 @@ gem 'sass', '~> 3.3.12' gem 'compass', :path => "../" gem 'compass-core', :path => "../../core" gem 'compass-import-once', :path => "../../import-once" +gem 'compass-sprites', :path=> '../../sprites' gem 'listen', '~> 2.7.1' gemspec :path=>"../" diff --git a/cli/gemfiles/sass_3_3.gemfile b/cli/gemfiles/sass_3_3.gemfile index 88b890a642..5ae582d634 100644 --- a/cli/gemfiles/sass_3_3.gemfile +++ b/cli/gemfiles/sass_3_3.gemfile @@ -6,5 +6,6 @@ gem 'sass', "~> 3.3.12" gem 'compass', :path => "../" gem 'compass-core', :path => "../../core" gem 'compass-import-once', :path => "../../import-once" +gem "compass-sprites", :path => "../../sprites" gemspec :path=>"../" diff --git a/cli/lib/compass.rb b/cli/lib/compass.rb index 58ff2d8739..a09cf351b6 100644 --- a/cli/lib/compass.rb +++ b/cli/lib/compass.rb @@ -34,7 +34,6 @@ def lib_directory app_integration compiler sass_compiler - sprite_importer ).each do |lib| require "compass/#{lib}" end diff --git a/cli/lib/compass/commands/base.rb b/cli/lib/compass/commands/base.rb index d0149752ff..adbd20c01f 100644 --- a/cli/lib/compass/commands/base.rb +++ b/cli/lib/compass/commands/base.rb @@ -1,10 +1,26 @@ module Compass module Commands class Base + + @callbacks = {} + def self.register(command_name) Compass::Commands[command_name] = self end + def self.register_callback(event, &block) + @callbacks[event] ||= [] + @callbacks[event] << block + end + + def self.run_callback(event) + if @callbacks.has_key?(event) + @callbacks[event].each do |callback| + callback.call(self) + end + end + end + include Actions attr_accessor :working_path, :options @@ -13,7 +29,7 @@ def initialize(working_path, options) self.working_path = working_path.to_s self.options = options end - + def execute perform end diff --git a/cli/lib/compass/commands/clean_project.rb b/cli/lib/compass/commands/clean_project.rb index 7a082380fc..3fc78cbcd1 100644 --- a/cli/lib/compass/commands/clean_project.rb +++ b/cli/lib/compass/commands/clean_project.rb @@ -30,8 +30,12 @@ def initialize(working_path, options) def perform compiler = new_compiler_instance compiler.clean! - Compass::SpriteImporter.find_all_sprite_map_files(Compass.configuration.generated_images_path).each do |sprite| - remove sprite + # if compass sprites is loaded clean those + # todo: the cli interface should emmit some kinda events so addons can attach there cleanup + if defined?(Compass::Sprites::Importer) + Compass::Sprites::Importer.find_all_sprite_map_files(Compass.configuration.generated_images_path).each do |sprite| + remove sprite + end end end diff --git a/cli/lib/compass/commands/sprite.rb b/cli/lib/compass/commands/sprite.rb index 08b4afda54..767578b624 100644 --- a/cli/lib/compass/commands/sprite.rb +++ b/cli/lib/compass/commands/sprite.rb @@ -39,11 +39,10 @@ def initialize(working_path, options) def perform relative_uri = options[:uri].gsub(/^#{Compass.configuration.images_dir}\//, '') - name = Compass::SpriteImporter.sprite_name(relative_uri) - sprites = Compass::SpriteImporter.new + name = Compass::Sprites::Importer.sprite_name(relative_uri) options[:output_file] ||= File.join(Compass.configuration.sass_path, "sprites", "_#{name}.#{Compass.configuration.preferred_syntax}") options[:skip_overrides] ||= false - contents = Compass::SpriteImporter.content_for_images(relative_uri, name, options[:skip_overrides]) + contents = Compass::Sprites::Importer.content_for_images(relative_uri, name, options[:skip_overrides]) if options[:output_file][-4..-1] != "scss" contents = Sass::Engine.new(contents, Compass.sass_engine_options.merge(:syntax => :scss)).to_tree.to_sass end diff --git a/cli/lib/compass/sass_extensions.rb b/cli/lib/compass/sass_extensions.rb index d8b0303d5b..5d5dda83b9 100644 --- a/cli/lib/compass/sass_extensions.rb +++ b/cli/lib/compass/sass_extensions.rb @@ -2,8 +2,4 @@ raise LoadError, "It looks like you've got an incompatible version of Sass. This often happens when you have an old haml gem installed. Please upgrade Haml to v3.1 or above." end -module Compass::SassExtensions -end - require 'compass/sass_extensions/functions' -require 'compass/sass_extensions/sprites' diff --git a/cli/lib/compass/sass_extensions/functions.rb b/cli/lib/compass/sass_extensions/functions.rb index 3be73cffe5..eb52ae5641 100644 --- a/cli/lib/compass/sass_extensions/functions.rb +++ b/cli/lib/compass/sass_extensions/functions.rb @@ -5,11 +5,3 @@ def declare(*args) end end end - -%w(sprites).each do |func| - require "compass/sass_extensions/functions/#{func}" -end - -module Sass::Script::Functions - include Compass::SassExtensions::Functions::Sprites -end diff --git a/cli/lib/compass/sass_extensions/sprites.rb b/cli/lib/compass/sass_extensions/sprites.rb deleted file mode 100644 index c48cf58705..0000000000 --- a/cli/lib/compass/sass_extensions/sprites.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'digest/md5' -require 'compass/sprite_importer' - -module Compass - module SassExtensions - module Sprites - end - end -end - -require 'compass/sass_extensions/sprites/images' -require 'compass/sass_extensions/sprites/layout' -require 'compass/sass_extensions/sprites/image_row' -require 'compass/sass_extensions/sprites/row_fitter' -require 'compass/sass_extensions/sprites/image' -require 'compass/sass_extensions/sprites/layout_methods' -require 'compass/sass_extensions/sprites/sprite_methods' -require 'compass/sass_extensions/sprites/image_methods' -require 'compass/sass_extensions/sprites/sprite_map' -require 'compass/sass_extensions/sprites/engines' - diff --git a/cli/lib/compass/sprite_importer.rb b/cli/lib/compass/sprite_importer.rb deleted file mode 100644 index 83f150e264..0000000000 --- a/cli/lib/compass/sprite_importer.rb +++ /dev/null @@ -1,119 +0,0 @@ -require 'erb' -require 'compass/sprite_importer/binding' -module Compass - class SpriteImporter < Sass::Importers::Base - VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ - SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)\.png} - VALID_EXTENSIONS = ['.png'] - - TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'sprite_importer') - CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb') - CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE)) - - # finds all sprite files - def self.find_all_sprite_map_files(path) - hex = "[0-9a-f]" - glob = "*-s#{hex*10}{#{VALID_EXTENSIONS.join(",")}}" - Sass::Util.glob(File.join(path, "**", glob)) - end - - def find(uri, options) - if uri =~ SPRITE_IMPORTER_REGEX - return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options) - end - nil - end - - def find_relative(uri, base, options) - nil - end - - def to_s - self.class.name - end - - def hash - self.class.name.hash - end - - def eql?(other) - other.class == self.class - end - - def mtime(uri, options) - self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file| - (t = File.mtime(file)) > max_time ? t : max_time - end - end - - def key(uri, options={}) - [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] - end - - def public_url(*args) - nil - end - - def self.path_and_name(uri) - if uri =~ SPRITE_IMPORTER_REGEX - [$1, $3] - else - raise Compass::Error, "invalid sprite path" - end - end - - # Name of this spite - def self.sprite_name(uri) - _, name = path_and_name(uri) - name - end - - # The on-disk location of this sprite - def self.path(uri) - path, _ = path_and_name(uri) - path - end - - # Returns the Glob of image files for the uri - def self.files(uri) - resolved_files = Compass.configuration.sprite_resolver.glob(:image, uri, :match_all => true) - resolved_files = resolved_files.inject({}) do |basenames, file| - basename = File.basename(file, '.png') - unless basenames.has_key?(basename) - basenames[basename] = true - basenames[:files] ||= [] - basenames[:files] << file - end - basenames - end[:files] - return resolved_files.sort if resolved_files.any? - path = Compass.configuration.sprite_resolver.asset_collections.map{|ac| ac.images_path }.join(", ") - raise Compass::SpriteException, %Q{No images were found in the sprite path matching "#{uri}". Your current load paths are: #{path}} - end - - # Returns an Array of image names without the file extension - def self.sprite_names(uri) - files(uri).collect do |file| - File.basename(file, '.png') - end - end - - # Returns the sass_options for this sprite - def self.sass_options(uri, importer, options) - options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer) - end - - # Returns a Sass::Engine for this sprite object - def self.sass_engine(uri, name, importer, options) - content = content_for_images(uri, name, options[:skip_overrides]) - Sass::Engine.new(content, sass_options(uri, importer, options)) - end - - # Generates the Sass for this sprite file - def self.content_for_images(uri, name, skip_overrides = false) - binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri)) - CONTENT_TEMPLATE.result(binder.get_binding) - end - end -end - diff --git a/cli/test/fixtures/stylesheets/compass/config.rb b/cli/test/fixtures/stylesheets/compass/config.rb index fc7b471aeb..0e4758ee3c 100644 --- a/cli/test/fixtures/stylesheets/compass/config.rb +++ b/cli/test/fixtures/stylesheets/compass/config.rb @@ -1,6 +1,7 @@ # Require any additional compass plugins here. require 'true' require 'compass/import-once/activate' +require 'compass/sprites' project_type = :stand_alone css_dir = "tmp" sass_dir = "sass" diff --git a/cli/test/helpers/command_line.rb b/cli/test/helpers/command_line.rb deleted file mode 100644 index c2b58cd9f6..0000000000 --- a/cli/test/helpers/command_line.rb +++ /dev/null @@ -1,114 +0,0 @@ -require 'timeout' - -module Compass::CommandLineHelper - def compass(*arguments) - options = arguments.last.is_a?(Hash) ? arguments.pop : {} - options[:wait] = 0.25 - if block_given? - responder = Responder.new - yield responder - IO.popen("-", "r+") do |io| - if io - #parent process - output = "" - eof_at = nil - while !eof_at || (Time.now - eof_at < options[:wait]) - if io.eof? - eof_at ||= Time.now - sleep 0.1 - else - eof_at = nil - timeout(1) do - partial_output = io.readpartial(1024) - # puts "))))#{partial_output}(((((" - output << partial_output - end - prompt = output.split("\n").last.strip - if response = responder.response_for(prompt) - io.puts response - io.flush - end - end - end - responder.assert_required_responses! - @last_result = decolorize(output) - else - #child process - execute *arguments - end - end - else - @last_error = capture_warning do - @last_result = decolorize(capture_output do - @last_exit_code = execute *arguments - end) - end - end - rescue Timeout::Error - fail "Read from child process timed out" - end - - def decolorize(str) - str.gsub(/\e\[\d+m/,'') - end - - class Responder - Response = Struct.new(:prompt, :text, :required, :responded) - def initialize - @responses = [] - end - def respond_to(prompt, options = {}) - @responses << Response.new(prompt, options[:with], options[:required]) - end - def response_for(prompt) - response = @responses.detect do |r| - case r.prompt - when Regexp - prompt =~ r.prompt - when String - r.prompt == prompt - end - end - if response - response.responded = true - response.text - end - end - def assert_required_responses! - @responses.each do |response| - if response.required && !response.responded - raise "Prompt not encountered: \"#{response.prompt}\"" - end - end - end - end - - def assert_action_performed(action, path) - actions_found = [] - @last_result.split("\n").each do |line| - line = line.split - return if line.first == action.to_s && line.last == path - actions_found << line.first if line.last == path - end - message = "Action #{action.inspect} was not performed on: #{path}." - message += "The following actions were performed: #{actions_found.map{|a|a.inspect}.join(", ")}" if actions_found.any? - # puts @last_result - fail message - end - - def within_tmp_directory(dir = "tmp") - d = absolutize(dir) - FileUtils.mkdir_p(d) - Dir.chdir(d) do - yield - end - ensure - FileUtils.rm_rf(d) - end - - def execute(*arguments) - exit_code = Compass::Exec::SubCommandUI.new(arguments).run! - # fail "Command Failed with exit code: #{exit_code}" unless exit_code == 0 - exit_code - end -end diff --git a/cli/test/helpers/diff.rb b/cli/test/helpers/diff.rb deleted file mode 100644 index 3f988802b5..0000000000 --- a/cli/test/helpers/diff.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'diff/lcs' -require 'diff/lcs/hunk' -module Compass - module Diff - #stole this from rspec who stole this from the gem - def diff_as_string(data_old, data_new) - data_old = data_old.split(/\n/).map! { |e| e.chomp } - data_new = data_new.split(/\n/).map! { |e| e.chomp } - output = "" - diffs = ::Diff::LCS.diff(data_old, data_new) - return output if diffs.empty? - oldhunk = hunk = nil - file_length_difference = 0 - diffs.each do |piece| - begin - hunk = ::Diff::LCS::Hunk.new( - data_old, data_new, piece, context_lines, file_length_difference - ) - file_length_difference = hunk.file_length_difference - next unless oldhunk - # Hunks may overlap, which is why we need to be careful when our - # diff includes lines of context. Otherwise, we might print - # redundant lines. - if (context_lines > 0) and hunk.overlaps?(oldhunk) - hunk.unshift(oldhunk) - else - output << oldhunk.diff(format) - end - ensure - oldhunk = hunk - output << "\n" - end - end - #Handle the last remaining hunk - output << oldhunk.diff(format) << "\n" - end - - protected - - def format - :unified - end - - def context_lines - 3 - end - - end -end \ No newline at end of file diff --git a/cli/test/helpers/io.rb b/cli/test/helpers/io.rb deleted file mode 100644 index ee88380d6c..0000000000 --- a/cli/test/helpers/io.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Compass - module IoHelper - def capture_output - real_stdout, $stdout = $stdout, StringIO.new - yield - $stdout.string - ensure - $stdout = real_stdout - end - - def capture_warning - real_stderr, $stderr = $stderr, StringIO.new - yield - $stderr.string - ensure - $stderr = real_stderr - end - - def capture_pipe(io, options = {}) - options[:wait] = 0.25 - options[:timeout] = 1.0 - output = "" - eof_at = nil - while !eof_at || (Time.now - eof_at < options[:wait]) - if io.eof? - eof_at ||= Time.now - sleep 0.1 - else - eof_at = nil - timeout(options[:timeout]) { output << io.readpartial(1024) } - end - end - output - end - end -end diff --git a/cli/test/helpers/rails.rb b/cli/test/helpers/rails.rb deleted file mode 100644 index 614322a8d0..0000000000 --- a/cli/test/helpers/rails.rb +++ /dev/null @@ -1,55 +0,0 @@ -module Compass - module RailsHelper - def generate_rails_app_directories(name) - Dir.mkdir name - Dir.mkdir File.join(name, "config") - Dir.mkdir File.join(name, "config", "initializers") - Dir.mkdir File.join(name, "tmp") - end - - # Generate a rails application without polluting our current set of requires - # with the rails libraries. This will allow testing against multiple versions of rails - # by manipulating the load path. - def generate_rails_app(name, dir = nil) - if pid = fork - Process.wait(pid) - if $?.exitstatus == 2 - raise LoadError, "Couldn't load rails" - elsif $?.exitstatus != 0 - raise "Failed to generate rails application." - end - else - begin - require 'action_pack/version' - if ActionPack::VERSION::MAJOR >= 3 - require 'rails/generators' - require 'rails/generators/rails/app/app_generator' - require 'mocha' - dir ||= File.join(File.expand_path('../../', __FILE__)) - args = [File.join(dir, name), '-q', '-f', '--skip-bundle', '--skip-gemfile'] - - #stub this so you can generate more apps - Rails::Generators::AppGenerator.any_instance.stubs(:valid_const?).returns(true) - Rails::Generators::AppGenerator.start(args, {:destination_root => dir}) - - else - require 'rails/version' - require 'rails_generator' - require 'rails_generator/scripts/generate' - Rails::Generator::Base.use_application_sources! - capture_output do - Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new $stdout - Rails::Generator::Scripts::Generate.new.run([name], :generator => 'app') - end - end - rescue LoadError - Kernel.exit!(2) - rescue => e - $stderr.puts e - Kernel.exit!(1) - end - Kernel.exit!(0) - end - end - end -end diff --git a/cli/test/helpers/test_case.rb b/cli/test/helpers/test_case.rb deleted file mode 100644 index 3195cecb49..0000000000 --- a/cli/test/helpers/test_case.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Compass - module TestCaseHelper - def absolutize(path) - if Compass::Util.blank?(path) - File.expand_path('../../', __FILE__) - elsif path[0] == ?/ - File.join(File.expand_path('../', __FILE__), path) - else - File.join(File.expand_path('../../', __FILE__), path) - end - end - - # compile a Sass string in the context of a project in the current working directory. - def compile_for_project(contents, options = {}) - Compass.add_project_configuration - options[:syntax] ||= :scss - Sass::Engine.new(contents, Compass.configuration.to_sass_engine_options.merge(options)).render - end - - def assert_correct(before, after) - if before == after - assert(true) - else - assert false, diff_as_string(before.inspect, after.inspect) - end - end - - module ClassMethods - - def let(method, &block) - define_method method, &block - end - - def it(name, &block) - test(name, &block) - end - - def test(name, &block) - define_method "test_#{underscore(name)}".to_sym, &block - end - - def setup(&block) - define_method :setup do - yield - end - end - - def after(&block) - define_method :teardown do - yield - end - end - - private - - def underscore(string) - string.gsub(' ', '_') - end - - end - end -end diff --git a/cli/test/integrations/compass_test.rb b/cli/test/integrations/compass_test.rb index 59eeed29cd..dddac84614 100644 --- a/cli/test/integrations/compass_test.rb +++ b/cli/test/integrations/compass_test.rb @@ -25,7 +25,7 @@ def test_on_stylesheet_saved_callback before_compile = Proc.new do |config| config.on_stylesheet_saved {|filepath| path = filepath; saved = true } end - within_project(:compass, before_compile) + within_project('envtest', before_compile) assert saved, "Stylesheet callback didn't get called" assert path.is_a?(String), "Path is not a string. Got: #{path.class.name}" end diff --git a/cli/test/test_helper.rb b/cli/test/test_helper.rb index 82c82f5d9e..a993773c7f 100644 --- a/cli/test/test_helper.rb +++ b/cli/test/test_helper.rb @@ -5,6 +5,7 @@ require 'compass' require 'test/unit' +require "mocha/setup" require 'true' @@ -14,54 +15,23 @@ def name end end -%w(command_line diff io rails test_case).each do |helper| - require "helpers/#{helper}" -end +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test', 'common', 'helpers')) + +module Compass::Test::TestCaseHelper + def absolutize(path) + File.join(File.expand_path('../', __FILE__), path) + end +end class Test::Unit::TestCase - include Compass::Diff - include Compass::TestCaseHelper - include Compass::IoHelper - extend Compass::TestCaseHelper::ClassMethods + include Compass::Test::Diff + include Compass::Test::TestCaseHelper + include Compass::Test::IoHelper + extend Compass::Test::TestCaseHelper::ClassMethods def fixture_path File.join(File.expand_path('../', __FILE__), 'fixtures') end end - -module SpriteHelper - URI = "selectors/*.png" - - def init_sprite_helper - @images_proj_path = File.join(File.expand_path('../', __FILE__), 'fixtures', 'sprites', 'public') - @images_src_dir = 'images' - @images_src_path = File.join(@images_proj_path, @images_src_dir) - @images_tmp_dir = 'images-tmp' - @images_tmp_path = File.join(@images_proj_path, @images_tmp_dir) - end - - def sprite_map_test(options, uri = URI) - importer = Compass::SpriteImporter.new - path, name = Compass::SpriteImporter.path_and_name(uri) - sprite_names = Compass::SpriteImporter.sprite_names(uri) - sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options) - map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options) - map.options = {:compass => {:logger => Compass::NullLogger.new}} - map - end - - def create_sprite_temp - init_sprite_helper - ::FileUtils.cp_r @images_src_path, @images_tmp_path - end - - def clean_up_sprites - init_sprite_helper - ::FileUtils.rm_r @images_tmp_path - rescue Errno::ENOENT - #pass - end - -end diff --git a/cli/test/units/command_line_test.rb b/cli/test/units/command_line_test.rb index 8771280286..e0c92837d2 100644 --- a/cli/test/units/command_line_test.rb +++ b/cli/test/units/command_line_test.rb @@ -5,9 +5,9 @@ require 'timeout' class CommandLineTest < Test::Unit::TestCase - include Compass::TestCaseHelper - include Compass::CommandLineHelper - include Compass::IoHelper + include Compass::Test::TestCaseHelper + include Compass::Test::CommandLineHelper + include Compass::Test::IoHelper def teardown Compass.reset_configuration! diff --git a/compass-style.org/content/help/tutorials/extending.markdown b/compass-style.org/content/help/tutorials/extending.markdown index e07fe249ae..67366bc829 100644 --- a/compass-style.org/content/help/tutorials/extending.markdown +++ b/compass-style.org/content/help/tutorials/extending.markdown @@ -14,6 +14,8 @@ The sprite engine is the work horse of sprite generation it's the interface for ### Requirements +A sprite engine requires a constant be defined called `VALID_EXTENSIONS` that returns and array of valid image extensions ex. `['.png', '.jpg']` + A sprite engine requires two methods `construct_sprite`, and `save(filename)` Once inside the class you have access to `images` which is a collection of [Compass::SassExtensions::Sprites::Image](http://rdoc.info/github/chriseppstein/compass/dda7c9/Compass/SassExtensions/Sprites/Image) diff --git a/compass-style.org/content/help/tutorials/spriting.markdown b/compass-style.org/content/help/tutorials/spriting.markdown index fcbf668d5d..ecea5ee8a9 100644 --- a/compass-style.org/content/help/tutorials/spriting.markdown +++ b/compass-style.org/content/help/tutorials/spriting.markdown @@ -36,7 +36,7 @@ Each is an icon that is 32px square. The simplest way to use these icon sprites is to let compass give you a class for each sprite: - @import "compass/utilities/sprites"; + @import "compass/sprites"; @import "my-icons/*.png"; @include all-my-icons-sprites; @@ -116,7 +116,7 @@ And your stylesheet will compile to: Getting the image dimensions of a sprite You can get a unit value by using the magical dimension functions `-sprite-height` and `-sprite-width` -If you are looking to just return the dimensions see the [docs](/reference/compass/utilities/sprites/base/#mixin-sprite-dimensions) +If you are looking to just return the dimensions see the [docs](/reference/compass/sprites/base/#mixin-sprite-dimensions) Example: diff --git a/core/.gitignore b/core/.gitignore index 47151fd337..721b36a038 100644 --- a/core/.gitignore +++ b/core/.gitignore @@ -1,3 +1,7 @@ pkg/ .bundle/ Gemfile.lock +RELEASE_VERSION +test/**/tmp/* +Gemfile.lock +.sass-cache diff --git a/core/lib/compass/configuration/adapters.rb b/core/lib/compass/configuration/adapters.rb index c5d308e149..8374a38832 100644 --- a/core/lib/compass/configuration/adapters.rb +++ b/core/lib/compass/configuration/adapters.rb @@ -35,11 +35,6 @@ def to_sass_plugin_options plugin_opts.merge!(sass_options || {}) plugin_opts[:load_paths] ||= [] plugin_opts[:load_paths] += resolve_additional_import_paths - # TODO: When sprites are extracted to their own plugin, this - # TODO: will need to be extracted to there. - if defined?(Compass::SpriteImporter.new) - plugin_opts[:load_paths] << Compass::SpriteImporter.new - end plugin_opts[:full_exception] = (environment == :development) plugin_opts end @@ -93,11 +88,6 @@ def sass_load_paths next p if p.respond_to?(:find_relative) importer.new(p.to_s) end - # TODO: When sprites are extracted to their own plugin, this - # TODO: will need to be extracted to there. - if defined?(Compass::SpriteImporter.new) - load_paths << Compass::SpriteImporter.new - end load_paths end diff --git a/core/stylesheets/_lemonade.scss b/core/stylesheets/_lemonade.scss deleted file mode 100644 index 226d9264fa..0000000000 --- a/core/stylesheets/_lemonade.scss +++ /dev/null @@ -1,38 +0,0 @@ -@mixin image-dimensions($file) { - height: image-height($file); - width: image-width($file); -} - -@mixin sprite-image($file) { - background: sprite-image($file) $repeat; -} - -@mixin sized-sprite-image($file) { - background: sprite-image($file); - @include image-dimensions($file); -} - -@mixin sprite-folder($folder, $image-dimensions: false) { - .#{$folder} { - @if $image-dimensions { - background: sprite-url($folder); - } - @else { - background: sprite-url($folder) no-repeat; - } - } - @for $i from 0 to sprite-files-in-folder($folder) { - $file: sprite-file-from-folder($folder, $i); - .#{$folder}-#{image-basename($file)} { - @extend .#{$folder}; - background-position: sprite-position(sprite-file-from-folder($folder, $i)); - @if $image-dimensions { - @include image-dimensions($file); - } - } - } -} - -@mixin sized-sprite-folder($folder) { - @include sprite-folder($folder, true); -} \ No newline at end of file diff --git a/core/stylesheets/compass/_utilities.scss b/core/stylesheets/compass/_utilities.scss index 53a02445d2..dbe3412ad2 100644 --- a/core/stylesheets/compass/_utilities.scss +++ b/core/stylesheets/compass/_utilities.scss @@ -1,6 +1,5 @@ @import "utilities/color"; @import "utilities/general"; -@import "utilities/sprites"; @import "utilities/tables"; // deprecated diff --git a/core/test/integrations/test_helper.rb b/core/test/integrations/test_helper.rb index 01d72f556b..4c5e06e406 100644 --- a/core/test/integrations/test_helper.rb +++ b/core/test/integrations/test_helper.rb @@ -1,5 +1,5 @@ require "test/unit" -require File.join(File.dirname(__FILE__), "..", "helpers", "diff") +require File.join(File.dirname(__FILE__), "..", "..", "..", "test", "common", "helpers", "diff") -include Compass::Diff +include Compass::Test::Diff diff --git a/core/test/units/test_helper.rb b/core/test/units/test_helper.rb index 2e4d9dd6d5..6407ee4692 100644 --- a/core/test/units/test_helper.rb +++ b/core/test/units/test_helper.rb @@ -4,9 +4,9 @@ require 'compass/core' require "test/unit" -require File.expand_path(File.join(File.dirname(__FILE__), "..", "helpers", "diff")) +require File.join(File.dirname(__FILE__), "..", "..", "..", "test", "common", "helpers", "diff") -include Compass::Diff +include Compass::Test::Diff class Test::Unit::TestCase def assert_raise_message(klass, message) diff --git a/sprites/.gitignore b/sprites/.gitignore new file mode 100644 index 0000000000..f37e3cfdb3 --- /dev/null +++ b/sprites/.gitignore @@ -0,0 +1,2 @@ +.sass-cache +.rake_tasks~ diff --git a/sprites/Gemfile b/sprites/Gemfile new file mode 100644 index 0000000000..b258e217a8 --- /dev/null +++ b/sprites/Gemfile @@ -0,0 +1,12 @@ +# A sample Gemfile +source "https://rubygems.org" +gemspec +gemspec :path => '../cli' +gemspec :path => '../core' +gemspec :path => '../import-once' + +group :test do + gem "mocha", "~> 0.14.0", :require => false +end + +gem "oily_png", "~> 1.1.0" diff --git a/sprites/Gemfile.lock b/sprites/Gemfile.lock new file mode 100644 index 0000000000..9ac28f343f --- /dev/null +++ b/sprites/Gemfile.lock @@ -0,0 +1,66 @@ +PATH + remote: . + specs: + compass-sprites (1.0.0.alpha.17) + chunky_png (~> 1.3.1) + +PATH + remote: ../cli + specs: + compass (1.1.0.alpha.3) + compass-core (~> 1.1.0.alpha.3) + compass-import-once (~> 1.0.5) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) + sass (>= 3.3.13, < 3.5) + +PATH + remote: ../core + specs: + compass-core (1.1.0.alpha.3) + multi_json (~> 1.0) + sass (>= 3.3.0, < 3.5) + +PATH + remote: ../import-once + specs: + compass-import-once (1.0.5) + sass (>= 3.2, < 3.5) + +GEM + remote: https://rubygems.org/ + specs: + chunky_png (1.3.1) + diff-lcs (1.2.5) + ffi (1.9.5) + metaclass (0.0.4) + mocha (0.14.0) + metaclass (~> 0.0.1) + multi_json (1.10.1) + oily_png (1.1.2) + chunky_png (~> 1.3.1) + rake (10.3.2) + rb-fsevent (0.9.4) + rb-inotify (0.9.5) + ffi (>= 0.5.0) + sass (3.4.5) + sass-globbing (1.1.1) + sass (>= 3.1) + true (0.2.3) + sass (>= 3.3.0, < 3.5) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.3) + compass! + compass-core! + compass-import-once! + compass-sprites! + diff-lcs + mocha (~> 0.14.0) + oily_png (~> 1.1.0) + rake + sass-globbing + true (>= 0.2.0.rc.3) diff --git a/sprites/Rakefile b/sprites/Rakefile new file mode 100644 index 0000000000..53ea83bc53 --- /dev/null +++ b/sprites/Rakefile @@ -0,0 +1,33 @@ +require 'rubygems' +require 'rubygems/package_task' +require 'rake/testtask' +require 'fileutils' + +begin + require 'rake/dsl_definition' +rescue LoadError + #pass +end + + +task :default => [:test] + +task :test => [:integrations, :units] + +Rake::TestTask.new :units do |t| + t.libs << 'lib' + t.libs << 'test' + test_files = FileList['test/units/**/*_test.rb'] + test_files.exclude('test/rails/*', 'test/haml/*') + t.test_files = test_files + t.verbose = true +end + +Rake::TestTask.new :integrations do |t| + t.libs << 'lib' + t.libs << 'test' + test_files = FileList['test/integrations/**/*_test.rb'] + test_files.exclude('test/rails/*', 'test/haml/*') + t.test_files = test_files + t.verbose = true +end diff --git a/sprites/VERSION b/sprites/VERSION new file mode 100644 index 0000000000..7401bd40fc --- /dev/null +++ b/sprites/VERSION @@ -0,0 +1 @@ +1.0.0.alpha.17 diff --git a/sprites/compass-sprites.gemspec b/sprites/compass-sprites.gemspec new file mode 100644 index 0000000000..9ff40e7ec5 --- /dev/null +++ b/sprites/compass-sprites.gemspec @@ -0,0 +1,25 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'compass/sprites/version' + +Gem::Specification.new do |spec| + spec.name = "compass-sprites" + spec.version = Compass::Sprites::VERSION + spec.authors = ["Chris Eppstein", "Scott Davis"] + spec.email = ["chris@eppsteins.net", "me@sdavis.info"] + spec.description = %q{The Compass Core Sprite Library. This Library provides an interface for creating sprites with compass} + spec.summary = %q{The Compass Core Sprite Library} + spec.homepage = "https://github.com/chriseppstein/compass/tree/master/sprites" + spec.license = "MIT" + + spec.files = `git ls-files #{File.dirname(__FILE__)}`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_dependency 'chunky_png', '~> 1.3.1' + spec.add_development_dependency "bundler", "~> 1.3" + spec.add_development_dependency "rake" + spec.add_development_dependency 'diff-lcs', '~> 1.1.2' +end diff --git a/sprites/lib/compass/sprites.rb b/sprites/lib/compass/sprites.rb new file mode 100644 index 0000000000..ab9e632428 --- /dev/null +++ b/sprites/lib/compass/sprites.rb @@ -0,0 +1,12 @@ +require 'compass/sprites/importer' +require 'compass/sprites/version' +stylesheet_dir = File.expand_path('../../../stylesheets', __FILE__) +config = Compass::Configuration::Data.new("compass-sprites") +config.add_import_path(Compass::Sprites::Importer.new) +config.add_import_path(stylesheet_dir) +Compass.add_configuration(config) + + +Compass::Frameworks.register('sprite-framework', + :stylesheets_directory => stylesheet_dir +) diff --git a/sprites/lib/compass/sprites/importer.rb b/sprites/lib/compass/sprites/importer.rb new file mode 100644 index 0000000000..c61566f992 --- /dev/null +++ b/sprites/lib/compass/sprites/importer.rb @@ -0,0 +1,145 @@ +require 'erb' +require 'compass-core' +require 'compass/sprites/sass_extensions/sprites' +require 'compass/sprites/importer/binding' + +module Compass + module Sprites + class Importer < Sass::Importers::Base + VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ + SPRITE_IMPORTER_REGEX = %r{((.+\/)?([^\*.]+))\/(.+?)} + + TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'importer') + CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb') + CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE)) + + # finds all sprite files + def self.find_all_sprite_map_files(path) + hex = "[0-9a-f]" + glob = "*-s#{hex*10}{#{valid_extensions.join(",")}}" + Sass::Util.glob(File.join(path, "**", glob)) + end + + def find(uri, options) + if uri =~ self.class.sprite_importer_regex_with_ext + return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options) + end + nil + end + + def find_relative(uri, base, options) + nil + end + + def to_s + self.class.name + end + + def hash + self.class.name.hash + end + + def key(uri, options={}) + [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] + end + + def public_url(*args) + nil + end + + def self.path_and_name(uri) + if uri =~ sprite_importer_regex_with_ext + [$1, $3] + else + raise Compass::Error, "invalid sprite path" + end + end + + def eql?(other) + other.class == self.class + end + + def mtime(uri, options) + self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file| + (t = File.mtime(file)) > max_time ? t : max_time + end + end + + def key(uri, options={}) + [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] + end + + def self.path_and_name(uri) + if uri =~ sprite_importer_regex_with_ext + [$1, $3] + else + raise Compass::Error, "invalid sprite path" + end + end + + # The on-disk location of this sprite + def self.path(uri) + path, _ = path_and_name(uri) + path + end + + # Returns the Glob of image files for the uri + def self.files(uri) + resolved_files = Compass.configuration.sprite_resolver.glob(:image, uri, :match_all => true) + resolved_files = resolved_files.inject({}) do |basenames, file| + basename = File.basename(file, '.png') + unless basenames.has_key?(basename) + basenames[basename] = true + basenames[:files] ||= [] + basenames[:files] << file + end + basenames + end[:files] + return resolved_files.sort if resolved_files.any? + path = Compass.configuration.sprite_resolver.asset_collections.map{|ac| ac.images_path }.join(", ") + raise Compass::SpriteException, %Q{No images were found in the sprite path matching "#{uri}". Your current load paths are: #{path}} + end + + # Name of this spite + def self.sprite_name(uri) + _, name = path_and_name(uri) + name + end + + # Returns an Array of image names without the file extension + def self.sprite_names(uri) + files(uri).collect do |file| + File.basename(file, '.png') + end + end + + # Returns the sass_options for this sprite + def self.sass_options(uri, importer, options) + options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer) + end + + # Returns a Sass::Engine for this sprite object + def self.sass_engine(uri, name, importer, options) + content = content_for_images(uri, name, options[:skip_overrides]) + Sass::Engine.new(content, sass_options(uri, importer, options)) + end + + # Generates the Sass for this sprite file + def self.content_for_images(uri, name, skip_overrides = false) + binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri)) + CONTENT_TEMPLATE.result(binder.get_binding) + end + + private + + def self.valid_extensions + @valid_extensions ||= Compass::Sprites::SassExtensions::SpriteMap.sprite_engine_class::VALID_EXTENSIONS + end + + def self.sprite_importer_regex_with_ext + @importer_regex ||= %r{#{SPRITE_IMPORTER_REGEX}(#{valid_extensions.join('|')})} + end + end + end +end + diff --git a/cli/lib/compass/sprite_importer/binding.rb b/sprites/lib/compass/sprites/importer/binding.rb similarity index 100% rename from cli/lib/compass/sprite_importer/binding.rb rename to sprites/lib/compass/sprites/importer/binding.rb diff --git a/cli/lib/compass/sprite_importer/content.erb b/sprites/lib/compass/sprites/importer/content.erb similarity index 99% rename from cli/lib/compass/sprite_importer/content.erb rename to sprites/lib/compass/sprites/importer/content.erb index 7f072f83f8..62ac2943d5 100644 --- a/cli/lib/compass/sprite_importer/content.erb +++ b/sprites/lib/compass/sprites/importer/content.erb @@ -1,4 +1,4 @@ -@import "compass/utilities/sprites/base"; +@import "compass/sprites/base"; // General Sprite Defaults // You can override them before you import this file. diff --git a/cli/lib/compass/sass_extensions/sprites/engines.rb b/sprites/lib/compass/sprites/sass_extensions/engines.rb similarity index 80% rename from cli/lib/compass/sass_extensions/sprites/engines.rb rename to sprites/lib/compass/sprites/sass_extensions/engines.rb index 978a68361c..eb9d94fde5 100644 --- a/cli/lib/compass/sass_extensions/sprites/engines.rb +++ b/sprites/lib/compass/sprites/sass_extensions/engines.rb @@ -1,9 +1,10 @@ + module Compass module SassExtensions module Sprites class Engine attr_accessor :width, :height, :images, :canvas - def initialize(width, height, images) + def initialize(width=nil, height=nil, images=nil) @width, @height, @images = width, height, images @canvas = nil end @@ -21,5 +22,4 @@ def save(filename) end end - -require 'compass/sass_extensions/sprites/engines/chunky_png_engine' +require 'compass/sprites/sass_extensions/engines/chunky_png_engine' \ No newline at end of file diff --git a/cli/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb b/sprites/lib/compass/sprites/sass_extensions/engines/chunky_png_engine.rb similarity index 96% rename from cli/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb rename to sprites/lib/compass/sprites/sass_extensions/engines/chunky_png_engine.rb index 9c494438e6..d4af6afbf6 100644 --- a/cli/lib/compass/sass_extensions/sprites/engines/chunky_png_engine.rb +++ b/sprites/lib/compass/sprites/sass_extensions/engines/chunky_png_engine.rb @@ -9,6 +9,8 @@ module SassExtensions module Sprites class ChunkyPngEngine < Compass::SassExtensions::Sprites::Engine + VALID_EXTENSIONS = ['.png'] + def construct_sprite @canvas = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT) images.each do |image| diff --git a/cli/lib/compass/sass_extensions/functions/sprites.rb b/sprites/lib/compass/sprites/sass_extensions/functions.rb similarity index 98% rename from cli/lib/compass/sass_extensions/functions/sprites.rb rename to sprites/lib/compass/sprites/sass_extensions/functions.rb index 218ae9665c..d826271c39 100644 --- a/cli/lib/compass/sass_extensions/functions/sprites.rb +++ b/sprites/lib/compass/sprites/sass_extensions/functions.rb @@ -1,4 +1,4 @@ -module Compass::SassExtensions::Functions::Sprites +module Compass::Sprites::SassExtensions::Functions extend Compass::SassExtensions::Functions::SassDeclarationHelper extend Sass::Script::Value::Helpers @@ -75,7 +75,7 @@ def inline_sprite(map) # the first time it is converted to a url. Simply constructing it has no side-effects. def sprite_map(glob, kwargs = {}) kwargs.extend VariableReader - Compass::SassExtensions::Sprites::SpriteMap.from_uri(glob, self, kwargs) + Compass::Sprites::SassExtensions::SpriteMap.from_uri(glob, self, kwargs) end declare :sprite_map, [:glob], :var_kwargs => true @@ -281,7 +281,7 @@ def convert_sprite_name(sprite) end def verify_map(map, error = "sprite") - unless map.is_a?(Compass::SassExtensions::Sprites::SpriteMap) + unless map.is_a?(Compass::Sprites::SassExtensions::SpriteMap) missing_sprite!(error) end end diff --git a/cli/lib/compass/sass_extensions/sprites/image.rb b/sprites/lib/compass/sprites/sass_extensions/image.rb similarity index 98% rename from cli/lib/compass/sass_extensions/sprites/image.rb rename to sprites/lib/compass/sprites/sass_extensions/image.rb index 6f1525ffe2..7c2b3df7c5 100644 --- a/cli/lib/compass/sass_extensions/sprites/image.rb +++ b/sprites/lib/compass/sprites/sass_extensions/image.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions class Image include Sass::Script::Value::Helpers ACTIVE = %r{[_-]active$} diff --git a/cli/lib/compass/sass_extensions/sprites/image_methods.rb b/sprites/lib/compass/sprites/sass_extensions/image_methods.rb similarity index 97% rename from cli/lib/compass/sass_extensions/sprites/image_methods.rb rename to sprites/lib/compass/sprites/sass_extensions/image_methods.rb index 1a80f3ca51..850a4134bb 100644 --- a/cli/lib/compass/sass_extensions/sprites/image_methods.rb +++ b/sprites/lib/compass/sprites/sass_extensions/image_methods.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module ImageMethods # Fetches the Sprite::Image object for the supplied name def image_for(name) diff --git a/cli/lib/compass/sass_extensions/sprites/image_row.rb b/sprites/lib/compass/sprites/sass_extensions/image_row.rb similarity index 95% rename from cli/lib/compass/sass_extensions/sprites/image_row.rb rename to sprites/lib/compass/sprites/sass_extensions/image_row.rb index 51ae0d7391..211ed8de56 100644 --- a/cli/lib/compass/sass_extensions/sprites/image_row.rb +++ b/sprites/lib/compass/sprites/sass_extensions/image_row.rb @@ -1,8 +1,8 @@ require 'forwardable' module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions class ImageRow extend Forwardable diff --git a/cli/lib/compass/sass_extensions/sprites/images.rb b/sprites/lib/compass/sprites/sass_extensions/images.rb similarity index 93% rename from cli/lib/compass/sass_extensions/sprites/images.rb rename to sprites/lib/compass/sprites/sass_extensions/images.rb index 6ee529d408..d72b7b34b4 100644 --- a/cli/lib/compass/sass_extensions/sprites/images.rb +++ b/sprites/lib/compass/sprites/sass_extensions/images.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions class Images < Array def sort_by!(method) diff --git a/cli/lib/compass/sass_extensions/sprites/layout.rb b/sprites/lib/compass/sprites/sass_extensions/layout.rb similarity index 95% rename from cli/lib/compass/sass_extensions/sprites/layout.rb rename to sprites/lib/compass/sprites/sass_extensions/layout.rb index b4bfa025d2..b92247d46a 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout.rb @@ -1,7 +1,7 @@ require 'rational' module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module Layout class SpriteLayout diff --git a/cli/lib/compass/sass_extensions/sprites/layout/diagonal.rb b/sprites/lib/compass/sprites/sass_extensions/layout/diagonal.rb similarity index 96% rename from cli/lib/compass/sass_extensions/sprites/layout/diagonal.rb rename to sprites/lib/compass/sprites/sass_extensions/layout/diagonal.rb index 0c2ab885b3..c3bce26ca5 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout/diagonal.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout/diagonal.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module Layout class Diagonal < SpriteLayout diff --git a/cli/lib/compass/sass_extensions/sprites/layout/horizontal.rb b/sprites/lib/compass/sprites/sass_extensions/layout/horizontal.rb similarity index 97% rename from cli/lib/compass/sass_extensions/sprites/layout/horizontal.rb rename to sprites/lib/compass/sprites/sass_extensions/layout/horizontal.rb index e46c18fa2e..ff5a517df4 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout/horizontal.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout/horizontal.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module Layout class Horizontal < SpriteLayout diff --git a/cli/lib/compass/sass_extensions/sprites/layout/smart.rb b/sprites/lib/compass/sprites/sass_extensions/layout/smart.rb similarity index 95% rename from cli/lib/compass/sass_extensions/sprites/layout/smart.rb rename to sprites/lib/compass/sprites/sass_extensions/layout/smart.rb index 14bfb29b00..9da109776b 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout/smart.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout/smart.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module Layout class Smart < SpriteLayout diff --git a/cli/lib/compass/sass_extensions/sprites/layout/vertical.rb b/sprites/lib/compass/sprites/sass_extensions/layout/vertical.rb similarity index 98% rename from cli/lib/compass/sass_extensions/sprites/layout/vertical.rb rename to sprites/lib/compass/sprites/sass_extensions/layout/vertical.rb index 1a3900f317..4e2ccdaa42 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout/vertical.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout/vertical.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module Layout class Vertical < SpriteLayout diff --git a/cli/lib/compass/sass_extensions/sprites/layout_methods.rb b/sprites/lib/compass/sprites/sass_extensions/layout_methods.rb similarity index 80% rename from cli/lib/compass/sass_extensions/sprites/layout_methods.rb rename to sprites/lib/compass/sprites/sass_extensions/layout_methods.rb index 7ca1f38262..c3cfb29f4d 100644 --- a/cli/lib/compass/sass_extensions/sprites/layout_methods.rb +++ b/sprites/lib/compass/sprites/sass_extensions/layout_methods.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module LayoutMethods HORIZONTAL = 'horizontal' DIAGONAL = 'diagonal' @@ -32,16 +32,16 @@ def layout def compute_image_positions! case layout when SMART - require 'compass/sass_extensions/sprites/layout/smart' + require 'compass/sprites/sass_extensions/layout/smart' @images, @width, @height = Layout::Smart.new(@images, @kwargs).properties when DIAGONAL - require 'compass/sass_extensions/sprites/layout/diagonal' + require 'compass/sprites/sass_extensions/layout/diagonal' @images, @width, @height = Layout::Diagonal.new(@images, @kwargs).properties when HORIZONTAL - require 'compass/sass_extensions/sprites/layout/horizontal' + require 'compass/sprites/sass_extensions/layout/horizontal' @images, @width, @height = Layout::Horizontal.new(@images, @kwargs).properties else - require 'compass/sass_extensions/sprites/layout/vertical' + require 'compass/sprites/sass_extensions/layout/vertical' @images, @width, @height = Layout::Vertical.new(@images, @kwargs).properties end end diff --git a/cli/lib/compass/sass_extensions/sprites/row_fitter.rb b/sprites/lib/compass/sprites/sass_extensions/row_fitter.rb similarity index 96% rename from cli/lib/compass/sass_extensions/sprites/row_fitter.rb rename to sprites/lib/compass/sprites/sass_extensions/row_fitter.rb index 834c57fa2f..806fcdcba4 100644 --- a/cli/lib/compass/sass_extensions/sprites/row_fitter.rb +++ b/sprites/lib/compass/sprites/sass_extensions/row_fitter.rb @@ -39,7 +39,7 @@ def efficiency private def new_row(image = nil) - row = Compass::SassExtensions::Sprites::ImageRow.new(width) + row = Compass::Sprites::SassExtensions::ImageRow.new(width) row.add(image) if image row end diff --git a/cli/lib/compass/sass_extensions/sprites/sprite_map.rb b/sprites/lib/compass/sprites/sass_extensions/sprite_map.rb similarity index 76% rename from cli/lib/compass/sass_extensions/sprites/sprite_map.rb rename to sprites/lib/compass/sprites/sass_extensions/sprite_map.rb index 22125ea359..e38e45900b 100644 --- a/cli/lib/compass/sass_extensions/sprites/sprite_map.rb +++ b/sprites/lib/compass/sprites/sass_extensions/sprite_map.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions class SpriteMap < Sass::Script::Value::Base attr_accessor :image_names, :path, :name, :map, :kwargs attr_accessor :images, :width, :height, :engine @@ -15,8 +15,8 @@ class SpriteMap < Sass::Script::Value::Base # the path is relative to the images_path confguration option def self.from_uri(uri, context, kwargs) uri = uri.value - path, name = Compass::SpriteImporter.path_and_name(uri) - files = Compass::SpriteImporter.files(uri) + path, name = Compass::Sprites::Importer.path_and_name(uri) + files = Compass::Sprites::Importer.files(uri) sprites = files.map do |sprite| relative_name(sprite) end @@ -27,6 +27,10 @@ def self.relative_name(sprite) Compass.configuration.sprite_resolver.relative_path(:image, sprite) end + def self.sprite_engine_class + constantize("Compass::SassExtensions::Sprites::#{self.modulize}Engine") + end + def initialize(sprites, path, name, context, kwargs) @image_names = sprites @path = path @@ -76,10 +80,23 @@ def method_missing(meth, *args, &block) private - def modulize + def self.modulize @modulize ||= Compass::configuration.sprite_engine.to_s.scan(/([^_.]+)/).flatten.map {|chunk| "#{chunk[0].chr.upcase}#{chunk[1..-1]}" }.join end + def self.constantize(camel_cased_word) + names = camel_cased_word.split('::') + names.shift if names.empty? || names.first.empty? + + constant = Object + names.each do |name| + constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) + end + constant + end + + + end end end diff --git a/cli/lib/compass/sass_extensions/sprites/sprite_methods.rb b/sprites/lib/compass/sprites/sass_extensions/sprite_methods.rb similarity index 96% rename from cli/lib/compass/sass_extensions/sprites/sprite_methods.rb rename to sprites/lib/compass/sprites/sass_extensions/sprite_methods.rb index a5295aee2a..409d917959 100644 --- a/cli/lib/compass/sass_extensions/sprites/sprite_methods.rb +++ b/sprites/lib/compass/sprites/sass_extensions/sprite_methods.rb @@ -1,6 +1,6 @@ module Compass - module SassExtensions - module Sprites + module Sprites + module SassExtensions module SpriteMethods # Changing this string will invalidate all previously generated sprite images. @@ -18,7 +18,7 @@ def compute_image_metadata! end def init_engine - @engine = eval("::Compass::SassExtensions::Sprites::#{modulize}Engine.new(nil, nil, nil)") + @engine = self.class.sprite_engine_class.new @engine.width = @width @engine.height = @height @engine.images = @images diff --git a/sprites/lib/compass/sprites/sass_extensions/sprites.rb b/sprites/lib/compass/sprites/sass_extensions/sprites.rb new file mode 100644 index 0000000000..f22e20deee --- /dev/null +++ b/sprites/lib/compass/sprites/sass_extensions/sprites.rb @@ -0,0 +1,24 @@ +module Compass + module Sprites + module SassExtensions + end + end +end + +require 'digest/md5' +require 'compass/sprites/sass_extensions/functions' +require 'compass/sprites/sass_extensions/images' +require 'compass/sprites/sass_extensions/layout' +require 'compass/sprites/sass_extensions/image_row' +require 'compass/sprites/sass_extensions/row_fitter' +require 'compass/sprites/sass_extensions/image' +require 'compass/sprites/sass_extensions/layout_methods' +require 'compass/sprites/sass_extensions/sprite_methods' +require 'compass/sprites/sass_extensions/image_methods' +require 'compass/sprites/sass_extensions/sprite_map' +require 'compass/sprites/sass_extensions/engines' +require 'compass/sprites/importer' + +module Sass::Script::Functions + include Compass::Sprites::SassExtensions::Functions +end diff --git a/sprites/lib/compass/sprites/version.rb b/sprites/lib/compass/sprites/version.rb new file mode 100644 index 0000000000..7c17001cd3 --- /dev/null +++ b/sprites/lib/compass/sprites/version.rb @@ -0,0 +1,5 @@ +module Compass + module Sprites + VERSION = File.read(File.join(File.dirname(__FILE__), "..", "..", "..", "VERSION")).strip + end +end diff --git a/core/stylesheets/compass/utilities/_sprites.scss b/sprites/stylesheets/compass/_sprites.scss similarity index 100% rename from core/stylesheets/compass/utilities/_sprites.scss rename to sprites/stylesheets/compass/_sprites.scss diff --git a/core/stylesheets/compass/utilities/sprites/_base.scss b/sprites/stylesheets/compass/sprites/_base.scss similarity index 100% rename from core/stylesheets/compass/utilities/sprites/_base.scss rename to sprites/stylesheets/compass/sprites/_base.scss diff --git a/core/stylesheets/compass/utilities/sprites/_sprite-img.scss b/sprites/stylesheets/compass/sprites/_sprite-img.scss similarity index 100% rename from core/stylesheets/compass/utilities/sprites/_sprite-img.scss rename to sprites/stylesheets/compass/sprites/_sprite-img.scss diff --git a/cli/test/fixtures/sprites/public/images/bad_extensions/ten-by-ten.gif b/sprites/test/fixtures/public/images/bad_extensions/ten-by-ten.gif similarity index 100% rename from cli/test/fixtures/sprites/public/images/bad_extensions/ten-by-ten.gif rename to sprites/test/fixtures/public/images/bad_extensions/ten-by-ten.gif diff --git a/cli/test/fixtures/sprites/public/images/bad_extensions/twenty-by-twenty.jpg b/sprites/test/fixtures/public/images/bad_extensions/twenty-by-twenty.jpg similarity index 100% rename from cli/test/fixtures/sprites/public/images/bad_extensions/twenty-by-twenty.jpg rename to sprites/test/fixtures/public/images/bad_extensions/twenty-by-twenty.jpg diff --git a/cli/test/fixtures/sprites/public/images/bool/false.png b/sprites/test/fixtures/public/images/bool/false.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/bool/false.png rename to sprites/test/fixtures/public/images/bool/false.png diff --git a/cli/test/fixtures/sprites/public/images/bool/true.png b/sprites/test/fixtures/public/images/bool/true.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/bool/true.png rename to sprites/test/fixtures/public/images/bool/true.png diff --git a/cli/test/fixtures/sprites/public/images/colors/blue.png b/sprites/test/fixtures/public/images/colors/blue.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/colors/blue.png rename to sprites/test/fixtures/public/images/colors/blue.png diff --git a/cli/test/fixtures/sprites/public/images/colors/yellow.png b/sprites/test/fixtures/public/images/colors/yellow.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/colors/yellow.png rename to sprites/test/fixtures/public/images/colors/yellow.png diff --git a/cli/test/fixtures/sprites/public/images/focus/ten-by-ten.png b/sprites/test/fixtures/public/images/focus/ten-by-ten.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/focus/ten-by-ten.png rename to sprites/test/fixtures/public/images/focus/ten-by-ten.png diff --git a/cli/test/fixtures/sprites/public/images/focus/ten-by-ten_active.png b/sprites/test/fixtures/public/images/focus/ten-by-ten_active.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/focus/ten-by-ten_active.png rename to sprites/test/fixtures/public/images/focus/ten-by-ten_active.png diff --git a/cli/test/fixtures/sprites/public/images/focus/ten-by-ten_focus.png b/sprites/test/fixtures/public/images/focus/ten-by-ten_focus.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/focus/ten-by-ten_focus.png rename to sprites/test/fixtures/public/images/focus/ten-by-ten_focus.png diff --git a/cli/test/fixtures/sprites/public/images/focus/ten-by-ten_hover.png b/sprites/test/fixtures/public/images/focus/ten-by-ten_hover.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/focus/ten-by-ten_hover.png rename to sprites/test/fixtures/public/images/focus/ten-by-ten_hover.png diff --git a/cli/test/fixtures/sprites/public/images/focus/ten-by-ten_target.png b/sprites/test/fixtures/public/images/focus/ten-by-ten_target.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/focus/ten-by-ten_target.png rename to sprites/test/fixtures/public/images/focus/ten-by-ten_target.png diff --git a/cli/test/fixtures/sprites/public/images/image_row/large.png b/sprites/test/fixtures/public/images/image_row/large.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/image_row/large.png rename to sprites/test/fixtures/public/images/image_row/large.png diff --git a/cli/test/fixtures/sprites/public/images/image_row/large_square.png b/sprites/test/fixtures/public/images/image_row/large_square.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/image_row/large_square.png rename to sprites/test/fixtures/public/images/image_row/large_square.png diff --git a/cli/test/fixtures/sprites/public/images/image_row/medium.png b/sprites/test/fixtures/public/images/image_row/medium.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/image_row/medium.png rename to sprites/test/fixtures/public/images/image_row/medium.png diff --git a/cli/test/fixtures/sprites/public/images/image_row/small.png b/sprites/test/fixtures/public/images/image_row/small.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/image_row/small.png rename to sprites/test/fixtures/public/images/image_row/small.png diff --git a/cli/test/fixtures/sprites/public/images/image_row/tall.png b/sprites/test/fixtures/public/images/image_row/tall.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/image_row/tall.png rename to sprites/test/fixtures/public/images/image_row/tall.png diff --git a/cli/test/fixtures/sprites/public/images/ko/default_background.png b/sprites/test/fixtures/public/images/ko/default_background.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/ko/default_background.png rename to sprites/test/fixtures/public/images/ko/default_background.png diff --git a/cli/test/fixtures/sprites/public/images/ko/starbg26x27.png b/sprites/test/fixtures/public/images/ko/starbg26x27.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/ko/starbg26x27.png rename to sprites/test/fixtures/public/images/ko/starbg26x27.png diff --git a/cli/test/fixtures/sprites/public/images/nested/squares/ten-by-ten.png b/sprites/test/fixtures/public/images/nested/squares/ten-by-ten.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/nested/squares/ten-by-ten.png rename to sprites/test/fixtures/public/images/nested/squares/ten-by-ten.png diff --git a/cli/test/fixtures/sprites/public/images/numeric/200.png b/sprites/test/fixtures/public/images/numeric/200.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/numeric/200.png rename to sprites/test/fixtures/public/images/numeric/200.png diff --git a/cli/test/fixtures/sprites/public/images/prefix/20-by-20.png b/sprites/test/fixtures/public/images/prefix/20-by-20.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/prefix/20-by-20.png rename to sprites/test/fixtures/public/images/prefix/20-by-20.png diff --git a/cli/test/fixtures/sprites/public/images/prefix/ten-by-ten.png b/sprites/test/fixtures/public/images/prefix/ten-by-ten.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/prefix/ten-by-ten.png rename to sprites/test/fixtures/public/images/prefix/ten-by-ten.png diff --git a/cli/test/fixtures/sprites/public/images/repeat_x/five.png b/sprites/test/fixtures/public/images/repeat_x/five.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/repeat_x/five.png rename to sprites/test/fixtures/public/images/repeat_x/five.png diff --git a/cli/test/fixtures/sprites/public/images/repeat_x/four.png b/sprites/test/fixtures/public/images/repeat_x/four.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/repeat_x/four.png rename to sprites/test/fixtures/public/images/repeat_x/four.png diff --git a/cli/test/fixtures/sprites/public/images/repeat_x/one.png b/sprites/test/fixtures/public/images/repeat_x/one.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/repeat_x/one.png rename to sprites/test/fixtures/public/images/repeat_x/one.png diff --git a/cli/test/fixtures/sprites/public/images/repeat_x/three.png b/sprites/test/fixtures/public/images/repeat_x/three.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/repeat_x/three.png rename to sprites/test/fixtures/public/images/repeat_x/three.png diff --git a/cli/test/fixtures/sprites/public/images/repeat_x/two.png b/sprites/test/fixtures/public/images/repeat_x/two.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/repeat_x/two.png rename to sprites/test/fixtures/public/images/repeat_x/two.png diff --git a/cli/test/fixtures/sprites/public/images/selectors/ten-by-ten.png b/sprites/test/fixtures/public/images/selectors/ten-by-ten.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/selectors/ten-by-ten.png rename to sprites/test/fixtures/public/images/selectors/ten-by-ten.png diff --git a/cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_active.png b/sprites/test/fixtures/public/images/selectors/ten-by-ten_active.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_active.png rename to sprites/test/fixtures/public/images/selectors/ten-by-ten_active.png diff --git a/cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_hover.png b/sprites/test/fixtures/public/images/selectors/ten-by-ten_hover.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_hover.png rename to sprites/test/fixtures/public/images/selectors/ten-by-ten_hover.png diff --git a/cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_target.png b/sprites/test/fixtures/public/images/selectors/ten-by-ten_target.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/selectors/ten-by-ten_target.png rename to sprites/test/fixtures/public/images/selectors/ten-by-ten_target.png diff --git a/cli/test/fixtures/sprites/public/images/squares/ten-by-ten.png b/sprites/test/fixtures/public/images/squares/ten-by-ten.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/squares/ten-by-ten.png rename to sprites/test/fixtures/public/images/squares/ten-by-ten.png diff --git a/cli/test/fixtures/sprites/public/images/squares/twenty-by-twenty.png b/sprites/test/fixtures/public/images/squares/twenty-by-twenty.png similarity index 100% rename from cli/test/fixtures/sprites/public/images/squares/twenty-by-twenty.png rename to sprites/test/fixtures/public/images/squares/twenty-by-twenty.png diff --git a/sprites/test/helpers/sprite_helper.rb b/sprites/test/helpers/sprite_helper.rb new file mode 100644 index 0000000000..7b50878bdf --- /dev/null +++ b/sprites/test/helpers/sprite_helper.rb @@ -0,0 +1,34 @@ +require "compass/sprites" +module Compass::Sprites::Test::SpriteHelper + URI = "selectors/*.png" + + def init_sprite_helper + @images_proj_path = File.join(fixture_path, 'public') + @images_src_dir = 'images' + @images_src_path = File.join(@images_proj_path, @images_src_dir) + @images_tmp_dir = 'images-tmp' + @images_tmp_path = File.join(@images_proj_path, @images_tmp_dir) + end + + def sprite_map_test(options, uri = URI) + importer = Compass::Sprites::Importer.new + path, name = Compass::Sprites::Importer.path_and_name(uri) + sprite_names = Compass::Sprites::Importer.sprite_names(uri) + sass_engine = Compass::Sprites::Importer.sass_engine(uri, name, importer, options) + map = Compass::Sprites::SassExtensions::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options) + map.options = {:compass => {:logger => Compass::NullLogger.new}} + map + end + + def create_sprite_temp + init_sprite_helper + ::FileUtils.cp_r @images_src_path, @images_tmp_path + end + + def clean_up_sprites + init_sprite_helper + ::FileUtils.rm_r @images_tmp_path + rescue Errno::ENOENT + #pass + end +end diff --git a/cli/test/integrations/sprites_test.rb b/sprites/test/integrations/sprites_test.rb similarity index 99% rename from cli/test/integrations/sprites_test.rb rename to sprites/test/integrations/sprites_test.rb index 676de45190..e5808d2a61 100644 --- a/cli/test/integrations/sprites_test.rb +++ b/sprites/test/integrations/sprites_test.rb @@ -1,6 +1,5 @@ require 'test_helper' require 'fileutils' -require 'compass' require 'compass/logger' require 'sass/plugin' @@ -9,7 +8,7 @@ class SpritesTest < Test::Unit::TestCase def setup Compass.reset_configuration! - @images_project_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'sprites', 'public')) + @images_project_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures', 'public')) @images_src_dir = 'images' @images_src_path = File.join(@images_project_path, @images_src_dir) @images_tmp_dir = 'images-tmp' @@ -19,8 +18,10 @@ def setup ::FileUtils.cp_r @images_src_path, @images_tmp_path ::FileUtils.mkdir_p @generated_images_tmp_path file = StringIO.new(<<-CONFIG) + require 'compass/sprites' project_path = "#{@images_project_path}" images_dir = "#{@images_tmp_dir}" + add_import_path(Compass::Sprites::Importer.new) CONFIG Compass.add_configuration(file, "sprite_config") Compass.configure_sass_plugin! @@ -96,6 +97,7 @@ def clean(string) images_path = #{@images_tmp_path.inspect} generated_images_path = #{@generated_images_tmp_path.inspect} http_generated_images_path = "/images/generated" + add_import_path(Compass::Sprites::Importer.new) CONFIG Compass.add_configuration(file, "sprite_config") Compass.configure_sass_plugin! @@ -937,7 +939,7 @@ def clean(string) it "should replace text with images and dimensions using sprites" do css = render <<-SCSS - @import "compass/utilities/sprites/sprite-img"; + @import "compass/sprites/sprite-img"; @import "colors/*.png"; .blue { @include sprite-replace-text($colors-sprites, blue); @@ -981,6 +983,7 @@ def clean(string) file = StringIO.new(<<-CONFIG) images_path = #{@images_tmp_path.inspect} generated_images_path = #{@generated_images_tmp_path.inspect} + add_import_path(Compass::Sprites::Importer.new) CONFIG Compass.add_configuration(file, "sprite_config") Compass.configure_sass_plugin! diff --git a/sprites/test/test_helper.rb b/sprites/test/test_helper.rb new file mode 100644 index 0000000000..2273e2aa7e --- /dev/null +++ b/sprites/test/test_helper.rb @@ -0,0 +1,69 @@ + +::TEST_DIR = File.dirname(__FILE__) +$:.unshift(::TEST_DIR) unless $:.include?(::TEST_DIR) +cli_dir = File.join(::TEST_DIR, '..', '..', 'cli', 'lib') +$:.unshift(cli_dir) unless $:.include?(cli_dir) +core_dir = File.join(::TEST_DIR, '..', '..', 'core', 'lib') +$:.unshift(core_dir) unless $:.include?(core_dir) +lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +$:.unshift(lib_dir) unless $:.include?(lib_dir) + +require 'compass' +require 'compass/logger' +require 'compass/sprites' +require 'test/unit' +require "mocha/setup" +module Compass + module Sprites + module Test + end + end +end +require File.join(::TEST_DIR, '..', '..', 'test', 'common', 'helpers') +require File.join(::TEST_DIR, 'helpers', 'sprite_helper') + +class Test::Unit::TestCase + include Compass::Test::Diff + include Compass::Test::TestCaseHelper + include Compass::Test::IoHelper + extend Compass::Test::TestCaseHelper::ClassMethods + + def fixture_path + File.join(::TEST_DIR, 'fixtures') + end + +end + +module SpriteHelper + URI = "selectors/*.png" + + def init_sprite_helper + @images_proj_path = File.join(fixture_path, 'public') + @images_src_dir = 'images' + @images_src_path = File.join(@images_proj_path, @images_src_dir) + @images_tmp_dir = 'images-tmp' + @images_tmp_path = File.join(@images_proj_path, @images_tmp_dir) + end + + def sprite_map_test(options, uri = URI) + importer = Compass::Sprites::Importer.new + path, name = Compass::Sprites::Importer.path_and_name(uri) + sprite_names = Compass::Sprites::Importer.sprite_names(uri) + sass_engine = Compass::Sprites::Importer.sass_engine(uri, name, importer, options) + map = Compass::Sprites::SassExtensions::SpriteMap.new(sprite_names.map{|n| uri.gsub('*', n)}, path, name, sass_engine, options) + map.options = {:compass => {:logger => Compass::NullLogger.new}} + map + end + + def create_sprite_temp + init_sprite_helper + ::FileUtils.cp_r @images_src_path, @images_tmp_path + end + + def clean_up_sprites + init_sprite_helper + ::FileUtils.rm_r @images_tmp_path + rescue Errno::ENOENT + #pass + end +end diff --git a/cli/test/units/sprites/engine_test.rb b/sprites/test/units/engine_test.rb similarity index 70% rename from cli/test/units/sprites/engine_test.rb rename to sprites/test/units/engine_test.rb index 571c1facbf..d7567daccb 100644 --- a/cli/test/units/sprites/engine_test.rb +++ b/sprites/test/units/engine_test.rb @@ -1,12 +1,12 @@ require 'test_helper' class EngineTest < Test::Unit::TestCase - include SpriteHelper + include Compass::Sprites::Test::SpriteHelper def setup create_sprite_temp sprite_filename = 'squares/ten-by-ten.png' @images = [ - Compass::SassExtensions::Sprites::Image.new(nil, File.join(sprite_filename), {}) + Compass::Sprites::SassExtensions::Image.new(nil, File.join(sprite_filename), {}) ] @engine = Compass::SassExtensions::Sprites::Engine.new(100, 100, @images) end @@ -16,19 +16,19 @@ def taredown end - test "should have width of 100" do + def test_should_have_width_of_100 assert_equal 100, @engine.width end - test "should have height of 100" do + def test_should_have_height_of_100 assert_equal 100, @engine.height end - test "should have correct images" do + def test_should_have_correct_images assert_equal @images, @engine.images end - test "raises Compass::Error when calling save" do + def test_raises_compass_error_when_calling_save begin @engine.save('foo') assert false, '#save did not raise an exception' @@ -37,7 +37,7 @@ def taredown end end - test "raises Compass::Error when calling construct_sprite" do + def test_raises_compass_error_when_calling_construct_sprite begin @engine.construct_sprite assert false, '#construct_sprite did not raise an exception' diff --git a/cli/test/units/sprites/image_row_test.rb b/sprites/test/units/image_row_test.rb similarity index 70% rename from cli/test/units/sprites/image_row_test.rb rename to sprites/test/units/image_row_test.rb index 6f40c819ce..e60ee4e15a 100644 --- a/cli/test/units/sprites/image_row_test.rb +++ b/sprites/test/units/image_row_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class ImageRowTest < Test::Unit::TestCase - include SpriteHelper + include Compass::Sprites::Test::SpriteHelper def setup clean_up_sprites create_sprite_temp @@ -11,7 +11,7 @@ def setup @image_files = Dir["#{@images_src_path}/image_row/*.png"].sort @images = @image_files.map do |img| img.gsub!("#{@images_src_path}/", '') - Compass::SassExtensions::Sprites::Image.new(nil, img, {}) + Compass::Sprites::SassExtensions::Image.new(nil, img, {}) end image_row(1000) end @@ -21,7 +21,7 @@ def teardown end def image_row(max) - @image_row = Compass::SassExtensions::Sprites::ImageRow.new(max) + @image_row = Compass::Sprites::SassExtensions::ImageRow.new(max) end def populate_row @@ -30,28 +30,28 @@ def populate_row end end - it "should return false if image will not fit in row" do + def test_should_return_false_if_image_will_not_fit_in_row image_row(100) - img = Compass::SassExtensions::Sprites::Image.new(nil, File.join('image_row', 'large.png'), {}) + img = Compass::Sprites::SassExtensions::Image.new(nil, File.join('image_row', 'large.png'), {}) assert !@image_row.add(img) end - it "should have 5 images" do + def test_should_have_5_images populate_row assert_equal 5, @image_row.images.size end - it "should return max image width" do + def test_should_return_max_image_width populate_row assert_equal 400, @image_row.width end - it "should return max image height" do + def test_should_return_max_image_height populate_row assert_equal 40, @image_row.height end - it "should have an efficiency rating" do + def test_should_have_an_efficiency_rating populate_row assert_equal 1 - (580.0 / 1000.0), @image_row.efficiency end diff --git a/cli/test/units/sprites/image_test.rb b/sprites/test/units/image_test.rb similarity index 85% rename from cli/test/units/sprites/image_test.rb rename to sprites/test/units/image_test.rb index 6a34bdb420..8cd13ca1f0 100644 --- a/cli/test/units/sprites/image_test.rb +++ b/sprites/test/units/image_test.rb @@ -1,9 +1,8 @@ require 'test_helper' -require 'mocha' require 'ostruct' class SpritesImageTest < Test::Unit::TestCase - include SpriteHelper + include Compass::Sprites::Test::SpriteHelper def setup create_sprite_temp @@ -36,7 +35,7 @@ def test_image(options ={}) test_map(options).images.first end - test 'initialize' do + def test_initialize image = test_image assert_equal sprite_name, image.name assert_equal sprite_path, image.file @@ -48,11 +47,11 @@ def test_image(options ={}) assert_equal 0, image.left end - test 'hover' do + def test_hover assert_equal 'ten-by-ten_hover', test_image.hover.name end - test 'hover should find image by _ or - in file name' do + def test_hover_should_find_image_by_underscore_or_dash_in_file_name map = test_map(:seperator => '-') map.images.each_index do |i| if map.images[i].name == 'ten-by-ten_hover' @@ -64,50 +63,50 @@ def test_image(options ={}) assert_equal 'ten-by-ten-hover', test_image.hover.name end - test 'no parent' do + def test_no_parent assert_nil test_image.parent end - test 'image type is "global" should raise exception' do + def test_image_type_is_global_should_raise_exception assert_raise ::Compass::SpriteException do image = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('global') image.repeat end end - test 'image type is "no-repeat"' do + def test_image_type_is_no_repeat img = test_image assert_equal 'no-repeat', img.repeat assert img.no_repeat? end - test 'image repeat-x' do + def test_image_repeat_x img = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('repeat-x') assert img.repeat_x? end - test 'image repeat-y' do + def test_image_repeat_y img = test_image "selectors_ten_by_ten_repeat" => Sass::Script::String.new('repeat-y') assert img.repeat_y? end - test 'image position' do + def test_image_position image = test_image "selectors_ten_by_ten_position" => Sass::Script::Number.new(100, ["px"]) assert_equal 100, image.position.value end - test 'image spacing' do + def test_image_spacing @spacing = 10 image = test_image "spacing" => Sass::Script::Number.new(100, ["px"]) assert_equal 100, image.spacing end - test 'offset' do + def test_offset image = test_image "selectors_ten_by_ten_position" => Sass::Script::Number.new(100, ["px"]) assert_equal 100, image.offset end - test 'neither, uses 0' do + def test_neither_uses_0 img = test_image img.position.stubs(:unitless?).returns(false) assert_equal 0, img.offset diff --git a/cli/test/units/sprites/images_test.rb b/sprites/test/units/images_test.rb similarity index 77% rename from cli/test/units/sprites/images_test.rb rename to sprites/test/units/images_test.rb index 3041e04efb..bb72159cb4 100644 --- a/cli/test/units/sprites/images_test.rb +++ b/sprites/test/units/images_test.rb @@ -1,10 +1,10 @@ require 'test_helper' -require 'compass/sass_extensions/sprites/images' +require 'compass/sprites/sass_extensions/images' class ImagesTest < Test::Unit::TestCase def setup - @images = Compass::SassExtensions::Sprites::Images.new + @images = Compass::Sprites::SassExtensions::Images.new @images << OpenStruct.new(:foo => 1, :name => 'bob', :size => 1200, :width => 10) @images << OpenStruct.new(:foo => 2, :name => 'bob', :size => 300, :width => 100) @images << OpenStruct.new(:foo => 3, :name => 'aob', :size => 120, :width => 50) @@ -12,32 +12,32 @@ def setup end - test "sort by size" do + def test_sort_by_size @images.sort_by! :size assert_equal [3, 2, 4, 1], @images.map(&:foo) end - test "sort by !size" do + def test_sort_by_size_with_bang @images.sort_by! '!size' assert_equal [3, 2, 4, 1].reverse, @images.map(&:foo) end - test "sort by name" do + def test_sort_by_name @images.sort_by! :name assert_equal [3, 2, 1, 4], @images.map(&:foo) end - test "sort by !name" do + def test_sort_by_name_with_bang @images.sort_by! '!name' assert_equal [3, 2, 1, 4].reverse, @images.map(&:foo) end - test "sort by width" do + def test_sort_by_width @images.sort_by! :width assert_equal [1, 3, 4, 2], @images.map(&:foo) end - test "sort by !width" do + def test_sort_by_width_with_bang @images.sort_by! '!width' assert_equal [1, 3, 4, 2].reverse, @images.map(&:foo) end diff --git a/cli/test/units/sprites/importer_test.rb b/sprites/test/units/importer_test.rb similarity index 61% rename from cli/test/units/sprites/importer_test.rb rename to sprites/test/units/importer_test.rb index 6691fa4153..934b69a9a3 100644 --- a/cli/test/units/sprites/importer_test.rb +++ b/sprites/test/units/importer_test.rb @@ -1,24 +1,24 @@ require 'test_helper' class ImporterTest < Test::Unit::TestCase - include SpriteHelper - + include Compass::Sprites::Test::SpriteHelper + def setup create_sprite_temp file = StringIO.new("images_path = #{@images_src_path.inspect}\n") Compass.add_configuration(file, "sprite_config") - @importer = Compass::SpriteImporter.new + @importer = Compass::Sprites::Importer.new end def teardown Compass.reset_configuration! end - + def options {:foo => 'bar'} end - - test "should use search path to find sprites" do + + def test_should_use_search_path_to_find_sprites Compass.reset_configuration! uri = 'foo/*.png' other_folder = File.join(@images_tmp_path, '../other-temp') @@ -31,61 +31,60 @@ def options config.images_path = @images_tmp_path config.sprite_load_path = [@images_tmp_path, other_folder] Compass.add_configuration(config, "sprite_config") - importer = Compass::SpriteImporter.new + importer = Compass::Sprites::Importer.new assert_equal 2, Compass.configuration.sprite_load_path.compact.size assert Compass.configuration.sprite_load_path.include?(other_folder) - assert_equal ["bar", "my"], Compass::SpriteImporter.sprite_names(uri) - + assert_equal ["bar", "my"], Compass::Sprites::Importer.sprite_names(uri) + ensure FileUtils.rm_rf other_folder end - - test "name should return the sprite name" do - assert_equal 'selectors', Compass::SpriteImporter.sprite_name(URI) + + def test_name_should_return_the_sprite_name + assert_equal 'selectors', Compass::Sprites::Importer.sprite_name(URI) end - - test "path should return the sprite path" do - assert_equal 'selectors', Compass::SpriteImporter.path(URI) + + def test_path_should_return_the_sprite_path + assert_equal 'selectors', Compass::Sprites::Importer.path(URI) end - - test "should return all the sprite names" do - assert_equal ["ten-by-ten", "ten-by-ten_active", "ten-by-ten_hover", "ten-by-ten_target"], Compass::SpriteImporter.sprite_names(URI) + + def test_should_return_all_the_sprite_names + assert_equal ["ten-by-ten", "ten-by-ten_active", "ten-by-ten_hover", "ten-by-ten_target"], Compass::Sprites::Importer.sprite_names(URI) end - - test "should have correct mtime" do + + def test_should_have_correct_mtime thirtydays = Time.now.to_i + (60*60*24*30) file = Dir[File.join(@images_src_path, URI)].sort.first File.utime(thirtydays, thirtydays, file) assert_equal thirtydays, File.mtime(file).to_i assert_equal thirtydays, @importer.mtime(URI, {}).to_i end - - test "should return sass engine on find" do + + def test_should_return_sass_engine_on_find assert @importer.find(URI, {}).is_a?(Sass::Engine) end - - test "sass options should contain options" do - opts = Compass::SpriteImporter.sass_options('foo', @importer, options) + + def test_sass_options_should_contain_options + opts = Compass::Sprites::Importer.sass_options('foo', @importer, options) assert_equal 'bar', opts[:foo] end - - test "verify that the sass_engine passes the correct filename" do - importer = Compass::SpriteImporter.new - engine = Compass::SpriteImporter.sass_engine(URI, 'foo', importer, options) + + def test_verify_that_the_sass_engine_passes_the_correct_filename + importer = Compass::Sprites::Importer.new + engine = Compass::Sprites::Importer.sass_engine(URI, 'foo', importer, options) assert_equal engine.options[:filename], URI end - - test "should fail given bad sprite extensions" do + + def test_should_fail_given_bad_sprite_extensions @images_src_path = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'sprites', 'public', 'images') file = StringIO.new("images_path = #{@images_src_path.inspect}\n") Compass.add_configuration(file, "sprite_config") - importer = Compass::SpriteImporter.new + importer = Compass::Sprites::Importer.new uri = "bad_extensions/*.jpg" begin - Compass::SpriteImporter.sass_engine(uri, Compass::SpriteImporter.sprite_name(uri), importer, {}) + Compass::Sprites::Importer.sass_engine(uri, Compass::Sprites::Importer.sprite_name(uri), importer, {}) assert false, "An invalid sprite file made it past validation." rescue Compass::Error => e assert e.message.include?("invalid sprite path") end end - end \ No newline at end of file diff --git a/cli/test/units/sprites/layout_test.rb b/sprites/test/units/layout_test.rb similarity index 81% rename from cli/test/units/sprites/layout_test.rb rename to sprites/test/units/layout_test.rb index 8eca41eaac..19d67b353d 100644 --- a/cli/test/units/sprites/layout_test.rb +++ b/sprites/test/units/layout_test.rb @@ -4,7 +4,7 @@ class LayoutTest < Test::Unit::TestCase include SpriteHelper def setup - Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader) + Hash.send(:include, Compass::Sprites::SassExtensions::Functions::VariableReader) clean_up_sprites create_sprite_temp file = StringIO.new("images_path = #{@images_tmp_path.inspect}\n") @@ -27,12 +27,12 @@ def vertical def smart options = @options.merge("layout" => Sass::Script::String.new('smart')) - importer = Compass::SpriteImporter.new + importer = Compass::Sprites::Importer.new uri = "image_row/*.png" - path, name = Compass::SpriteImporter.path_and_name(uri) - sprite_names = Compass::SpriteImporter.sprite_names(uri) - sass_engine = Compass::SpriteImporter.sass_engine(uri, name, importer, options) - map = Compass::SassExtensions::Sprites::SpriteMap.new(sprite_names.map {|n| "image_row/#{n}.png"}, path, name, sass_engine, options) + path, name = Compass::Sprites::Importer.path_and_name(uri) + sprite_names = Compass::Sprites::Importer.sprite_names(uri) + sass_engine = Compass::Sprites::Importer.sass_engine(uri, name, importer, options) + map = Compass::Sprites::SassExtensions::SpriteMap.new(sprite_names.map {|n| "image_row/#{n}.png"}, path, name, sass_engine, options) map.options = {:compass => {:logger => Compass::NullLogger.new}} map @@ -53,7 +53,7 @@ def horizontal(options= {}, uri=URI) # REPEAT_X - test 'repeat-x layout single image' do + def test_repeat_x_layout_single_image opts = {"repeat_x_three_repeat" => Sass::Script::String.new('repeat-x'), 'sort_by' => Sass::Script::String.new('width')} map = sprite_map_test(@options.merge(opts), 'repeat_x/*.png') assert_equal 6, map.width @@ -61,13 +61,13 @@ def horizontal(options= {}, uri=URI) assert_equal [0, 0, 0, 0, 0, 0, 3], map.images.map(&:left) end - test 'repeat-x layout multi image' do + def test_repeat_x_layout_multi_image opts = {"repeat_x_three_repeat" => Sass::Script::String.new('repeat-x'), "repeat_x_four_repeat" => Sass::Script::String.new('repeat-x')} map = sprite_map_test(@options.merge(opts), 'repeat_x/*.png') assert_equal 12, map.width end - test "repeat-y layout single image" do + def test_repeat_y_layout_single_image opts = {"layout" => Sass::Script::String.new('horizontal'), "squares_ten_by_ten_repeat" => Sass::Script::String.new('repeat-y')} map = sprite_map_test(@options.merge(opts), 'squares/*.png') assert_equal 30, map.width @@ -77,7 +77,7 @@ def horizontal(options= {}, uri=URI) assert map.horizontal? end - test "repeat-y layout multi image" do + def test_repeat_y_layout_multi_image opts = {"layout" => Sass::Script::String.new('horizontal'), "repeat_x_three_repeat" => Sass::Script::String.new('repeat-y'), "repeat_x_four_repeat" => Sass::Script::String.new('repeat-y')} map = sprite_map_test(@options.merge(opts), 'repeat_x/*.png') assert_equal [[0, 0], [0, 5], [0, 9], [0, 10], [0, 13], [4, 5], [8, 5], [3, 10], [6, 10], [9, 10]], map.images.map { |img| [img.top, img.left] } @@ -85,24 +85,24 @@ def horizontal(options= {}, uri=URI) # VERTICAL LAYOUT - it "should have a vertical layout" do + def test_should_have_a_vertical_layout vert = vertical assert_equal [0, 10, 20, 30], vert.images.map(&:top) assert_equal [0, 0, 0, 0], vert.images.map(&:left) assert vert.vertical? end - it "should have a vertical layout with spacing" do + def test_should_have_a_vertical_layout_with_spacing vert = sprite_map_test(@options.merge({"spacing" => Sass::Script::Number.new(10, ['px'])})) assert_equal [0, 20, 40, 60], vert.images.map(&:top) end - it "should layout vertical with position" do + def test_should_layout_vertical_with_position vert = sprite_map_test("selectors_ten_by_ten_active_position" => Sass::Script::Number.new(10, ['px'])) assert_equal [0, 10, 0, 0], vert.images.map(&:left) end - it "should generate vertical sprites in decending order" do + def test_should_generate_vertical_sprites_in_decending_order sizes = vertical.images.map{|image| File.size(image.file) } assert_equal sizes.min, File.size(vertical.images.first.file) assert_equal sizes.max, File.size(vertical.images.last.file) @@ -110,7 +110,7 @@ def horizontal(options= {}, uri=URI) # SMART LAYOUT - it "should have a smart layout" do + def test_should_have_a_smart_layout base = smart base.generate assert base.smart? @@ -123,7 +123,7 @@ def horizontal(options= {}, uri=URI) # DIAGONAL LAYOUT - it "should generate a diagonal sprite" do + def test_should_generate_a_diagonal_sprite base = diagonal base.generate assert base.diagonal? @@ -136,45 +136,45 @@ def horizontal(options= {}, uri=URI) # HORIZONTAL LAYOUT - it "should have a horizontal layout" do + def test_should_have_a_horizontal_layout base = horizontal assert base.horizontal? assert_equal 10, base.height assert_equal 40, base.width end - it "should layout images horizontaly" do + def test_should_layout_images_horizontaly base = horizontal assert_equal [0, 10, 20, 30], base.images.map(&:left) assert_equal [0, 0, 0, 0], base.images.map(&:top) end - it "should layout horizontaly with spacing" do + def test_should_layout_horizontaly_with_spacing base = horizontal("spacing" => Sass::Script::Number.new(10, ['px'])) assert_equal [0, 20, 40, 60], base.images.map(&:left) assert_equal [0, 0, 0, 0], base.images.map(&:top) assert_equal 80, base.width end - it "should layout horizontaly with spacing and and position" do + def test_should_layout_horizontaly_with_spacing_and_position base = horizontal({"spacing" => Sass::Script::Number.new(10, ['px']), "position" => Sass::Script::Number.new(50, ['%'])}, 'squares/*.png') assert_equal [0, 20], base.images.map(&:left) assert_equal [5, 0], base.images.map(&:top) assert_equal 50, base.width end - it "should layout horizontaly with position" do + def test_should_layout_horizontaly_with_position base = horizontal("selectors_ten_by_ten_active_position" => Sass::Script::Number.new(10, ['px'])) assert_equal [0, 10, 0, 0], base.images.map(&:top) assert_equal 40, base.width assert_equal 20, base.height end - it "should generate a horrizontal sprite" do + def test_should_generate_a_horrizontal_sprite base = horizontal base.generate assert File.exists?(base.filename) FileUtils.rm base.filename end -end \ No newline at end of file +end diff --git a/cli/test/units/regressions_test.rb b/sprites/test/units/regressions_test.rb similarity index 95% rename from cli/test/units/regressions_test.rb rename to sprites/test/units/regressions_test.rb index 51e411be45..c73fede37c 100644 --- a/cli/test/units/regressions_test.rb +++ b/sprites/test/units/regressions_test.rb @@ -5,7 +5,7 @@ class RegressionsTest < Test::Unit::TestCase include SpriteHelper - include Compass::CommandLineHelper + include Compass::Test::CommandLineHelper def setup create_sprite_temp diff --git a/cli/test/units/sprites/row_fitter_test.rb b/sprites/test/units/row_fitter_test.rb similarity index 86% rename from cli/test/units/sprites/row_fitter_test.rb rename to sprites/test/units/row_fitter_test.rb index 38096ef893..50e5956aa8 100644 --- a/cli/test/units/sprites/row_fitter_test.rb +++ b/sprites/test/units/row_fitter_test.rb @@ -1,5 +1,5 @@ require 'test_helper' -require 'compass/sass_extensions/sprites/row_fitter' +require 'compass/sprites/sass_extensions/row_fitter' class RowFitterTest < Test::Unit::TestCase include SpriteHelper @@ -18,7 +18,7 @@ def teardown def create_images(dims) dims.collect { |width, height| - image = Compass::SassExtensions::Sprites::Image.new('blah', 'blah', {}) + image = Compass::Sprites::SassExtensions::Image.new('blah', 'blah', {}) image.stubs(:width => width, :height => height) image } @@ -34,7 +34,7 @@ def basic_dims ] end - it 'should use the fast placement algorithm' do + def test_should_use_the_fast_placement_algorithm images = create_images(basic_dims) row_fitter(images) @@ -50,7 +50,7 @@ def basic_dims assert_equal [ images[4] ], row_fitter[3].images end - it 'should use the scan placement algorithm' do + def test_should_use_the_scan_placement_algorithm images = create_images(basic_dims) row_fitter(images) diff --git a/cli/test/units/sprites/sprite_command_test.rb b/sprites/test/units/sprite_command_test.rb similarity index 75% rename from cli/test/units/sprites/sprite_command_test.rb rename to sprites/test/units/sprite_command_test.rb index 2c30c1681c..53ea3f26ff 100644 --- a/cli/test/units/sprites/sprite_command_test.rb +++ b/sprites/test/units/sprite_command_test.rb @@ -1,9 +1,9 @@ require 'test_helper' require 'compass/exec' class SpriteCommandTest < Test::Unit::TestCase - include Compass::TestCaseHelper - include Compass::CommandLineHelper - include Compass::IoHelper + include Compass::Test::TestCaseHelper + include Compass::Test::CommandLineHelper + include Compass::Test::IoHelper attr_reader :test_dir include SpriteHelper @@ -24,7 +24,7 @@ def config_data end def create_temp_cli_dir - directory = File.join(File.expand_path('../', __FILE__), 'test') + directory = File.join(File.expand_path('../', __FILE__), '..', 'test') ::FileUtils.mkdir_p directory @test_dir = directory end @@ -47,9 +47,9 @@ def teardown end end - it "should create sprite file" do + def test_should_create_sprite_file assert_equal 0, run_compass_with_options(['sprite', "-f", 'stylesheet.scss', "squares/*.png"]).to_i - assert File.exists?(File.join(test_dir, 'stylesheet.scss')) + assert File.exists?(File.join(test_dir, 'stylesheet.scss')), "Stylesheet.scss was not created" end end \ No newline at end of file diff --git a/cli/test/units/sprites/sprite_map_test.rb b/sprites/test/units/sprite_map_test.rb similarity index 67% rename from cli/test/units/sprites/sprite_map_test.rb rename to sprites/test/units/sprite_map_test.rb index 96e8b0c9f7..a48fe9cbf3 100644 --- a/cli/test/units/sprites/sprite_map_test.rb +++ b/sprites/test/units/sprite_map_test.rb @@ -2,9 +2,9 @@ class SpriteMapTest < Test::Unit::TestCase include SpriteHelper - + def setup - Hash.send(:include, Compass::SassExtensions::Functions::Sprites::VariableReader) + Hash.send(:include, Compass::Sprites::SassExtensions::Functions::VariableReader) create_sprite_temp file = StringIO.new(<<-CONFIG) project_path = "#{@images_proj_path}" @@ -20,70 +20,64 @@ def teardown clean_up_sprites @base = nil end - - it "should have the correct size" do + + def test_should_have_the_correct_size assert_equal [10,40], @base.size end - - it "should have the sprite names" do - assert_equal Compass::SpriteImporter.sprite_names(URI), @base.sprite_names + + def test_should_have_the_sprite_names + assert_equal Compass::Sprites::Importer.sprite_names(URI), @base.sprite_names end - - it 'should have image filenames' do + + def test_should_have_image_filenames assert_equal Dir["#{@images_tmp_path}/selectors/*.png"].sort, @base.image_filenames end - - it 'should need generation' do + + def test_should_need_generation assert @base.generation_required? end - - test 'uniqueness_hash' do + + def test_uniqueness_hash assert_equal '4c703bbc05', @base.uniqueness_hash end - - it 'should be outdated' do + + def test_should_be_outdated assert @base.outdated? end - it 'should have correct filename' do + def test_should_have_correct_filename assert_equal File.join(@images_tmp_path, "#{@base.path}-s#{@base.uniqueness_hash}.png"), @base.filename end - - it "should return the 'ten-by-ten' image" do + + def test_should_return_the_ten_by_ten_image assert_equal 'ten-by-ten', @base.image_for('ten-by-ten').name - assert @base.image_for('ten-by-ten').is_a?(Compass::SassExtensions::Sprites::Image) + assert @base.image_for('ten-by-ten').is_a?(Compass::Sprites::SassExtensions::Image) end - - %w(target hover active).each do |selector| - it "should have a #{selector}" do - assert @base.send(:"has_#{selector}?", 'ten-by-ten') - end - - it "should return #{selector} image class" do - assert_equal "ten-by-ten_#{selector}", @base.image_for('ten-by-ten').send(:"#{selector}").name - end - - it "should find file with '-' #{selector}" do - map = sprite_map_test(:seperator => '-') - map.images.each_index do |i| - if map.images[i].name != 'ten-by-ten' - name = map.images[i].name.gsub(/_/, '-') - map.images[i].stubs(:name).returns(name) - end - end - assert_equal "ten-by-ten-#{selector}", map.image_for('ten-by-ten').send(:"#{selector}").name - end - end + def test_should_have_selectors + %w(target hover active).each do |selector| + assert @base.send(:"has_#{selector}?", 'ten-by-ten') + assert_equal "ten-by-ten_#{selector}", @base.image_for('ten-by-ten').send(:"#{selector}").name + + map = sprite_map_test(:seperator => '-') + map.images.each_index do |i| + if map.images[i].name != 'ten-by-ten' + name = map.images[i].name.gsub(/_/, '-') + map.images[i].stubs(:name).returns(name) + end + end + assert_equal "ten-by-ten-#{selector}", map.image_for('ten-by-ten').send(:"#{selector}").name + end + end - it "should generate sprite" do + def test_should_generate_sprite @base.generate assert File.exists?(@base.filename) assert !@base.generation_required? assert !@base.outdated? end - - it "should remove old sprite when generating new" do + + def test_should_remove_old_sprite_when_generating_new @base.generate file = @base.filename assert File.exists?(file), "Original file does not exist" @@ -94,10 +88,8 @@ def teardown @base.generate assert !File.exists?(file), "Sprite file did not get removed" end - - test "should get correct relative_name" do - Compass.reset_configuration! - uri = 'foo/*.png' + + def test_should_get_correct_relative_name other_folder = File.join(@images_tmp_path, '../other-temp') FileUtils.mkdir_p other_folder FileUtils.mkdir_p File.join(other_folder, 'foo') @@ -108,40 +100,39 @@ def teardown 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')) + assert_equal 'foo/my.png', Compass::Sprites::SassExtensions::SpriteMap.relative_name(File.join(other_folder, 'foo/my.png')) FileUtils.rm_rf other_folder end - - test "should get correct relative_name for directories with similar names" do - Compass.reset_configuration! - uri = 'foo/*.png' + + def test_should_get_correct_relative_name_for_directories_with_similar_names other_folder = File.join(@images_tmp_path, '../other-temp') other_folder2 = File.join(@images_tmp_path, '../other-temp2') FileUtils.mkdir_p other_folder FileUtils.mkdir_p other_folder2 - + FileUtils.mkdir_p File.join(other_folder2, 'foo') %w(my bar).each do |file| FileUtils.touch(File.join(other_folder2, "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, other_folder2] Compass.add_configuration(config, "sprite_config") - assert_equal 'foo/my.png', Compass::SassExtensions::Sprites::SpriteMap.relative_name(File.join(other_folder2, 'foo/my.png')) + assert_equal 'foo/my.png', Compass::Sprites::SassExtensions::SpriteMap.relative_name(File.join(other_folder2, 'foo/my.png')) + ensure FileUtils.rm_rf other_folder FileUtils.rm_rf other_folder2 end - + test "should create map for nested" do - base = Compass::SassExtensions::Sprites::SpriteMap.from_uri OpenStruct.new(:value => 'nested/squares/*.png'), @base.instance_variable_get(:@evaluation_context), @options + base = Compass::Sprites::SassExtensions::SpriteMap.from_uri OpenStruct.new(:value => 'nested/squares/*.png'), @base.instance_variable_get(:@evaluation_context), @options assert_equal 'squares', base.name assert_equal 'nested/squares', base.path end - + test "should have correct position on ten-by-ten" do percent = Sass::Script::Number.new(50, ['%']) base = sprite_map_test(@options.merge('selectors_ten_by_ten_position' => percent)) @@ -149,8 +140,6 @@ def teardown 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') @@ -161,9 +150,8 @@ def teardown 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(@base, "foo/my.png", {}) + image = Compass::Sprites::SassExtensions::Image.new(@base, "foo/my.png", {}) assert_equal File.expand_path(File.join(other_folder, 'foo/my.png')), image.file assert_equal 0, image.size end - end diff --git a/test/common/helpers.rb b/test/common/helpers.rb new file mode 100644 index 0000000000..ee38a1386c --- /dev/null +++ b/test/common/helpers.rb @@ -0,0 +1,7 @@ +helpers_dir = File.join(File.expand_path('../', __FILE__), 'helpers') + +REPO_ROOT = File.expand_path('../../../', __FILE__) + +%w(io command_line test_case diff).each do |helper| + require File.join(helpers_dir, helper) +end diff --git a/test/common/helpers/command_line.rb b/test/common/helpers/command_line.rb new file mode 100644 index 0000000000..b5db9cbc46 --- /dev/null +++ b/test/common/helpers/command_line.rb @@ -0,0 +1,118 @@ +require 'timeout' + +module Compass + module Test + module CommandLineHelper + def compass(*arguments) + options = arguments.last.is_a?(Hash) ? arguments.pop : {} + options[:wait] = 0.25 + if block_given? + responder = Responder.new + yield responder + IO.popen("-", "r+") do |io| + if io + #parent process + output = "" + eof_at = nil + while !eof_at || (Time.now - eof_at < options[:wait]) + if io.eof? + eof_at ||= Time.now + sleep 0.1 + else + eof_at = nil + timeout(1) do + partial_output = io.readpartial(1024) + # puts "))))#{partial_output}(((((" + output << partial_output + end + prompt = output.split("\n").last.strip + if response = responder.response_for(prompt) + io.puts response + io.flush + end + end + end + responder.assert_required_responses! + @last_result = decolorize(output) + else + #child process + execute *arguments + end + end + else + @last_error = capture_warning do + @last_result = decolorize(capture_output do + @last_exit_code = execute *arguments + end) + end + end + rescue Timeout::Error + fail "Read from child process timed out" + end + + def decolorize(str) + str.gsub(/\e\[\d+m/,'') + end + + class Responder + Response = Struct.new(:prompt, :text, :required, :responded) + def initialize + @responses = [] + end + def respond_to(prompt, options = {}) + @responses << Response.new(prompt, options[:with], options[:required]) + end + def response_for(prompt) + response = @responses.detect do |r| + case r.prompt + when Regexp + prompt =~ r.prompt + when String + r.prompt == prompt + end + end + if response + response.responded = true + response.text + end + end + def assert_required_responses! + @responses.each do |response| + if response.required && !response.responded + raise "Prompt not encountered: \"#{response.prompt}\"" + end + end + end + end + + def assert_action_performed(action, path) + actions_found = [] + @last_result.split("\n").each do |line| + line = line.split + return if line.first == action.to_s && line.last == path + actions_found << line.first if line.last == path + end + message = "Action #{action.inspect} was not performed on: #{path}." + message += "The following actions were performed: #{actions_found.map{|a|a.inspect}.join(", ")}" if actions_found.any? + # puts @last_result + fail message + end + + def within_tmp_directory(dir = "tmp") + d = absolutize(dir) + FileUtils.mkdir_p(d) + Dir.chdir(d) do + yield + end + ensure + FileUtils.rm_rf(d) + end + + def execute(*arguments) + exit_code = Compass::Exec::SubCommandUI.new(arguments).run! + # fail "Command Failed with exit code: #{exit_code}" unless exit_code == 0 + exit_code + end + end + end +end diff --git a/test/common/helpers/diff.rb b/test/common/helpers/diff.rb new file mode 100644 index 0000000000..5964abd3a6 --- /dev/null +++ b/test/common/helpers/diff.rb @@ -0,0 +1,51 @@ +require 'diff/lcs' +require 'diff/lcs/hunk' +module Compass + module Test + module Diff + #stole this from rspec who stole this from the gem + def diff_as_string(data_old, data_new) + data_old = data_old.split(/\n/).map! { |e| e.chomp } + data_new = data_new.split(/\n/).map! { |e| e.chomp } + output = "" + diffs = ::Diff::LCS.diff(data_old, data_new) + return output if diffs.empty? + oldhunk = hunk = nil + file_length_difference = 0 + diffs.each do |piece| + begin + hunk = ::Diff::LCS::Hunk.new( + data_old, data_new, piece, context_lines, file_length_difference + ) + file_length_difference = hunk.file_length_difference + next unless oldhunk + # Hunks may overlap, which is why we need to be careful when our + # diff includes lines of context. Otherwise, we might print + # redundant lines. + if (context_lines > 0) and hunk.overlaps?(oldhunk) + hunk.unshift(oldhunk) + else + output << oldhunk.diff(format) + end + ensure + oldhunk = hunk + output << "\n" + end + end + #Handle the last remaining hunk + output << oldhunk.diff(format) << "\n" + end + + protected + + def format + :unified + end + + def context_lines + 3 + end + + end + end +end \ No newline at end of file diff --git a/test/common/helpers/io.rb b/test/common/helpers/io.rb new file mode 100644 index 0000000000..2891492cf7 --- /dev/null +++ b/test/common/helpers/io.rb @@ -0,0 +1,38 @@ +module Compass + module Test + module IoHelper + def capture_output + real_stdout, $stdout = $stdout, StringIO.new + yield + $stdout.string + ensure + $stdout = real_stdout + end + + def capture_warning + real_stderr, $stderr = $stderr, StringIO.new + yield + $stderr.string + ensure + $stderr = real_stderr + end + + def capture_pipe(io, options = {}) + options[:wait] = 0.25 + options[:timeout] = 1.0 + output = "" + eof_at = nil + while !eof_at || (Time.now - eof_at < options[:wait]) + if io.eof? + eof_at ||= Time.now + sleep 0.1 + else + eof_at = nil + timeout(options[:timeout]) { output << io.readpartial(1024) } + end + end + output + end + end + end +end diff --git a/test/common/helpers/test_case.rb b/test/common/helpers/test_case.rb new file mode 100644 index 0000000000..0907e0bd6a --- /dev/null +++ b/test/common/helpers/test_case.rb @@ -0,0 +1,65 @@ +module Compass + module Test + module TestCaseHelper + def absolutize(path) + if Compass::Util.blank?(path) + File.expand_path('../../', __FILE__) + elsif path[0] == ?/ + File.join(File.expand_path('../', __FILE__), path) + else + File.join(File.expand_path('../../', __FILE__), path) + end + end + + # compile a Sass string in the context of a project in the current working directory. + def compile_for_project(contents, options = {}) + Compass.add_project_configuration + Compass.configuration.add_import_path Compass::Sprites::Importer.new + options[:syntax] ||= :scss + Sass::Engine.new(contents, Compass.configuration.to_sass_engine_options.merge(options)).render + end + + def assert_correct(before, after) + if before == after + assert(true) + else + assert false, diff_as_string(before.inspect, after.inspect) + end + end + + module ClassMethods + + def let(method, &block) + define_method method, &block + end + + def it(name, &block) + test(name, &block) + end + + def test(name, &block) + define_method "test_#{underscore(name)}".to_sym, &block + end + + def setup(&block) + define_method :setup do + yield + end + end + + def after(&block) + define_method :teardown do + yield + end + end + + private + + def underscore(string) + string.gsub(' ', '_') + end + + end + end + end +end diff --git a/test_all.sh b/test_all.sh index 3202382652..4a78d9d198 100755 --- a/test_all.sh +++ b/test_all.sh @@ -1,6 +1,9 @@ #!/bin/sh cd core && bundle install --quiet && bundle exec rake test && cd .. && - cd cli && bundle install --quiet && bundle exec rake && cd .. && - cd import-once && bundle install --quiet && bundle exec rake test && cd .. && - cd import-once && (BUNDLE_GEMFILE=Gemfile_sass_3_2 bundle install --quiet && bundle exec rake test) && cd .. +cd cli && bundle install --quiet && bundle exec rake && cd .. && +cd cli && (BUNDLE_GEMFILE=gemfiles/sass_3_3.gemfile bundle install --quiet && bundle exec rake test) && cd .. +cd import-once && bundle install --quiet && bundle exec rake test && cd .. && +cd import-once && (BUNDLE_GEMFILE=Gemfile_sass_3_2 bundle install --quiet && bundle exec rake test) && cd .. +cd cli && bundle install --quiet && bundle exec rake && cd .. && +cd sprites && bundle install --quiet && bundle exec rake && cd ..