Add File::lstat, #lstat. Report the correct size for symlinks when using lstat. Closes Github #25
This commit is contained in:
parent
01735bb17a
commit
53b74cf6c8
@ -172,13 +172,18 @@ module FakeFS
|
||||
File::Stat.new(file)
|
||||
end
|
||||
|
||||
def self.lstat(file)
|
||||
File::Stat.new(file, true)
|
||||
end
|
||||
|
||||
class Stat
|
||||
def initialize(file)
|
||||
def initialize(file, __lstat = false)
|
||||
if !File.exists?(file)
|
||||
raise(Errno::ENOENT, "No such file or directory - #{file}")
|
||||
end
|
||||
|
||||
@file = file
|
||||
@__lstat = __lstat
|
||||
end
|
||||
|
||||
def symlink?
|
||||
@ -194,9 +199,13 @@ module FakeFS
|
||||
end
|
||||
|
||||
def size
|
||||
if @__lstat && symlink?
|
||||
FileSystem.find(@file).target.size
|
||||
else
|
||||
File.size(@file)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :path
|
||||
|
||||
@ -236,7 +245,11 @@ module FakeFS
|
||||
end
|
||||
|
||||
def stat
|
||||
raise NotImplementedError
|
||||
self.class.stat(@path)
|
||||
end
|
||||
|
||||
def lstat
|
||||
self.class.lstat(@path)
|
||||
end
|
||||
|
||||
def sysseek(position, whence = SEEK_SET)
|
||||
@ -278,10 +291,6 @@ module FakeFS
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def lstat
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def mtime
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
59
test/fake/file/lstat_test.rb
Normal file
59
test/fake/file/lstat_test.rb
Normal file
@ -0,0 +1,59 @@
|
||||
require "test_helper"
|
||||
|
||||
class FileStat < Test::Unit::TestCase
|
||||
def setup
|
||||
FakeFS.activate!
|
||||
FakeFS::FileSystem.clear
|
||||
end
|
||||
|
||||
def teardown
|
||||
FakeFS.deactivate!
|
||||
end
|
||||
|
||||
def test_calling_lstat_should_create_a_new_file_stat_object
|
||||
File.open("foo", "w") do |f|
|
||||
f << "bar"
|
||||
end
|
||||
|
||||
File.open("foo") do |f|
|
||||
assert_equal File::Stat, f.lstat.class
|
||||
end
|
||||
end
|
||||
|
||||
def test_lstat_should_use_correct_file
|
||||
File.open("bar", "w") do |f|
|
||||
f << "1"
|
||||
end
|
||||
|
||||
File.open("bar") do |f|
|
||||
assert_equal 1, f.lstat.size
|
||||
end
|
||||
end
|
||||
|
||||
def test_lstat_should_report_on_symlink_itself
|
||||
File.open("foo", "w") { |f| f << "some content" }
|
||||
File.symlink "foo", "my_symlink"
|
||||
|
||||
assert_not_equal File.lstat("my_symlink").size, File.lstat("foo").size
|
||||
end
|
||||
|
||||
def test_should_report_on_symlink_itself_with_size_instance_method
|
||||
File.open("foo", "w") { |f| f << "some content" }
|
||||
File.symlink "foo", "my_symlink"
|
||||
|
||||
file = File.open("foo")
|
||||
symlink = File.open("my_symlink")
|
||||
|
||||
assert_not_equal file.lstat.size, symlink.lstat.size
|
||||
end
|
||||
|
||||
def test_symlink_size_is_size_of_path_pointed_to
|
||||
File.open("a", "w") { |x| x << "foobarbazfoobarbaz" }
|
||||
File.symlink "a", "one_char_symlink"
|
||||
assert_equal 1, File.lstat("one_char_symlink").size
|
||||
|
||||
File.open("ab", "w") { |x| x << "foobarbazfoobarbaz" }
|
||||
File.symlink "ab", "two_char_symlink"
|
||||
assert_equal 2, File.lstat("two_char_symlink").size
|
||||
end
|
||||
end
|
39
test/fake/file/stat_test.rb
Normal file
39
test/fake/file/stat_test.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require "test_helper"
|
||||
|
||||
class FileStat < Test::Unit::TestCase
|
||||
def setup
|
||||
FakeFS.activate!
|
||||
FakeFS::FileSystem.clear
|
||||
end
|
||||
|
||||
def teardown
|
||||
FakeFS.deactivate!
|
||||
end
|
||||
|
||||
def test_calling_stat_should_create_a_new_file_stat_object
|
||||
File.open("foo", "w") do |f|
|
||||
f << "bar"
|
||||
end
|
||||
|
||||
File.open("foo") do |f|
|
||||
assert_equal File::Stat, f.stat.class
|
||||
end
|
||||
end
|
||||
|
||||
def test_stat_should_use_correct_file
|
||||
File.open("bar", "w") do |f|
|
||||
f << "1"
|
||||
end
|
||||
|
||||
File.open("bar") do |f|
|
||||
assert_equal 1, f.stat.size
|
||||
end
|
||||
end
|
||||
|
||||
def test_stat_should_report_on_symlink_pointer
|
||||
File.open("foo", "w") { |f| f << "some content" }
|
||||
File.symlink "foo", "my_symlink"
|
||||
|
||||
assert_equal File.stat("my_symlink").size, File.stat("foo").size
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user