Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
Wrote first specs for filter
Browse files Browse the repository at this point in the history
  • Loading branch information
avdgaag committed May 20, 2011
1 parent 11be2d1 commit b19acc2
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--format documentation --color -r spec/spec_helper.rb
8 changes: 5 additions & 3 deletions lib/nanoc3/filters/cache_buster.rb
@@ -1,3 +1,5 @@
require 'digest'

module Nanoc3
module Filters

Expand All @@ -23,7 +25,7 @@ class CacheBuster < Nanoc3::Filter
REGEX_CSS = /url\(('|"|)(([^'")]+)\.(#{EXTENSIONS.join('|')}))\1\)/i

# Regex for finding all references to files to be cache busted in HTML files
REGEX_HTML = /(href|src)="([^"]+(\.(?:#{EXTENSIONS.join('|')})))"/
REGEX_HTML = /(href|src)=("|'|)([^'"]+(\.(?:#{EXTENSIONS.join('|')})))\2/

# Custom exception that can be raised by #source_path
NoSuchSourceFile = Class.new(Exception)
Expand Down Expand Up @@ -113,10 +115,10 @@ def bust_stylesheet(content)
# @return <String> rewritten content
def bust_page(content)
content.gsub(REGEX_HTML) do |m|
attribute, path, extension = $1, $2, $3
attribute, quote, path, extension = $1, $2, $3, $4
begin
real_path = content_path(source_path(path))
%Q{#{attribute}="#{path.sub(extension, self.class.hash(real_path) + extension)}"}
%Q{#{attribute}=#{quote}#{path.sub(extension, self.class.hash(real_path) + extension)}#{quote}}
rescue NoSuchSourceFile, NoCacheBusting
m
end
Expand Down
123 changes: 123 additions & 0 deletions spec/nanoc3/filters/cache_buster_spec.rb
@@ -0,0 +1,123 @@
require 'ostruct'

describe Nanoc3::Filters::CacheBuster do
before(:each) do
@item = { :extension => 'css', :content_filename => 'styles.css' }
@context = {
:site => OpenStruct.new,
:item => @item,
:config => { :filter_extensions => { :scss => 'css' }},
:content => 'example content',
:items => [@item]
}
@context[:site].config = @context[:config]
@context[:site].items = @context[:items]
Digest::MD5.stub!(:hexdigest).and_return('123456789')
end

let(:subject) { Nanoc3::Filters::CacheBuster.new @context }

describe 'filter interface' do
it { should be_kind_of(Nanoc3::Filter) }
it { should respond_to(:run) }

it 'should accept a string and an options Hash' do
lambda { subject.run('foo', {}) }.should_not raise_error(ArgumentError)
end
end

def self.it_should_filter(replacements = {})
replacements.each do |original, busted|
it 'should add cache buster to reference' do
@context[:content] = original
subject.run(original).should == busted
end
end
end

def self.it_should_not_filter(str)
it 'should not change the reference' do
@context[:content] = str
subject.run(str).should == str
end
end

describe 'filtering CSS' do
before(:each) do
@item[:extension] = 'css'
@item.stub!(:path).and_return('./foo-cb123456789.png')
File.stub!(:read).with(File.join(Dir.pwd, 'content', 'styles.css')).and_return(@context[:content])
end

describe 'without quotes' do
it_should_filter %Q{background: url(foo.png);} => 'background: url(foo-cb123456789.png);'
end

describe 'with single quotes' do
it_should_filter %Q{background: url('foo.png');} => %Q{background: url('foo-cb123456789.png');}
end

describe 'with double quotes' do
it_should_filter %Q{background: url("foo.png");} => %Q{background: url("foo-cb123456789.png");}
end

describe 'when the file does not exist' do
before(:each) do
@item.stub!(:path).and_return('bar.png')
end

it_should_not_filter %Q{background: url(foo.png);}
end

describe 'when the file is not cache busted' do
before(:each) do
@item.stub!(:path).and_return('foo.png')
end

it_should_not_filter %Q{background: url(foo.png);}
end
end

describe 'filtering HTML' do
before(:each) do
@item[:content_filename] = 'page.html'
@item[:extension] = 'html'
end

describe 'on the src attribute' do
before(:each) do
@context[:content] = '<img src="foo.png">'
@item.stub!(:path).and_return('./foo-cb123456789.png')
File.stub!(:read).with(File.join(Dir.pwd, 'content', 'page.html')).and_return(@context[:content])
end

describe 'without quotes' do
it_should_filter %Q{<img src=foo.png>} => %Q{<img src=foo-cb123456789.png>}
end

describe 'with single quotes' do
it_should_filter %Q{<img src='foo.png'>} => %Q{<img src='foo-cb123456789.png'>}
end

describe 'with double quotes' do
it_should_filter %Q{<img src="foo.png">} => %Q{<img src="foo-cb123456789.png">}
end

describe 'when the file does not exist' do
before(:each) do
@item.stub!(:path).and_return('bar.png')
end

it_should_not_filter '<img src="foo.png">'
end

describe 'when the file is not cache busted' do
before(:each) do
@item.stub!(:path).and_return('foo.png')
end

it_should_not_filter '<img src="foo.png">'
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'nanoc_cachebuster'

0 comments on commit b19acc2

Please sign in to comment.