From 364dc66ee5e6ffb954cef537556b6a6d4282ceca Mon Sep 17 00:00:00 2001 From: Jeff Hodges Date: Mon, 1 Jun 2009 23:54:04 -0700 Subject: [PATCH] FileSystem.clone now clones directories and dot files. --- lib/fakefs.rb | 5 ++++- test/fakefs_test.rb | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 9f670c3..59037b6 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -4,6 +4,8 @@ require 'pathname' RealFile = File RealFileUtils = FileUtils RealDir = Dir +RealFileUtils::Dir = RealDir +RealFileUtils::File = RealFile module FakeFS module FileUtils @@ -229,7 +231,8 @@ module FakeFS def clone(path) path = File.expand_path(path) pattern = File.join(path, '**', '*') - files = RealFile.file?(path) ? [path] : RealDir.glob(pattern) + dot_pattern = File.join(path, '**', '.*') + files = RealFile.file?(path) ? [path] : [path] + RealDir.glob([pattern, dot_pattern]) files.each do |f| if RealFile.file?(f) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index de0d34f..533f2d7 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -345,7 +345,6 @@ class FakeFSTest < Test::Unit::TestCase end def test_clone_clones_normal_files - def here(fname); File.expand_path(File.dirname(__FILE__)+'/'+fname); end RealFile.open(here('foo'), 'w'){|f| f.write 'bar' } assert !File.exists?(here('foo')) FileSystem.clone(here('foo')) @@ -354,6 +353,26 @@ class FakeFSTest < Test::Unit::TestCase RealFile.unlink(here('foo')) if RealFile.exists?(here('foo')) end + def test_clone_clones_directories + 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 + RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir')) + end + + def test_clone_clones_dot_files + RealFileUtils.mkdir_p(here('subdir/.bar')) + assert !File.exists?(here('subdir')) + FileSystem.clone(here('subdir')) + assert_equal ['.bar'], FileSystem.find(here('subdir')).keys + ensure + RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir')) + end + def test_putting_a_dot_at_end_copies_the_contents FileUtils.mkdir_p 'subdir' Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } } @@ -374,4 +393,8 @@ class FakeFSTest < Test::Unit::TestCase assert_equal 'works', File.open('new/nother'){|f| f.read } end + + def here(fname) + RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname) + end end