From 8354db1b7729438cfd67a264bb8a46d822071bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Sun, 3 Oct 2010 16:02:21 +0200 Subject: [PATCH] Implemented File.ctime, File.stat(file).ctime, File.open(file){|f| f.ctime }, File.mtime, File.stat(file).mtime and File.open(file){|f| f.mtime } --- lib/fakefs/fake/dir.rb | 5 +++- lib/fakefs/fake/file.rb | 6 +++-- lib/fakefs/file.rb | 24 +++++++++++++----- test/fakefs_test.rb | 54 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 9 deletions(-) diff --git a/lib/fakefs/fake/dir.rb b/lib/fakefs/fake/dir.rb index c75a57b..f72ea4f 100644 --- a/lib/fakefs/fake/dir.rb +++ b/lib/fakefs/fake/dir.rb @@ -1,10 +1,13 @@ module FakeFS class FakeDir < Hash attr_accessor :name, :parent + attr_reader :ctime, :mtime def initialize(name = nil, parent = nil) - @name = name + @name = name @parent = parent + @ctime = Time.now + @mtime = @ctime end def entry diff --git a/lib/fakefs/fake/file.rb b/lib/fakefs/fake/file.rb index e31c8fe..f480fe5 100644 --- a/lib/fakefs/fake/file.rb +++ b/lib/fakefs/fake/file.rb @@ -1,6 +1,7 @@ module FakeFS class FakeFile - attr_accessor :name, :parent, :content, :mtime + attr_accessor :name, :parent, :content + attr_reader :ctime, :mtime class Inode def initialize(file_owner) @@ -31,7 +32,8 @@ module FakeFS @name = name @parent = parent @inode = Inode.new(self) - @mtime = Time.now + @ctime = Time.now + @mtime = @ctime end attr_accessor :inode diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index d9dac53..37645d5 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -56,6 +56,14 @@ module FakeFS end end + def self.ctime(path) + if exists?(path) + FileSystem.find(path).ctime + else + raise Errno::ENOENT + end + end + def self.size(path) read(path).length end @@ -179,13 +187,17 @@ module FakeFS end class Stat + attr_reader :ctime, :mtime + def initialize(file, __lstat = false) if !File.exists?(file) raise(Errno::ENOENT, "No such file or directory - #{file}") end - @file = file - @__lstat = __lstat + @file = file + @fake_file = FileSystem.find(@file) + @__lstat = __lstat + @ctime, @mtime = @fake_file.ctime, @fake_file.mtime end def symlink? @@ -197,12 +209,12 @@ module FakeFS end def nlink - FileSystem.find(@file).links.size + @fake_file.links.size end def size if @__lstat && symlink? - FileSystem.find(@file).target.size + @fake_file.target.size else File.size(@file) end @@ -286,7 +298,7 @@ module FakeFS end def ctime - raise NotImplementedError + self.class.ctime(@path) end def flock(locking_constant) @@ -294,7 +306,7 @@ module FakeFS end def mtime - raise NotImplementedError + self.class.mtime(@path) end if RUBY_VERSION.to_f >= 1.9 diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index c3fa6de..fdb0309 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -330,6 +330,60 @@ class FakeFSTest < Test::Unit::TestCase assert File.mtime('/path/to/file.txt').is_a?(Time) end + def test_raises_error_on_ctime_if_file_does_not_exist + assert_raise Errno::ENOENT do + File.ctime('/path/to/file.txt') + end + end + + def test_can_return_ctime_on_existing_file + File.open("foo", "w") { |f| f << "some content" } + assert File.ctime('foo').is_a?(Time) + end + + def test_ctime_and_mtime_are_equal_for_new_files + File.open("foo", "w") { |f| f << "some content" } + ctime = File.ctime("foo") + mtime = File.mtime("foo") + assert ctime.is_a?(Time) + assert mtime.is_a?(Time) + assert_equal ctime, mtime + + File.open("foo", "r") do |f| + assert_equal ctime, f.ctime + assert_equal mtime, f.mtime + end + end + + def test_ctime_and_mtime_are_equal_for_new_directories + FileUtils.mkdir_p("foo") + ctime = File.ctime("foo") + mtime = File.mtime("foo") + assert ctime.is_a?(Time) + assert mtime.is_a?(Time) + assert_equal ctime, mtime + end + + def test_file_ctime_is_equal_to_file_stat_ctime + File.open("foo", "w") { |f| f << "some content" } + assert_equal File.stat("foo").ctime, File.ctime("foo") + end + + def test_directory_ctime_is_equal_to_directory_stat_ctime + FileUtils.mkdir_p("foo") + assert_equal File.stat("foo").ctime, File.ctime("foo") + end + + def test_file_mtime_is_equal_to_file_stat_mtime + File.open("foo", "w") { |f| f << "some content" } + assert_equal File.stat("foo").mtime, File.mtime("foo") + end + + def test_directory_mtime_is_equal_to_directory_stat_mtime + FileUtils.mkdir_p("foo") + assert_equal File.stat("foo").mtime, File.mtime("foo") + end + def test_can_read_with_File_readlines path = '/path/to/file.txt' File.open(path, 'w') do |f|