diff --git a/lib/fakefs/fake/dir.rb b/lib/fakefs/fake/dir.rb index f72ea4f..068a427 100644 --- a/lib/fakefs/fake/dir.rb +++ b/lib/fakefs/fake/dir.rb @@ -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 diff --git a/lib/fakefs/fake/file.rb b/lib/fakefs/fake/file.rb index ec5086f..10bdf7f 100644 --- a/lib/fakefs/fake/file.rb +++ b/lib/fakefs/fake/file.rb @@ -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 @@ module FakeFS @inode = Inode.new(self) @ctime = Time.now @mtime = @ctime + @atime = @ctime end attr_accessor :inode diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 2ed69e2..5684ede 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -69,9 +69,18 @@ module FakeFS 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 @@ module FakeFS 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 @@ module FakeFS 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 @@ module FakeFS @__lstat = __lstat @ctime = @fake_file.ctime @mtime = @fake_file.mtime + @atime = @fake_file.atime end def symlink? @@ -326,7 +337,7 @@ module FakeFS end def atime - raise NotImplementedError + self.class.atime(@path) end def chmod(mode_int) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index ffb1169..6223b51 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -495,27 +495,45 @@ class FakeFSTest < Test::Unit::TestCase 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 @@ class FakeFSTest < Test::Unit::TestCase 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 @@ class FakeFSTest < Test::Unit::TestCase 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 @@ class FakeFSTest < Test::Unit::TestCase 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|