Skip to content

Commit

Permalink
[change] 'Erubis::Engine.load_file()' to set cache timestamp to be th…
Browse files Browse the repository at this point in the history
…e same value as original file
  • Loading branch information
kwatch committed Mar 21, 2011
1 parent 2c7f043 commit 5f4e4cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/erubis/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def convert!(input)
def self.load_file(filename, properties={})
cachename = properties[:cachename] || (filename + '.cache')
properties[:filename] = filename
if test(?f, cachename) && File.mtime(filename) <= File.mtime(cachename)
timestamp = File.mtime(filename)
if test(?f, cachename) && timestamp == File.mtime(cachename)
engine = self.new(nil, properties)
engine.src = File.read(cachename)
else
Expand All @@ -57,6 +58,7 @@ def self.load_file(filename, properties={})
tmpname = cachename + rand().to_s[1,8]
File.open(tmpname, 'wb') {|f| f.write(engine.src) }
File.rename(tmpname, cachename)
File.utime(timestamp, timestamp, cachename)
end
engine.src.untaint # ok?
return engine
Expand Down
20 changes: 12 additions & 8 deletions test/test-erubis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,36 @@ def test_load_file_cache1
filename = 'tmp.load_file_timestamp1'
cachename = filename + '.cache'
begin
## when cache doesn't exist then it is created automatically
File.open(filename, 'w') { |f| f.write(@input) }
assert_block() { !test(?f, cachename) }
mtime = Time.now - 2.0
File.utime(mtime, mtime, filename)
!test(?f, cachename) or raise "** failed"
engine = @klass.load_file(filename)
assert_block() { test(?f, cachename) }
assert_block() { File.mtime(filename) <= File.mtime(cachename) }
assert_text_equal(@src, engine.src)
#
## when cache has different timestamp then it is recreated
input2 = @input.gsub(/ul>/, 'ol>')
src2 = @src.gsub(/ul>/, 'ol>')
mtime = File.mtime(filename)
File.open(filename, 'w') { |f| f.write(input2) }
t1 = Time.now()
sleep(1)
t2 = Time.now()
#
File.utime(t1, t1, filename)
File.utime(t2, t2, cachename)
assert_block('cache should be newer') { File.mtime(filename) < File.mtime(cachename) }
File.mtime(filename) < File.mtime(cachename) or raise "** failed"
engine = @klass.load_file(filename)
assert_block('cache should be newer') { File.mtime(filename) < File.mtime(cachename) }
assert_text_equal(@src, engine.src)
assert_block('cache should have same timestamp') { File.mtime(filename) == File.mtime(cachename) }
#assert_text_equal(@src, engine.src)
assert_text_equal(src2, engine.src)
#
File.utime(t2, t2, filename)
File.utime(t1, t1, cachename)
assert_block('cache should be older') { File.mtime(filename) > File.mtime(cachename) }
File.mtime(filename) > File.mtime(cachename) or raise "** failed"
engine = @klass.load_file(filename)
assert_block('cache should be newer') { File.mtime(filename) <= File.mtime(cachename) }
assert_block('cache should have same timestamp') { File.mtime(filename) == File.mtime(cachename) }
assert_text_equal(src2, engine.src)
ensure
File.unlink(cachename) if File.file?(cachename)
Expand Down

0 comments on commit 5f4e4cd

Please sign in to comment.