Permalink
Browse files

Merge pull request #108 from moretea/support_for_atime

Added support for atime
  • Loading branch information...
2 parents ec9c54e + 743477f commit 69e300abd47caf16115f1982723f24cc511f848c @rue rue committed Oct 25, 2011
Showing with 63 additions and 6 deletions.
  1. +2 −1 lib/fakefs/fake/dir.rb
  2. +2 −1 lib/fakefs/fake/file.rb
  3. +13 −2 lib/fakefs/file.rb
  4. +46 −2 test/fakefs_test.rb
View
@@ -1,13 +1,14 @@
module FakeFS
class FakeDir < Hash
attr_accessor :name, :parent
- attr_reader :ctime, :mtime
+ attr_reader :ctime, :mtime, :atime
def initialize(name = nil, parent = nil)
@name = name
@parent = parent
@ctime = Time.now
@mtime = @ctime
+ @atime = @ctime
end
def entry
View
@@ -1,6 +1,6 @@
module FakeFS
class FakeFile
- attr_accessor :name, :parent, :content, :mtime
+ attr_accessor :name, :parent, :content, :mtime, :atime
attr_reader :ctime
class Inode
@@ -34,6 +34,7 @@ def initialize(name = nil, parent = nil)
@inode = Inode.new(self)
@ctime = Time.now
@mtime = @ctime
+ @atime = @ctime
end
attr_accessor :inode
View
@@ -69,9 +69,18 @@ def self.ctime(path)
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)
paths.each do |path|
if exists?(path)
+ FileSystem.find(path).atime = atime
FileSystem.find(path).mtime = mtime
else
raise Errno::ENOENT
@@ -143,6 +152,7 @@ def self.readlink(path)
def self.read(path)
file = new(path)
if file.exists?
+ FileSystem.find(path).atime = Time.now
file.read
else
raise Errno::ENOENT
@@ -225,7 +235,7 @@ def self.split(path)
end
class Stat
- attr_reader :ctime, :mtime
+ attr_reader :ctime, :mtime, :atime
def initialize(file, __lstat = false)
if !File.exists?(file)
@@ -237,6 +247,7 @@ def initialize(file, __lstat = false)
@__lstat = __lstat
@ctime = @fake_file.ctime
@mtime = @fake_file.mtime
+ @atime = @fake_file.atime
end
def symlink?
@@ -326,7 +337,7 @@ def readpartial(maxlen, outbuf = nil)
end
def atime
- raise NotImplementedError
+ self.class.atime(@path)
end
def chmod(mode_int)
View
@@ -495,27 +495,45 @@ def test_can_return_ctime_on_existing_file
assert File.ctime('foo').is_a?(Time)
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" }
ctime = File.ctime("foo")
mtime = File.mtime("foo")
+ atime = File.atime("foo")
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
+ assert atime.is_a?(Time)
assert_equal ctime, mtime
+ assert_equal ctime, atime
File.open("foo", "r") do |f|
assert_equal ctime, f.ctime
assert_equal mtime, f.mtime
+ assert_equal atime, f.atime
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")
ctime = File.ctime("foo")
mtime = File.mtime("foo")
+ atime = File.atime("foo")
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
+ assert atime.is_a?(Time)
assert_equal ctime, mtime
+ assert_equal ctime, atime
end
def test_file_ctime_is_equal_to_file_stat_ctime
@@ -538,6 +556,16 @@ def test_directory_mtime_is_equal_to_directory_stat_mtime
assert_equal File.stat("foo").mtime, File.mtime("foo")
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
assert_raise Errno::ENOENT do
File.utime(Time.now, Time.now, '/path/to/file.txt')
@@ -552,6 +580,7 @@ def test_can_call_utime_on_an_existing_file
end
File.utime(time, time, path)
assert_equal time, File.mtime('file.txt')
+ assert_equal time, File.atime('file.txt')
end
def test_utime_returns_number_of_paths
@@ -564,6 +593,21 @@ def test_utime_returns_number_of_paths
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
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
path = 'file.txt'
File.open(path, 'w') do |f|

0 comments on commit 69e300a

Please sign in to comment.