diff --git a/lib/erubis/engine.rb b/lib/erubis/engine.rb index 0ef9a47..1df7e28 100644 --- a/lib/erubis/engine.rb +++ b/lib/erubis/engine.rb @@ -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 @@ -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 diff --git a/test/test-erubis.rb b/test/test-erubis.rb index a1cb4c2..ad00e5e 100644 --- a/test/test-erubis.rb +++ b/test/test-erubis.rb @@ -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)