diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index e09f749..7229537 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -7,6 +7,14 @@ module FakeFS end alias_method :mkpath, :mkdir_p + def mkdir(path) + parent = path.split('/') + parent.pop + raise Errno::ENOENT, "No such file or directory - #{path}" unless parent.join == "" || FileSystem.find(parent.join('/')) + raise Errno::EEXIST, "File exists - #{path}" if FileSystem.find(path) + FileSystem.add(path, FakeDir.new) + end + def rmdir(list, options = {}) list = [ list ] unless list.is_a?(Array) list.each do |l| diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index e14cac0..b17c433 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -23,7 +23,7 @@ class FakeFSTest < Test::Unit::TestCase assert_equal 1, fs.files.size end - def test_can_create_directories + def test_can_create_directories_with_file_utils_mkdir_p FileUtils.mkdir_p("/path/to/dir") assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] end @@ -33,6 +33,18 @@ class FakeFSTest < Test::Unit::TestCase assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] end + def test_can_create_directories_with_file_utils_mkdir + FileUtils.mkdir_p("/path/to/dir") + FileUtils.mkdir("/path/to/dir/subdir") + assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir'] + end + + def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path + assert_raises Errno::ENOENT do + FileUtils.mkdir("/this/path/does/not/exists/newdir") + end + end + def test_can_create_directories_with_mkpath FileUtils.mkpath("/path/to/dir") assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] @@ -83,6 +95,10 @@ class FakeFSTest < Test::Unit::TestCase assert File.exists?(path) FileUtils.mkdir_p("/path/to") assert File.exists?(path) + assert_raises Errno::EEXIST do + FileUtils.mkdir("/path/to") + end + assert File.exists?(path) end def test_can_create_symlinks