Skip to content

Commit

Permalink
Merge pull request #115 from autohaus24/ruby_193_compatibility
Browse files Browse the repository at this point in the history
improvements needed when upgrading to ruby 1.9.3
  • Loading branch information
jfirebaugh committed Apr 17, 2012
2 parents dc0d08d + af733ea commit ed857c4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/fakefs/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def self.mkdir(string, integer = 0)
parent = string.split('/')
parent.pop

joined_parent_path = parent.join
joined_parent_path = parent.join("/")

_check_for_valid_file(joined_parent_path) unless joined_parent_path == ""
raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)
Expand Down
3 changes: 2 additions & 1 deletion lib/fakefs/fake/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class FakeFile

class Inode
def initialize(file_owner)
@content = ""
#1.9.3 when possible set default external encoding
@content = "".respond_to?(:encode) ? "".encode(Encoding.default_external) : ""
@links = [file_owner]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/fakefs/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ def size

def initialize(path, mode = READ_ONLY, perm = nil)
@path = path
@mode = mode
@mode = mode.is_a?(Hash) ? (mode[:mode] || READ_ONLY) : mode
@file = FileSystem.find(path)
@autoclose = true

check_modes!

file_creation_mode? ? create_missing_file : check_file_existence!

super(@file.content, mode)
super(@file.content, @mode)
end

def exists?
Expand Down
7 changes: 4 additions & 3 deletions lib/fakefs/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ def add(path, object=FakeDir.new)

# copies directories and files from the real filesystem
# into our fake one
def clone(path)
def clone(path, target = nil)
path = File.expand_path(path)
pattern = File.join(path, '**', '*')
files = RealFile.file?(path) ? [path] : [path] + RealDir.glob(pattern, RealFile::FNM_DOTMATCH)

files.each do |f|
target_path = target ? f.gsub(path, target) : f
if RealFile.file?(f)
FileUtils.mkdir_p(File.dirname(f))
File.open(f, File::WRITE_ONLY) do |g|
File.open(target_path, File::WRITE_ONLY) do |g|
g.print RealFile.open(f){|h| h.read }
end
elsif RealFile.directory?(f)
FileUtils.mkdir_p(f)
FileUtils.mkdir_p(target_path)
elsif RealFile.symlink?(f)
FileUtils.ln_s()
end
Expand Down
50 changes: 38 additions & 12 deletions test/fakefs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,21 @@ def test_can_read_from_file_objects
assert_equal "Yatta!", File.new(path).read
end

if RUBY_VERSION >= "1.9"
def test_file_object_has_default_external_encoding
Encoding.default_external = "UTF-8"
path = 'file.txt'
File.open(path, 'w'){|f| f.write 'Yatta!' }
assert_equal "UTF-8", File.new(path).read.encoding.name
end
end

def test_file_object_initialization_with_mode_in_hash_parameter
assert_nothing_raised do
File.open("file.txt", {:mode => "w"}){ |f| f.write 'Yatta!' }
end
end

def test_file_read_errors_appropriately
assert_raise Errno::ENOENT do
File.read('anything')
Expand Down Expand Up @@ -1132,34 +1147,39 @@ def test_clone_clones_normal_files
end

def test_clone_clones_directories
FakeFS.deactivate!
RealFileUtils.mkdir_p(here('subdir'))
FakeFS.activate!
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir')) }

FileSystem.clone(here('subdir'))

assert File.exists?(here('subdir')), 'subdir was cloned'
assert File.directory?(here('subdir')), 'subdir is a directory'
ensure
FakeFS.deactivate!
RealFileUtils.rm_rf(here('subdir'))
FakeFS.activate!
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
end

def test_clone_clones_dot_files_even_hard_to_find_ones
FakeFS.deactivate!
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
FakeFS.activate!
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }

assert !File.exists?(here('subdir'))

FileSystem.clone(here('subdir'))
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
ensure
FakeFS.deactivate!
RealFileUtils.rm_rf(here('subdir'))
FakeFS.activate!
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
end

def test_clone_with_target_specified
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }

assert !File.exists?(here('subdir'))

FileSystem.clone(here('subdir'), here('subdir2'))
assert !File.exists?(here('subdir'))
assert_equal ['.bar'], FileSystem.find(here('subdir2')).keys
assert_equal ['foo'], FileSystem.find(here('subdir2/.bar/baz/.quux')).keys
ensure
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
end

def test_putting_a_dot_at_end_copies_the_contents
Expand Down Expand Up @@ -1464,6 +1484,12 @@ def test_directory_mkdir
assert File.exists?('/path')
end

def test_directory_mkdir_nested
Dir.mkdir("/tmp")
Dir.mkdir("/tmp/stream20120103-11847-xc8pb.lock")
assert File.exists?("/tmp/stream20120103-11847-xc8pb.lock")
end

def test_directory_mkdir_relative
FileUtils.mkdir_p('/new/root')
FileSystem.chdir('/new/root')
Expand Down
9 changes: 8 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
begin
require 'redgreen'
rescue LoadError
end
end

def act_on_real_fs
raise ArgumentError unless block_given?
FakeFS.deactivate!
yield
FakeFS.activate!
end

0 comments on commit ed857c4

Please sign in to comment.