Added Dir.mktmpdir and FileTest.directory? Closes #74
This commit is contained in:
parent
f5913573ef
commit
564387b6a3
@ -3,5 +3,9 @@ module FakeFS
|
||||
def self.exist?(file_name)
|
||||
File.exist?(file_name)
|
||||
end
|
||||
|
||||
def self.directory?(file_name)
|
||||
File.directory?(file_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -9,3 +9,4 @@ require 'fakefs/fileutils'
|
||||
require 'fakefs/file'
|
||||
require 'fakefs/file_test'
|
||||
require 'fakefs/dir'
|
||||
require 'fakefs/tmpdir'
|
||||
|
50
lib/fakefs/tmpdir.rb
Normal file
50
lib/fakefs/tmpdir.rb
Normal file
@ -0,0 +1,50 @@
|
||||
require 'fakefs/fileutils'
|
||||
|
||||
module FakeFS
|
||||
class Dir
|
||||
def Dir::mktmpdir(prefix_suffix=nil, tmpdir=nil)
|
||||
# This code has been borrowed from Rubinius. Thanks, guys! :-)
|
||||
|
||||
case prefix_suffix
|
||||
when nil
|
||||
prefix = "d"
|
||||
suffix = ""
|
||||
when String
|
||||
prefix = prefix_suffix
|
||||
suffix = ""
|
||||
when Array
|
||||
prefix = prefix_suffix[0]
|
||||
suffix = prefix_suffix[1]
|
||||
else
|
||||
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
|
||||
end
|
||||
tmpdir ||= Dir.tmpdir
|
||||
t = Time.now.strftime("%Y%m%d")
|
||||
n = nil
|
||||
begin
|
||||
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
||||
path << "-#{n}" if n
|
||||
path << suffix
|
||||
Dir.mkdir(path, 0700)
|
||||
rescue Errno::EEXIST
|
||||
n ||= 0
|
||||
n += 1
|
||||
retry
|
||||
end
|
||||
|
||||
if block_given?
|
||||
begin
|
||||
yield path
|
||||
ensure
|
||||
# This here was using FileUtils.remove_entry_secure instead of just
|
||||
# .rm_r. However, the security concerns that apply to
|
||||
# .rm_r/.remove_entry_secure shouldn't apply to a test fake
|
||||
# filesystem. :^)
|
||||
FileUtils.rm_r path
|
||||
end
|
||||
else
|
||||
path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1573,6 +1573,14 @@ class FakeFSTest < Test::Unit::TestCase
|
||||
assert !FileTest.exist?("/path/to/dir")
|
||||
end
|
||||
|
||||
def test_filetest_directory_returns_correct_values
|
||||
FileUtils.mkdir_p '/path/to/somedir'
|
||||
assert FileTest.directory?('/path/to/somedir')
|
||||
|
||||
FileUtils.rm_r '/path/to/somedir'
|
||||
assert !FileTest.directory?('/path/to/somedir')
|
||||
end
|
||||
|
||||
def test_pathname_exists_returns_correct_value
|
||||
FileUtils.touch "foo"
|
||||
assert Pathname.new("foo").exist?
|
||||
@ -1580,6 +1588,20 @@ class FakeFSTest < Test::Unit::TestCase
|
||||
assert !Pathname.new("bar").exist?
|
||||
end
|
||||
|
||||
def test_dir_mktmpdir
|
||||
FileUtils.mkdir '/tmp'
|
||||
|
||||
tmpdir = Dir.mktmpdir
|
||||
assert File.directory?(tmpdir)
|
||||
FileUtils.rm_r tmpdir
|
||||
|
||||
Dir.mktmpdir do |t|
|
||||
tmpdir = t
|
||||
assert File.directory?(t)
|
||||
end
|
||||
assert !File.directory?(tmpdir)
|
||||
end
|
||||
|
||||
def test_activating_returns_true
|
||||
FakeFS.deactivate!
|
||||
assert_equal true, FakeFS.activate!
|
||||
|
Loading…
Reference in New Issue
Block a user