From c2d7f46bcb6fe3824920fb584d9690133ad112ac Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:55:18 -0400 Subject: [PATCH] move around and reformat code --- lib/fakefs/dir.rb | 46 ++++++++++++++++++++++++++++++++++++++++ lib/fakefs/safe.rb | 1 - lib/fakefs/tmpdir.rb | 50 -------------------------------------------- 3 files changed, 46 insertions(+), 51 deletions(-) delete mode 100644 lib/fakefs/tmpdir.rb diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 32c5234..b2d84fd 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -107,6 +107,52 @@ module FakeFS FileSystem.current_dir.to_s end + # This code has been borrowed from Rubinius + def self.mktmpdir(prefix_suffix = nil, tmpdir = nil) + 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 + + 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 + mkdir(path, 0700) + rescue Errno::EEXIST + n ||= 0 + n += 1 + retry + end + + if block_given? + begin + yield path + ensure + require 'fileutils' + # 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 + class << self alias_method :getwd, :pwd alias_method :rmdir, :delete diff --git a/lib/fakefs/safe.rb b/lib/fakefs/safe.rb index 4f2f822..a1b804c 100644 --- a/lib/fakefs/safe.rb +++ b/lib/fakefs/safe.rb @@ -9,4 +9,3 @@ require 'fakefs/fileutils' require 'fakefs/file' require 'fakefs/file_test' require 'fakefs/dir' -require 'fakefs/tmpdir' diff --git a/lib/fakefs/tmpdir.rb b/lib/fakefs/tmpdir.rb deleted file mode 100644 index db8d79f..0000000 --- a/lib/fakefs/tmpdir.rb +++ /dev/null @@ -1,50 +0,0 @@ -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