diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index e7ce422..793819c 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -50,6 +50,10 @@ module FakeFS read(path).length end + def self.size?(path) + exists?(path) && !size(path).zero? + end + def self.const_missing(name) RealFile.const_get(name) end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index c537cc1..ae46b4b 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -208,6 +208,23 @@ class FakeFSTest < Test::Unit::TestCase assert_equal 9, File.size(path) end + def test_can_check_if_file_has_size? + path = '/path/to/file.txt' + File.open(path, 'w') do |f| + f << 'Yada Yada' + end + assert File.size?(path) + assert ! File.size?("/path/to/other.txt") + end + + def test_can_check_size?_of_empty_file + path = '/path/to/file.txt' + File.open(path, 'w') do |f| + f << '' + end + assert ! File.size?("/path/to/file.txt") + end + def test_can_read_with_File_readlines path = '/path/to/file.txt' File.open(path, 'w') do |f|