Permalink
Browse files

Add tolerance to missing files and logging

  • Loading branch information...
1 parent 0594a4c commit b14d72d349ee1c48d2821a15110b18c79f4a8bbc @fidothe fidothe committed Apr 14, 2011
Showing with 35 additions and 9 deletions.
  1. +8 −4 lib/cdn_helpers.rb
  2. +4 −1 lib/cdn_helpers/cdn_helpers.rake
  3. +23 −4 spec/cdn_helpers_spec.rb
View
@@ -11,12 +11,16 @@ def self.asset_cache
end
def self.call(path)
- hash_file(path, Rails.root.join('public'))
+ hash_file(path, Rails.root.join('public'), Rails.logger)
end
- def self.hash_file(path, public_root_path)
+ def self.hash_file(path, public_root_path, logger)
unless asset_cache.has_key?(path)
file_path = public_root_path.join(path.sub(/^\//, '')).to_s
+ unless File.file?(file_path)
+ logger.warn("Cannot rewrite URL for #{path}: File not found")
+ return path
+ end
sha1 = Digest::SHA1.file(file_path).hexdigest
extension = File.extname(path)
return asset_cache[path] = path + "/#{sha1[0..7]}#{extension}"
@@ -32,7 +36,7 @@ def self.rewrite_css_file(css_file_path, public_root_path, url_prefix = '/')
File.open(css_file_path, 'w') { |f| f.write(output) }
end
- def self.process_css_file(css_file, css_file_path, public_root_path, url_prefix = '/')
+ def self.process_css_file(logger, css_file, css_file_path, public_root_path, url_prefix = '/')
out_lines = []
css_file_path = Pathname.new(css_file_path).realpath
public_root_path = Pathname.new(public_root_path)
@@ -48,7 +52,7 @@ def self.process_css_file(css_file, css_file_path, public_root_path, url_prefix
local_url = local_url.to_s[(url_prefix.length - 1)..-1] if local_url.to_s.index(url_prefix) == 0
url = public_root_path.join(local_url[1..-1]).cleanpath.relative_path_from(public_root_path).to_s
end
- "url(#{CdnHelpers::AssetPath.hash_file("/" + url, public_root_path)})"
+ "url(#{CdnHelpers::AssetPath.hash_file("/" + url, public_root_path, logger)})"
else
"url(#{$1})"
end
@@ -3,13 +3,16 @@ namespace :cdn do
task :css_urls => :environment do
require 'cdn_helpers'
require 'pathname'
+ require 'logger'
begin
public_root_path = Pathname.new('public').realpath
+ logger = Logger.new(STDOUT)
+ logger.level = Logger::INFO
FileList['public/**/*.css'].each do |css_file_path|
css_file_path = Pathname.new(css_file_path).realpath
public_path = Rails.root.join('public')
- CdnHelpers::CssRewriter.rewrite_css_file(css_file_path, public_path)
+ CdnHelpers::CssRewriter.rewrite_css_file(css_file_path, public_path, logger)
end
rescue Errno::ENOENT
end
@@ -10,23 +10,42 @@ def public_root_path
Pathname.new(File.expand_path('fixtures', File.dirname(__FILE__)))
end
+ class MockLogger
+ attr_reader :logged
+
+ def initialize
+ @logged = []
+ end
+
+ def warn(msg)
+ @logged << msg
+ end
+ end
+
it "correctly rewrites a relative URL (no ../)" do
- CdnHelpers::CssRewriter.process_css_file(StringIO.new('background-image: url("eg.txt")'), css_path, public_root_path).
+ CdnHelpers::CssRewriter.process_css_file(MockLogger.new, StringIO.new('background-image: url("eg.txt")'), css_path, public_root_path).
should == 'background-image: url(/dir/eg.txt/1ba05a2d.txt)'
end
it "correctly rewrites a relative URL (with ../)" do
- CdnHelpers::CssRewriter.process_css_file(StringIO.new("background-image: url('../eg.txt')"), css_path, public_root_path).
+ CdnHelpers::CssRewriter.process_css_file(MockLogger.new, StringIO.new("background-image: url('../eg.txt')"), css_path, public_root_path).
should == 'background-image: url(/eg.txt/13c1f102.txt)'
end
it "correctly rewrites an absolute local URL (starts with /)" do
- CdnHelpers::CssRewriter.process_css_file(StringIO.new('background-image: url("/eg.txt")'), css_path, public_root_path).
+ CdnHelpers::CssRewriter.process_css_file(MockLogger.new, StringIO.new('background-image: url("/eg.txt")'), css_path, public_root_path).
should == 'background-image: url(/eg.txt/13c1f102.txt)'
end
it "correctly ignores an http:// url" do
- CdnHelpers::CssRewriter.process_css_file(StringIO.new('background-image: url("http://things.com/nice.png")'), css_path, public_root_path).
+ CdnHelpers::CssRewriter.process_css_file(MockLogger.new, StringIO.new('background-image: url("http://things.com/nice.png")'), css_path, public_root_path).
should == 'background-image: url(http://things.com/nice.png)'
end
+
+ it "logs and passes-through an unfound file" do
+ logger = MockLogger.new
+ CdnHelpers::CssRewriter.process_css_file(logger, StringIO.new('background-image: url("/nice.png")'), css_path, public_root_path).
+ should == 'background-image: url(/nice.png)'
+ logger.logged.should include("Cannot rewrite URL for /nice.png: File not found")
+ end
end

0 comments on commit b14d72d

Please sign in to comment.