diff --git a/lib/manifesto.rb b/lib/manifesto.rb index 9ab3b17..eb91f6a 100644 --- a/lib/manifesto.rb +++ b/lib/manifesto.rb @@ -30,7 +30,7 @@ def self.cache(options = {}) # files in the manifest. if File.file?(path) && File.basename(path)[0,1] != '.' if timestamp - manifest << "#{normalize_path(directory, path)}?#{File.mtime(path).to_i.to_s}\n" + manifest << "#{normalize_path(directory, path)}?#{get_timestamp(path)}\n" else manifest << "#{normalize_path(directory, path)}\n" end @@ -71,6 +71,11 @@ def self.normalize_path(directory, path) normalized_path = '/' + normalized_path unless normalized_path[0,1] == '/' normalized_path end + + # Calculates the timestamp for a given file using same methodology as Rails' asset timestamps. + def self.get_timestamp(path) + File.mtime(path).to_i.to_s + end # Checks that the options passed to the cache method are valid. def self.validate_options(directory, compute_hash, timestamp) diff --git a/spec/manifesto_spec.rb b/spec/manifesto_spec.rb index 8abc5f6..75dddc5 100644 --- a/spec/manifesto_spec.rb +++ b/spec/manifesto_spec.rb @@ -3,11 +3,15 @@ describe Manifesto do describe ".validate_options" do it "should raise ArgumentError if directory is not a real directory" do - expect{ Manifesto.validate_options('', false) }.to raise_error(ArgumentError) + expect{ Manifesto.validate_options('', false, false) }.to raise_error(ArgumentError) end it "should raise ArgumentError if compute_hash is not a boolean" do - expect{ Manifesto.validate_options('.', nil) }.to raise_error(ArgumentError) + expect{ Manifesto.validate_options('.', nil, false) }.to raise_error(ArgumentError) + end + + it "should raise ArgumentError if timestamp is not a boolean" do + expect{ Manifesto.validate_options('.', false, nil) }.to raise_error(ArgumentError) end end @@ -40,6 +44,11 @@ Manifesto.stub!(:get_file_paths).and_return([]) end + it "should not get the timestamp" do + Manifesto.should_receive(:get_timestamp).never + Manifesto.cache + end + it "should not compute hash" do Manifesto.should_receive(:compute_file_contents_hash).never Manifesto.cache @@ -58,6 +67,11 @@ File.stub!(:file?).and_return(false) end + it "should not get the timestamp" do + Manifesto.should_receive(:get_timestamp).never + Manifesto.cache + end + it "should not compute hash" do Manifesto.should_receive(:compute_file_contents_hash).never Manifesto.cache @@ -75,6 +89,11 @@ File.stub!(:file?).and_return(true) end + it "should not get the timestamp" do + Manifesto.should_receive(:get_timestamp).never + Manifesto.cache + end + it "should not compute hash" do Manifesto.should_receive(:compute_file_contents_hash).never Manifesto.cache @@ -93,6 +112,11 @@ Manifesto.stub!(:compute_file_contents_hash).and_return('asdfsafasdfsdfszxsd') end + it "should get the timestamp" do + Manifesto.should_receive(:get_timestamp).and_return('anything') + Manifesto.cache + end + it "should compute the hash" do Manifesto.should_receive(:compute_file_contents_hash).and_return('anything') Manifesto.cache