Skip to content

Commit

Permalink
Fix ruby 1.9 has no ftools
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 16, 2012
1 parent 0e673a4 commit 714c506
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions lib/file_sandbox.rb
@@ -1,12 +1,11 @@
require 'ftools'
require 'fileutils'
require 'test/unit/assertions'

module FileSandbox
VERSION = "0.5"

attr_reader :sandbox

def in_sandbox(&block)
raise "I expected to create a sandbox as you passed in a block to me" if !block_given?

Expand Down Expand Up @@ -56,7 +55,7 @@ def initialize(path = '__sandbox')
clean_up
FileUtils.mkdir_p @root
end

def [](name)
SandboxFile.new(File.join(@root, name))
end
Expand All @@ -74,17 +73,17 @@ def new(options)
file.content = (options.delete(:with_content) || options.delete(:with_contents) || '')
end
end

raise "unexpected keys '#{options.keys.join(', ')}'" unless options.empty?

dir || file
end

def remove(options)
name = File.join(@root, options[:file])
FileUtils.remove_file name
end

# usage assert :file=>'my file.rb', :has_contents=>'some stuff'
def assert(options)
name = File.join(@root, options[:file])
Expand All @@ -105,7 +104,7 @@ def clean_up

class SandboxFile
attr_reader :path

def initialize(path)
@path = path
end
Expand All @@ -117,12 +116,12 @@ def exist?
def content
File.read path
end

def content=(content)
FileUtils.mkdir_p File.dirname(@path)
File.open(@path, "w") {|f| f << content}
end

def binary_content=(content)
FileUtils.mkdir_p File.dirname(@path)
File.open(@path, "wb") {|f| f << content}
Expand All @@ -135,15 +134,15 @@ def create
alias exists? exist?
alias contents content
alias contents= content=
alias binary_contents= binary_content=
alias binary_contents= binary_content=
end

private

def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
end
end

0 comments on commit 714c506

Please sign in to comment.