Skip to content

Commit

Permalink
Moved timestamp logic into separate function for better rspec testing.
Browse files Browse the repository at this point in the history
Updated rspec tests to include new functionality.
  • Loading branch information
Jordan MacDonald committed May 31, 2011
1 parent 482c2c5 commit eb3f5d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/manifesto.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 <tt>cache</tt> method are valid.
def self.validate_options(directory, compute_hash, timestamp)
Expand Down
28 changes: 26 additions & 2 deletions spec/manifesto_spec.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit eb3f5d9

Please sign in to comment.