Add support for atime

This commit is contained in:
Maarten Hoogendoorn 2011-10-25 16:44:13 +02:00
parent ec9c54e373
commit 743477f4f4
4 changed files with 63 additions and 6 deletions

View File

@ -1,13 +1,14 @@
module FakeFS module FakeFS
class FakeDir < Hash class FakeDir < Hash
attr_accessor :name, :parent attr_accessor :name, :parent
attr_reader :ctime, :mtime attr_reader :ctime, :mtime, :atime
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)
@name = name @name = name
@parent = parent @parent = parent
@ctime = Time.now @ctime = Time.now
@mtime = @ctime @mtime = @ctime
@atime = @ctime
end end
def entry def entry

View File

@ -1,6 +1,6 @@
module FakeFS module FakeFS
class FakeFile class FakeFile
attr_accessor :name, :parent, :content, :mtime attr_accessor :name, :parent, :content, :mtime, :atime
attr_reader :ctime attr_reader :ctime
class Inode class Inode
@ -34,6 +34,7 @@ module FakeFS
@inode = Inode.new(self) @inode = Inode.new(self)
@ctime = Time.now @ctime = Time.now
@mtime = @ctime @mtime = @ctime
@atime = @ctime
end end
attr_accessor :inode attr_accessor :inode

View File

@ -69,9 +69,18 @@ module FakeFS
end end
end end
def self.atime(path)
if exists?(path)
FileSystem.find(path).atime
else
raise Errno::ENOENT
end
end
def self.utime(atime, mtime, *paths) def self.utime(atime, mtime, *paths)
paths.each do |path| paths.each do |path|
if exists?(path) if exists?(path)
FileSystem.find(path).atime = atime
FileSystem.find(path).mtime = mtime FileSystem.find(path).mtime = mtime
else else
raise Errno::ENOENT raise Errno::ENOENT
@ -143,6 +152,7 @@ module FakeFS
def self.read(path) def self.read(path)
file = new(path) file = new(path)
if file.exists? if file.exists?
FileSystem.find(path).atime = Time.now
file.read file.read
else else
raise Errno::ENOENT raise Errno::ENOENT
@ -225,7 +235,7 @@ module FakeFS
end end
class Stat class Stat
attr_reader :ctime, :mtime attr_reader :ctime, :mtime, :atime
def initialize(file, __lstat = false) def initialize(file, __lstat = false)
if !File.exists?(file) if !File.exists?(file)
@ -237,6 +247,7 @@ module FakeFS
@__lstat = __lstat @__lstat = __lstat
@ctime = @fake_file.ctime @ctime = @fake_file.ctime
@mtime = @fake_file.mtime @mtime = @fake_file.mtime
@atime = @fake_file.atime
end end
def symlink? def symlink?
@ -326,7 +337,7 @@ module FakeFS
end end
def atime def atime
raise NotImplementedError self.class.atime(@path)
end end
def chmod(mode_int) def chmod(mode_int)

View File

@ -495,27 +495,45 @@ class FakeFSTest < Test::Unit::TestCase
assert File.ctime('foo').is_a?(Time) assert File.ctime('foo').is_a?(Time)
end end
def test_ctime_and_mtime_are_equal_for_new_files def test_raises_error_on_atime_if_file_does_not_exist
assert_raise Errno::ENOENT do
File.atime('file.txt')
end
end
def test_can_return_atime_on_existing_file
File.open("foo", "w") { |f| f << "some content" }
assert File.atime('foo').is_a?(Time)
end
def test_ctime_mtime_and_atime_are_equal_for_new_files
File.open("foo", "w") { |f| f << "some content" } File.open("foo", "w") { |f| f << "some content" }
ctime = File.ctime("foo") ctime = File.ctime("foo")
mtime = File.mtime("foo") mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time) assert ctime.is_a?(Time)
assert mtime.is_a?(Time) assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime assert_equal ctime, mtime
assert_equal ctime, atime
File.open("foo", "r") do |f| File.open("foo", "r") do |f|
assert_equal ctime, f.ctime assert_equal ctime, f.ctime
assert_equal mtime, f.mtime assert_equal mtime, f.mtime
assert_equal atime, f.atime
end end
end end
def test_ctime_and_mtime_are_equal_for_new_directories def test_ctime_mtime_and_atime_are_equal_for_new_directories
FileUtils.mkdir_p("foo") FileUtils.mkdir_p("foo")
ctime = File.ctime("foo") ctime = File.ctime("foo")
mtime = File.mtime("foo") mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time) assert ctime.is_a?(Time)
assert mtime.is_a?(Time) assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime assert_equal ctime, mtime
assert_equal ctime, atime
end end
def test_file_ctime_is_equal_to_file_stat_ctime def test_file_ctime_is_equal_to_file_stat_ctime
@ -538,6 +556,16 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal File.stat("foo").mtime, File.mtime("foo") assert_equal File.stat("foo").mtime, File.mtime("foo")
end end
def test_file_atime_is_equal_to_file_stat_atime
File.open("foo", "w") { |f| f << "some content" }
assert_equal File.stat("foo").atime, File.atime("foo")
end
def test_directory_atime_is_equal_to_directory_stat_atime
FileUtils.mkdir_p("foo")
assert_equal File.stat("foo").atime, File.atime("foo")
end
def test_utime_raises_error_if_path_does_not_exist def test_utime_raises_error_if_path_does_not_exist
assert_raise Errno::ENOENT do assert_raise Errno::ENOENT do
File.utime(Time.now, Time.now, '/path/to/file.txt') File.utime(Time.now, Time.now, '/path/to/file.txt')
@ -552,6 +580,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
File.utime(time, time, path) File.utime(time, time, path)
assert_equal time, File.mtime('file.txt') assert_equal time, File.mtime('file.txt')
assert_equal time, File.atime('file.txt')
end end
def test_utime_returns_number_of_paths def test_utime_returns_number_of_paths
@ -564,6 +593,21 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 2, File.utime(Time.now, Time.now, path1, path2) assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
end end
def test_file_a_time_updated_when_file_is_read
old_atime = Time.now - 300
path = "file.txt"
File.open(path, "w") do |f|
f << "Hello"
end
File.utime(old_atime, File.mtime(path), path)
assert_equal File.atime(path), old_atime
File.read(path)
assert File.atime(path) != old_atime
end
def test_can_read_with_File_readlines def test_can_read_with_File_readlines
path = 'file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|