Add File.utime for updating files' mtime. Closes #28

Conflicts:

	lib/fakefs/file.rb
	test/fakefs_test.rb
This commit is contained in:
Scott Taylor 2010-12-27 12:01:45 -05:00
parent b6e5d10c29
commit d422c6a79f
3 changed files with 40 additions and 2 deletions

View File

@ -1,7 +1,7 @@
module FakeFS
class FakeFile
attr_accessor :name, :parent, :content
attr_reader :ctime, :mtime
attr_accessor :name, :parent, :content, :mtime
attr_reader :ctime
class Inode
def initialize(file_owner)

View File

@ -64,6 +64,18 @@ module FakeFS
end
end
def self.utime(atime, mtime, *paths)
paths.each do |path|
if exists?(path)
FileSystem.find(path).mtime = mtime
else
raise Errno::ENOENT
end
end
paths.size
end
def self.size(path)
read(path).length
end

View File

@ -384,6 +384,32 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal File.stat("foo").mtime, File.mtime("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')
end
end
def test_can_call_utime_on_an_existing_file
time = Time.now - 300 # Not now
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << ''
end
File.utime(time, time, path)
assert_equal time, File.mtime('/path/to/file.txt')
end
def test_utime_returns_number_of_paths
path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt'
[path1, path2].each do |path|
File.open(path, 'w') do |f|
f << ''
end
end
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
end
def test_can_read_with_File_readlines
path = '/path/to/file.txt'
File.open(path, 'w') do |f|