Skip to content

Commit

Permalink
Merge branch 'master' into sass
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 10, 2012
2 parents da6bdd3 + 2997be9 commit 19a8796
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/rake/sprocketstask.rb
Expand Up @@ -49,6 +49,9 @@ def environment
#
attr_accessor :assets

# Number of old assets to keep.
attr_accessor :keep

# Logger to use during rake tasks. Defaults to using stderr.
#
# t.logger = Logger.new($stdout)
Expand Down Expand Up @@ -78,6 +81,7 @@ def initialize(name = :assets)
@environment = lambda { Sprockets::Environment.new(Dir.pwd) }
@logger = Logger.new($stderr)
@logger.level = Logger::INFO
@keep = 2

yield self if block_given?

Expand Down Expand Up @@ -105,7 +109,7 @@ def define
desc name == :assets ? "Clean old assets" : "Clean old #{name} assets"
task "clean_#{name}" do
with_logger do
manifest.clean
manifest.clean(keep)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/sprockets/context.rb
Expand Up @@ -179,7 +179,9 @@ def evaluate(path, options = {})
result = options[:data]
else
if environment.respond_to?(:default_external_encoding)
result = Sprockets::Utils.read_unicode(pathname, environment.default_external_encoding)
mime_type = environment.mime_types(pathname.extname)
encoding = environment.encoding_for_mime_type(mime_type)
result = Sprockets::Utils.read_unicode(pathname, encoding)
else
result = Sprockets::Utils.read_unicode(pathname)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/directive_processor.rb
Expand Up @@ -260,7 +260,7 @@ def process_require_directory_directive(path = ".")
root = pathname.dirname.join(path).expand_path

unless (stats = stat(root)) && stats.directory?
raise ArgumentError, "require_tree argument must be a directory"
raise ArgumentError, "require_directory argument must be a directory"
end

context.depend_on(root)
Expand Down
10 changes: 10 additions & 0 deletions lib/sprockets/mime.rb
Expand Up @@ -30,6 +30,16 @@ def register_mime_type(mime_type, ext)
ext = Sprockets::Utils.normalize_extension(ext)
@mime_types[ext] = mime_type
end

if defined? Encoding
# Returns the correct encoding for a given mime type, while falling
# back on the default external encoding, if it exists.
def encoding_for_mime_type(type)
encoding = Encoding::BINARY if type =~ %r{^(image|audio|video)/}
encoding ||= default_external_encoding if respond_to?(:default_external_encoding)
encoding
end
end
end

# Extend Sprockets module to provide global registry
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/version.rb
@@ -1,3 +1,3 @@
module Sprockets
VERSION = "2.2.0.beta"
VERSION = "2.2.0"
end
Binary file added test/fixtures/encoding/binary.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/test_encoding.rb
Expand Up @@ -38,6 +38,21 @@ def setup
data
assert_equal Encoding.find('UTF-8'), data.encoding
end

test "read static BINARY asset" do
data = @env['binary.png'].to_s
assert_equal "\x89PNG\r\n\x1A\n\x00\x00\x00".force_encoding("BINARY"),
data[0..10]
assert_equal Encoding.find('BINARY'), data.encoding
end

test "read processed BINARY asset" do
@env.register_postprocessor('image/png', :noop_processor) { |context, data| data }
data = @env['binary.png'].to_s
assert_equal "\x89PNG\r\n\x1A\n\x00\x00\x00".force_encoding("BINARY"),
data[0..10]
assert_equal Encoding.find('BINARY'), data.encoding
end
else
test "read ASCII asset" do
assert_equal "var foo = \"bar\";\n", @env['ascii.js'].to_s
Expand All @@ -61,5 +76,16 @@ def setup
assert_equal "var foo = \"bar\";\nvar snowman = \"\342\230\203\";\n\n\n",
@env['ascii_utf8.js'].to_s
end

test "read static BINARY asset" do
data = @env['binary.png'].to_s
assert_equal "\x89PNG\r\n\x1A\n\x00\x00\x00", data[0..10]
end

test "read processed BINARY asset" do
@env.register_postprocessor('image/png', :noop_processor) { |context, data| data }
data = @env['binary.png'].to_s
assert_equal "\x89PNG\r\n\x1A\n\x00\x00\x00", data[0..10]
end
end
end
3 changes: 2 additions & 1 deletion test/test_server.rb
Expand Up @@ -121,7 +121,8 @@ def app
get "/cached/javascripts/application.js"
time_after_touching = last_response.headers['Last-Modified']

assert_equal time_before_touching, time_after_touching
# TODO: CI doesn't like this
# assert_equal time_before_touching, time_after_touching
end

test "not modified partial response when etags match" do
Expand Down

0 comments on commit 19a8796

Please sign in to comment.