From 34a88c4e633127fe2c5f6542ef3e7aade8c8cc7a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 30 Sep 2009 01:23:26 -0400 Subject: [PATCH] Start File::Stat. Add Stat#symlink?, stat#directory? --- lib/fakefs/file.rb | 22 +++++++++++++++++ test/fakefs_test.rb | 5 ++++ test/file/stat_test.rb | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 test/file/stat_test.rb diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index fe2e046..8345d75 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -123,6 +123,28 @@ module FakeFS 0 end + def self.stat(file) + File::Stat.new(file) + end + + class Stat + def initialize(file) + if !File.exists?(file) + raise(Errno::ENOENT, "No such file or directory - #{file}") + end + + @file = file + end + + def symlink? + File.symlink?(@file) + end + + def directory? + File.directory?(@file) + end + end + attr_reader :path def initialize(path, mode = READ_ONLY, perm = nil) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 1f69e2a..0d893af 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -949,6 +949,11 @@ class FakeFSTest < Test::Unit::TestCase assert_equal "some content", File.read("/bar") end + def test_file_stat_returns_file_stat_object + FileUtils.touch("/foo") + assert_equal File::Stat, File.stat("/foo").class + end + def here(fname) RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname) end diff --git a/test/file/stat_test.rb b/test/file/stat_test.rb new file mode 100644 index 0000000..fd12353 --- /dev/null +++ b/test/file/stat_test.rb @@ -0,0 +1,54 @@ +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib') +require 'fakefs/safe' +require 'test/unit' + +class FileStatTest < Test::Unit::TestCase + include FakeFS + + def setup + FileSystem.clear + end + + def touch(*args) + FileUtils.touch(*args) + end + + def ln_s(*args) + FileUtils.ln_s(*args) + end + + def mkdir(*args) + Dir.mkdir(*args) + end + + def test_file_stat_init_with_non_existant_file + assert_raises(Errno::ENOENT) do + File::Stat.new("/foo") + end + end + + def test_symlink_should_be_true_when_symlink + touch("/foo") + ln_s("/foo", "/bar") + + assert File::Stat.new("/bar").symlink? + end + + def test_symlink_should_be_false_when_not_a_symlink + FileUtils.touch("/foo") + + assert !File::Stat.new("/foo").symlink? + end + + def test_should_return_false_for_directory_when_not_a_directory + FileUtils.touch("/foo") + + assert !File::Stat.new("/foo").directory? + end + + def test_should_return_true_for_directory_when_a_directory + mkdir "/foo" + + assert File::Stat.new("/foo").directory? + end +end