From bd97171227c2873854950abd58861fa48c20cd03 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 13 Jun 2009 22:29:00 -0700 Subject: [PATCH] Refactored touch to verify that the parent directories exist --- lib/fakefs.rb | 15 +++++++++++++-- test/fakefs_test.rb | 10 ++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index ed3ae98..6f4f04e 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -96,8 +96,19 @@ module FakeFS chown(user, group, list, options={}) end - def touch(path) - FileSystem.add(path, MockFile.new) + def touch(list, options={}) + list.each do |f| + if f =~ /(.+\/)/ + dir_path = f.scan(/(.+\/)/) + if FileSystem.find(dir_path.to_s) + FileSystem.add(f, MockFile.new) + else + raise Errno::ENOENT, f + end + else + FileSystem.add(f, MockFile.new) + end + end end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 7481b95..ca3b3c7 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -447,8 +447,14 @@ class FakeFSTest < Test::Unit::TestCase end def test_files_can_be_touched - FileUtils.touch(here("file")) - assert File.exists?(here("file")) + FileUtils.touch('touched_file') + assert File.exists?('touched_file') + end + + def test_touch_does_not_work_if_the_dir_path_cannot_be_found + assert_raises(Errno::ENOENT) { + FileUtils.touch('this/path/should/not/be/here') + } end def here(fname)