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 }

This commit is contained in:
Víctor Martínez 2010-10-03 16:02:21 +02:00 committed by Scott Taylor
parent f59e9ba94e
commit 8354db1b77
4 changed files with 80 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|