From ba0b8a00356b4c854930a8e849b5629d51ffd70f Mon Sep 17 00:00:00 2001 From: "James A. Rosen" Date: Tue, 27 Mar 2012 16:18:31 -0700 Subject: [PATCH] CacheBustingFilter: better handling of dots in file names --- .../cache_buster_filter.rb | 3 +- spec/cache_buster_filter_spec.rb | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/rake-pipeline-web-filters/cache_buster_filter.rb b/lib/rake-pipeline-web-filters/cache_buster_filter.rb index 2db1daa..4303685 100644 --- a/lib/rake-pipeline-web-filters/cache_buster_filter.rb +++ b/lib/rake-pipeline-web-filters/cache_buster_filter.rb @@ -25,7 +25,8 @@ def initialize(&key_generator) key_generator ||= DEFAULT_KEY_GENERATOR output_name_generator = proc { |path, file| parts = path.split('.') - parts[0] << "-#{key_generator.call(file)}" + index_to_modify = parts.length > 1 ? -2 : -1 + parts[index_to_modify] << "-#{key_generator.call(file)}" parts.join('.') } super(&output_name_generator) diff --git a/spec/cache_buster_filter_spec.rb b/spec/cache_buster_filter_spec.rb index cb8f886..d13e05c 100644 --- a/spec/cache_buster_filter_spec.rb +++ b/spec/cache_buster_filter_spec.rb @@ -69,4 +69,37 @@ def setup_filter(filter) end end + + describe 'for an input file with multiple dots' do + let(:input_file) { + MemoryFileWrapper.new '/path/to/input', 'my.text.file.txt', 'UTF-8', content + } + + let(:output_file) { + MemoryFileWrapper.new output_root, "my.text.file-foo.txt", 'UTF-8' + } + + subject { setup_filter(CacheBusterFilter.new() { 'foo' }) } + + it 'appends the busting key to the penultimate part' do + subject.output_files.should == [ output_file ] + end + end + + describe 'for an input file with no dots' do + let(:input_file) { + MemoryFileWrapper.new '/path/to/input', 'my_text_file', 'UTF-8', content + } + + let(:output_file) { + MemoryFileWrapper.new output_root, "my_text_file-foo", 'UTF-8' + } + + subject { setup_filter(CacheBusterFilter.new() { 'foo' }) } + + it 'appends the busting key to the end of the filename' do + subject.output_files.should == [ output_file ] + end + end + end