From 12532c8e376872e9764176199d61066b5ea10e6a Mon Sep 17 00:00:00 2001 From: timo3377 Date: Tue, 19 Apr 2011 12:14:17 -0700 Subject: [PATCH 01/40] Alias mkdir_p + makedirs for compatability with Rails3. Closes #79 --- lib/fakefs/fileutils.rb | 1 + test/fakefs_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index 4315feb..0b9536f 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -6,6 +6,7 @@ module FakeFS FileSystem.add(path, FakeDir.new) end alias_method :mkpath, :mkdir_p + alias_method :makedirs, :mkdir_p def mkdir(path) parent = path.split('/') diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index f041baf..49495b3 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -55,6 +55,16 @@ class FakeFSTest < Test::Unit::TestCase assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] end + def test_can_create_directories_with_mkpath + FileUtils.makedirs("/path/to/dir") + assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] + end + + def test_can_create_directories_with_mkpath_and_options + FileUtils.makedirs("/path/to/dir", :mode => 0755) + assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] + end + def test_can_delete_directories FileUtils.mkdir_p("/path/to/dir") FileUtils.rmdir("/path/to/dir") From fa612b194bcc89f748b19902345e5a4ab692cb97 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:36:07 -0400 Subject: [PATCH 02/40] Update rake tasks to use rspec 2. --- Rakefile | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Rakefile b/Rakefile index 253d4af..353ebce 100644 --- a/Rakefile +++ b/Rakefile @@ -8,16 +8,9 @@ end task :default => [:test, :spec] -begin - require 'spec/rake/spectask' - - desc "Run specs" - Spec::Rake::SpecTask.new(:spec) do |t| - t.spec_files = FileList["spec/**/*.rb"] - end -rescue LoadError - puts "Spec task can't be loaded. `gem install rspec`" -end +require 'rspec/core/rake_task' +desc "Run specs" +RSpec::Core::RakeTask.new begin require 'jeweler' From b556f7ba2489cd92667b701d14a13a0cfad13d55 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:36:33 -0400 Subject: [PATCH 03/40] Add rspec 2 as a dev only dependency --- Gemfile | 3 +++ Gemfile.lock | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..16c23c7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +group :development do + gem 'rspec' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..02a6402 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + specs: + diff-lcs (1.1.2) + rspec (2.5.0) + rspec-core (~> 2.5.0) + rspec-expectations (~> 2.5.0) + rspec-mocks (~> 2.5.0) + rspec-core (2.5.1) + rspec-expectations (2.5.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.5.0) + +PLATFORMS + ruby + +DEPENDENCIES + rspec From f5913573ef58f04a5c08b69376b35b3907506f1a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:36:54 -0400 Subject: [PATCH 04/40] add .rspec file to run specs with color --- .rspec | 1 + 1 file changed, 1 insertion(+) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..5052887 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color \ No newline at end of file From 564387b6a33077a08c6d686b1b52592879894534 Mon Sep 17 00:00:00 2001 From: Eric MSP Veith Date: Sat, 2 Apr 2011 14:40:40 +0000 Subject: [PATCH 05/40] Added Dir.mktmpdir and FileTest.directory? Closes #74 --- lib/fakefs/file_test.rb | 4 ++++ lib/fakefs/safe.rb | 1 + lib/fakefs/tmpdir.rb | 50 +++++++++++++++++++++++++++++++++++++++++ test/fakefs_test.rb | 22 ++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 lib/fakefs/tmpdir.rb diff --git a/lib/fakefs/file_test.rb b/lib/fakefs/file_test.rb index 6c9cee7..5dbdbf5 100644 --- a/lib/fakefs/file_test.rb +++ b/lib/fakefs/file_test.rb @@ -3,5 +3,9 @@ module FakeFS def self.exist?(file_name) File.exist?(file_name) end + + def self.directory?(file_name) + File.directory?(file_name) + end end end diff --git a/lib/fakefs/safe.rb b/lib/fakefs/safe.rb index a1b804c..4f2f822 100644 --- a/lib/fakefs/safe.rb +++ b/lib/fakefs/safe.rb @@ -9,3 +9,4 @@ require 'fakefs/fileutils' require 'fakefs/file' require 'fakefs/file_test' require 'fakefs/dir' +require 'fakefs/tmpdir' diff --git a/lib/fakefs/tmpdir.rb b/lib/fakefs/tmpdir.rb new file mode 100644 index 0000000..db8d79f --- /dev/null +++ b/lib/fakefs/tmpdir.rb @@ -0,0 +1,50 @@ +require 'fakefs/fileutils' + +module FakeFS + class Dir + def Dir::mktmpdir(prefix_suffix=nil, tmpdir=nil) + # This code has been borrowed from Rubinius. Thanks, guys! :-) + + case prefix_suffix + when nil + prefix = "d" + suffix = "" + when String + prefix = prefix_suffix + suffix = "" + when Array + prefix = prefix_suffix[0] + suffix = prefix_suffix[1] + else + raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" + end + tmpdir ||= Dir.tmpdir + t = Time.now.strftime("%Y%m%d") + n = nil + begin + path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" + path << "-#{n}" if n + path << suffix + Dir.mkdir(path, 0700) + rescue Errno::EEXIST + n ||= 0 + n += 1 + retry + end + + if block_given? + begin + yield path + ensure + # This here was using FileUtils.remove_entry_secure instead of just + # .rm_r. However, the security concerns that apply to + # .rm_r/.remove_entry_secure shouldn't apply to a test fake + # filesystem. :^) + FileUtils.rm_r path + end + else + path + end + end + end +end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 49495b3..211471c 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -1573,6 +1573,14 @@ class FakeFSTest < Test::Unit::TestCase assert !FileTest.exist?("/path/to/dir") end + def test_filetest_directory_returns_correct_values + FileUtils.mkdir_p '/path/to/somedir' + assert FileTest.directory?('/path/to/somedir') + + FileUtils.rm_r '/path/to/somedir' + assert !FileTest.directory?('/path/to/somedir') + end + def test_pathname_exists_returns_correct_value FileUtils.touch "foo" assert Pathname.new("foo").exist? @@ -1580,6 +1588,20 @@ class FakeFSTest < Test::Unit::TestCase assert !Pathname.new("bar").exist? end + def test_dir_mktmpdir + FileUtils.mkdir '/tmp' + + tmpdir = Dir.mktmpdir + assert File.directory?(tmpdir) + FileUtils.rm_r tmpdir + + Dir.mktmpdir do |t| + tmpdir = t + assert File.directory?(t) + end + assert !File.directory?(tmpdir) + end + def test_activating_returns_true FakeFS.deactivate! assert_equal true, FakeFS.activate! From 903b8c5fd80ef70a343f866eee54766fc44c87a0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:54:56 -0400 Subject: [PATCH 06/40] Add source :rubygems to Gemfile --- Gemfile | 2 ++ Gemfile.lock | 1 + 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 16c23c7..1700579 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +source :rubygems + group :development do gem 'rspec' end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 02a6402..d2ea896 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: http://rubygems.org/ specs: diff-lcs (1.1.2) rspec (2.5.0) From c2d7f46bcb6fe3824920fb584d9690133ad112ac Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:55:18 -0400 Subject: [PATCH 07/40] move around and reformat code --- lib/fakefs/dir.rb | 46 ++++++++++++++++++++++++++++++++++++++++ lib/fakefs/safe.rb | 1 - lib/fakefs/tmpdir.rb | 50 -------------------------------------------- 3 files changed, 46 insertions(+), 51 deletions(-) delete mode 100644 lib/fakefs/tmpdir.rb diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 32c5234..b2d84fd 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -107,6 +107,52 @@ module FakeFS FileSystem.current_dir.to_s end + # This code has been borrowed from Rubinius + def self.mktmpdir(prefix_suffix = nil, tmpdir = nil) + case prefix_suffix + when nil + prefix = "d" + suffix = "" + when String + prefix = prefix_suffix + suffix = "" + when Array + prefix = prefix_suffix[0] + suffix = prefix_suffix[1] + else + raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" + end + + t = Time.now.strftime("%Y%m%d") + n = nil + + begin + path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" + path << "-#{n}" if n + path << suffix + mkdir(path, 0700) + rescue Errno::EEXIST + n ||= 0 + n += 1 + retry + end + + if block_given? + begin + yield path + ensure + require 'fileutils' + # This here was using FileUtils.remove_entry_secure instead of just + # .rm_r. However, the security concerns that apply to + # .rm_r/.remove_entry_secure shouldn't apply to a test fake + # filesystem. :^) + FileUtils.rm_r path + end + else + path + end + end + class << self alias_method :getwd, :pwd alias_method :rmdir, :delete diff --git a/lib/fakefs/safe.rb b/lib/fakefs/safe.rb index 4f2f822..a1b804c 100644 --- a/lib/fakefs/safe.rb +++ b/lib/fakefs/safe.rb @@ -9,4 +9,3 @@ require 'fakefs/fileutils' require 'fakefs/file' require 'fakefs/file_test' require 'fakefs/dir' -require 'fakefs/tmpdir' diff --git a/lib/fakefs/tmpdir.rb b/lib/fakefs/tmpdir.rb deleted file mode 100644 index db8d79f..0000000 --- a/lib/fakefs/tmpdir.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'fakefs/fileutils' - -module FakeFS - class Dir - def Dir::mktmpdir(prefix_suffix=nil, tmpdir=nil) - # This code has been borrowed from Rubinius. Thanks, guys! :-) - - case prefix_suffix - when nil - prefix = "d" - suffix = "" - when String - prefix = prefix_suffix - suffix = "" - when Array - prefix = prefix_suffix[0] - suffix = prefix_suffix[1] - else - raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}" - end - tmpdir ||= Dir.tmpdir - t = Time.now.strftime("%Y%m%d") - n = nil - begin - path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" - path << "-#{n}" if n - path << suffix - Dir.mkdir(path, 0700) - rescue Errno::EEXIST - n ||= 0 - n += 1 - retry - end - - if block_given? - begin - yield path - ensure - # This here was using FileUtils.remove_entry_secure instead of just - # .rm_r. However, the security concerns that apply to - # .rm_r/.remove_entry_secure shouldn't apply to a test fake - # filesystem. :^) - FileUtils.rm_r path - end - else - path - end - end - end -end From 129b557160da0065980f17d9aad5a85c5a5861c3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 21:59:18 -0400 Subject: [PATCH 08/40] add dev dependencies: jeweler, sdoc-helpers, rdiscount --- Gemfile | 3 +++ Gemfile.lock | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Gemfile b/Gemfile index 1700579..35f6baa 100644 --- a/Gemfile +++ b/Gemfile @@ -2,4 +2,7 @@ source :rubygems group :development do gem 'rspec' + gem 'jeweler' + gem 'sdoc-helpers' + gem 'rdiscount' end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index d2ea896..97e60c2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,15 @@ GEM remote: http://rubygems.org/ specs: diff-lcs (1.1.2) + git (1.2.5) + jeweler (1.6.0) + bundler (~> 1.0.0) + git (>= 1.2.5) + rake + json (1.5.1) + rake (0.8.7) + rdiscount (1.6.8) + rdoc (2.4.3) rspec (2.5.0) rspec-core (~> 2.5.0) rspec-expectations (~> 2.5.0) @@ -10,9 +19,17 @@ GEM rspec-expectations (2.5.0) diff-lcs (~> 1.1.2) rspec-mocks (2.5.0) + sdoc (0.2.20) + json (>= 1.1.3) + rdoc (= 2.4.3) + sdoc-helpers (0.1.4) + sdoc (~> 0.2) PLATFORMS ruby DEPENDENCIES + jeweler + rdiscount rspec + sdoc-helpers From 2e55319dc0c2f87af8f28c56ce6096060d22c111 Mon Sep 17 00:00:00 2001 From: Noah Paessel Date: Tue, 18 Jan 2011 20:22:26 -0500 Subject: [PATCH 09/40] add file#split. Closes #61 --- lib/fakefs/file.rb | 4 ++++ test/fakefs_test.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 15965fe..01931c6 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -215,6 +215,10 @@ module FakeFS File::Stat.new(file, true) end + def self.split(path) + return RealFile.split(path) + end + class Stat attr_reader :ctime, :mtime diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 211471c..fa7527d 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -3,6 +3,14 @@ require "test_helper" class FakeFSTest < Test::Unit::TestCase include FakeFS + def test_split + assert File.respond_to? :split + filename = "/this/is/what/we/expect.txt" + path,filename = File.split(filename) + assert_equal path, "/this/is/what/we" + assert_equal filename, "expect.txt" + end + def setup FakeFS.activate! FileSystem.clear From 3916ffc91d4be5b4f2f19bc7c7fde1fec97ad6d8 Mon Sep 17 00:00:00 2001 From: Noah Paessel Date: Tue, 18 Jan 2011 20:37:18 -0500 Subject: [PATCH 10/40] Be polite; add new test to bottom of test file. --- test/fakefs_test.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index fa7527d..39e46b0 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -3,14 +3,6 @@ require "test_helper" class FakeFSTest < Test::Unit::TestCase include FakeFS - def test_split - assert File.respond_to? :split - filename = "/this/is/what/we/expect.txt" - path,filename = File.split(filename) - assert_equal path, "/this/is/what/we" - assert_equal filename, "expect.txt" - end - def setup FakeFS.activate! FileSystem.clear @@ -1618,6 +1610,15 @@ class FakeFSTest < Test::Unit::TestCase def test_deactivating_returns_true assert_equal true, FakeFS.deactivate! end + + def test_split + assert File.respond_to? :split + filename = "/this/is/what/we/expect.txt" + path,filename = File.split(filename) + assert_equal path, "/this/is/what/we" + assert_equal filename, "expect.txt" + end + def here(fname) RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname)) From 8bf4b5aeb0751b78414c72b8f9e89e4135a70592 Mon Sep 17 00:00:00 2001 From: Mariusz Pietrzyk Date: Tue, 28 Dec 2010 10:25:19 +0100 Subject: [PATCH 11/40] Raising Errno::ENOENT exception with a particular path instead of src, which may be an array. Closes #59 --- lib/fakefs/fileutils.rb | 2 +- test/fakefs_test.rb | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index 0b9536f..8a47856 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -102,7 +102,7 @@ module FakeFS FileSystem.add(dest_path, target.entry.clone) FileSystem.delete(path) else - raise Errno::ENOENT, src + raise Errno::ENOENT, path end end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 39e46b0..e75aaf4 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -568,7 +568,6 @@ class FakeFSTest < Test::Unit::TestCase assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/**/*'] assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*'] - assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*'] FileUtils.cp_r '/path', '/otherpath' @@ -803,6 +802,10 @@ class FakeFSTest < Test::Unit::TestCase assert_raise(Errno::ENOENT) do FileUtils.mv 'blafgag', 'foo' end + exception = assert_raise(Errno::ENOENT) do + FileUtils.mv ['foo', 'bar'], 'destdir' + end + assert_equal "No such file or directory - foo", exception.message end def test_mv_actually_works @@ -1610,7 +1613,7 @@ class FakeFSTest < Test::Unit::TestCase def test_deactivating_returns_true assert_equal true, FakeFS.deactivate! end - + def test_split assert File.respond_to? :split filename = "/this/is/what/we/expect.txt" From 1f948a0fd7eedfb9024e1a42e47596e662a24189 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 22:54:26 -0400 Subject: [PATCH 12/40] update contributors --- CONTRIBUTORS | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 17ea9bc..7fc39ac 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -8,20 +8,23 @@ Víctor Martínez Nick Quaranto Aaron Suggs Victor Costan +Eric MSP Veith Jon Yurek Greg Campbell Ben Mabey Mark Sam Goldstein +Noah Paessel dmathieu <42@dmathieu.com> +Mariusz Pietrzyk Lars Gierth marano jameswilding -Scott Barron Tymon Tobolski -Mariusz Pietrzyk +Scott Barron Keita Urashima David Reese msassak +timo3377 Mislav Marohnić Rob Sanheim From 3762fa2b3580f0e9c9ca598b143237ad85e6155b Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 23:05:08 -0400 Subject: [PATCH 13/40] update version to 0.3.2 --- lib/fakefs/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fakefs/version.rb b/lib/fakefs/version.rb index 61e9d52..4cf4940 100644 --- a/lib/fakefs/version.rb +++ b/lib/fakefs/version.rb @@ -1,6 +1,6 @@ module FakeFS module Version - VERSION = "0.3.1" + VERSION = "0.3.2" def self.to_s VERSION From 1e074bf636d243275330b4c26d4d3e9aabf46d83 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 2 May 2011 23:06:29 -0400 Subject: [PATCH 14/40] update gemspec --- fakefs.gemspec | 108 ++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/fakefs.gemspec b/fakefs.gemspec index 9573c5a..4a96b83 100644 --- a/fakefs.gemspec +++ b/fakefs.gemspec @@ -1,86 +1,84 @@ # Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{fakefs} - s.version = "0.3.1" + s.version = "0.3.2" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"] - s.date = %q{2010-12-18} + s.date = %q{2011-05-02} s.description = %q{A fake filesystem. Use it in your tests.} s.email = %q{chris@ozmm.org} s.extra_rdoc_files = [ "LICENSE", - "README.markdown" + "README.markdown" ] s.files = [ ".autotest", - ".gitignore", - "CONTRIBUTORS", - "LICENSE", - "README.markdown", - "Rakefile", - "fakefs.gemspec", - "lib/fakefs.rb", - "lib/fakefs/base.rb", - "lib/fakefs/dir.rb", - "lib/fakefs/fake/dir.rb", - "lib/fakefs/fake/file.rb", - "lib/fakefs/fake/symlink.rb", - "lib/fakefs/file.rb", - "lib/fakefs/file_system.rb", - "lib/fakefs/file_test.rb", - "lib/fakefs/fileutils.rb", - "lib/fakefs/safe.rb", - "lib/fakefs/spec_helpers.rb", - "lib/fakefs/version.rb", - "spec/fakefs/spec_helpers_spec.rb", - "spec/spec.opts", - "spec/spec_helper.rb", - "test/fake/file/lstat_test.rb", - "test/fake/file/stat_test.rb", - "test/fake/file/sysseek_test.rb", - "test/fake/file/syswrite_test.rb", - "test/fake/file_test.rb", - "test/fake/symlink_test.rb", - "test/fakefs_test.rb", - "test/file/stat_test.rb", - "test/safe_test.rb", - "test/test_helper.rb", - "test/verify.rb" + ".rspec", + "CONTRIBUTORS", + "Gemfile", + "Gemfile.lock", + "LICENSE", + "README.markdown", + "Rakefile", + "fakefs.gemspec", + "lib/fakefs.rb", + "lib/fakefs/base.rb", + "lib/fakefs/dir.rb", + "lib/fakefs/fake/dir.rb", + "lib/fakefs/fake/file.rb", + "lib/fakefs/fake/symlink.rb", + "lib/fakefs/file.rb", + "lib/fakefs/file_system.rb", + "lib/fakefs/file_test.rb", + "lib/fakefs/fileutils.rb", + "lib/fakefs/safe.rb", + "lib/fakefs/spec_helpers.rb", + "lib/fakefs/version.rb", + "spec/fakefs/spec_helpers_spec.rb", + "spec/spec.opts", + "spec/spec_helper.rb", + "test/fake/file/join_test.rb", + "test/fake/file/lstat_test.rb", + "test/fake/file/stat_test.rb", + "test/fake/file/sysseek_test.rb", + "test/fake/file/syswrite_test.rb", + "test/fake/file_test.rb", + "test/fake/symlink_test.rb", + "test/fakefs_test.rb", + "test/file/stat_test.rb", + "test/safe_test.rb", + "test/test_helper.rb", + "test/verify.rb" ] s.homepage = %q{http://github.com/defunkt/fakefs} - s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.7} + s.rubygems_version = %q{1.6.2} s.summary = %q{A fake filesystem. Use it in your tests.} - s.test_files = [ - "spec/fakefs/spec_helpers_spec.rb", - "spec/spec_helper.rb", - "test/fake/file/lstat_test.rb", - "test/fake/file/stat_test.rb", - "test/fake/file/sysseek_test.rb", - "test/fake/file/syswrite_test.rb", - "test/fake/file_test.rb", - "test/fake/symlink_test.rb", - "test/fakefs_test.rb", - "test/file/stat_test.rb", - "test/safe_test.rb", - "test/test_helper.rb", - "test/verify.rb" - ] if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end end From a0e02ed974bd127c8ffb586009eb7c049765801d Mon Sep 17 00:00:00 2001 From: Andrius Chamentauskas Date: Tue, 30 Aug 2011 11:19:30 +0300 Subject: [PATCH 15/40] Added fake FileTest.file? (Closes #94) --- lib/fakefs/file_test.rb | 4 ++++ test/fakefs_test.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/fakefs/file_test.rb b/lib/fakefs/file_test.rb index 5dbdbf5..2f6a61d 100644 --- a/lib/fakefs/file_test.rb +++ b/lib/fakefs/file_test.rb @@ -7,5 +7,9 @@ module FakeFS def self.directory?(file_name) File.directory?(file_name) end + + def self.file?(file_name) + File.file?(file_name) + end end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index e75aaf4..cc0041c 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -1584,6 +1584,18 @@ class FakeFSTest < Test::Unit::TestCase assert !FileTest.directory?('/path/to/somedir') end + def test_filetest_file_returns_correct_values + path = '/path/to/file.txt' + File.open(path, 'w') { |f| f.write "Yatta!" } + assert FileTest.file?(path) + + FileUtils.rm path + assert !FileTest.file?(path) + + FileUtils.mkdir_p '/path/to/somedir' + assert !FileTest.file?('/path/to/somedir') + end + def test_pathname_exists_returns_correct_value FileUtils.touch "foo" assert Pathname.new("foo").exist? From 14367140a32752ff44d4ab4afde340a5c58b8298 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Wed, 27 Jul 2011 21:59:56 -0400 Subject: [PATCH 16/40] Added basic File#to_path support (Closes #92) --- lib/fakefs/file.rb | 2 +- test/fakefs_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 01931c6..3c42208 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -358,7 +358,7 @@ module FakeFS end def to_path - raise NotImplementedError + @path end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index cc0041c..732c1d2 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -1663,5 +1663,11 @@ class FakeFSTest < Test::Unit::TestCase assert_equal false, f.autoclose? end end + + def test_to_path + File.new("foo", 'w') do |f| + assert_equal "foo", f.to_path + end + end end end From c24b338a2e5979a5ad44f3a69292d6e845003369 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Wed, 27 Jul 2011 21:47:11 -0400 Subject: [PATCH 17/40] Improved error handling for Dir.delete and Dir.entries This better matches the behavior of Ruby 1.8 and 1.9 (Closes #91) --- lib/fakefs/dir.rb | 5 +++-- test/fakefs_test.rb | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index b2d84fd..64ac8fd 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -65,12 +65,13 @@ module FakeFS end def self.delete(string) - raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty? + raise Errno::ENOENT, "No such file or directory - #{string}" unless FileSystem.find(string) + raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).values.empty? FileSystem.delete(string) end def self.entries(dirname) - raise SystemCallError, dirname unless FileSystem.find(dirname) + raise Errno::ENOENT, "No such file or directory - #{dirname}" unless FileSystem.find(dirname) Dir.new(dirname).map { |file| File.basename(file) } end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 732c1d2..80b4252 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -1211,11 +1211,17 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.touch("/this/path/should/be/here/#{f}") end - assert_raises(SystemCallError) do + assert_raises(Errno::ENOTEMPTY) do Dir.delete('/this/path/should/be/here') end end + def test_directory_class_delete_does_not_work_if_dir_path_cannot_be_found + assert_raises(Errno::ENOENT) do + Dir.delete('/this/path/should/not/be/here') + end + end + def test_directory_entries test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ] @@ -1244,6 +1250,12 @@ class FakeFSTest < Test::Unit::TestCase test.each { |t| assert yielded.include?(t) } end + def test_directory_entries_does_not_work_if_dir_path_cannot_be_found + assert_raises(Errno::ENOENT) do + Dir.delete('/this/path/should/not/be/here') + end + end + def test_directory_foreach test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ] From 019a7f06a367f2451216a438f05394f019a73dd0 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 14:24:48 -0400 Subject: [PATCH 18/40] Minor refactoring --- lib/fakefs/dir.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 64ac8fd..fa88476 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -2,8 +2,13 @@ module FakeFS class Dir include Enumerable + def self._check_for_valid_file(path) + raise Errno::ENOENT, "No such file or directory - #{path}" unless FileSystem.find(path) + end + def initialize(string) - raise Errno::ENOENT, string unless FileSystem.find(string) + self.class._check_for_valid_file(string) + @path = string @open = true @pointer = 0 @@ -65,13 +70,15 @@ module FakeFS end def self.delete(string) - raise Errno::ENOENT, "No such file or directory - #{string}" unless FileSystem.find(string) + _check_for_valid_file(string) raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).values.empty? + FileSystem.delete(string) end def self.entries(dirname) - raise Errno::ENOENT, "No such file or directory - #{dirname}" unless FileSystem.find(dirname) + _check_for_valid_file(dirname) + Dir.new(dirname).map { |file| File.basename(file) } end @@ -87,8 +94,12 @@ module FakeFS def self.mkdir(string, integer = 0) parent = string.split('/') parent.pop - raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || FileSystem.find(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) + FileUtils.mkdir_p(string) end From 54def31e4a52be8f4547688f2fa461fd73efb85b Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 25 Jun 2011 19:39:18 +0200 Subject: [PATCH 19/40] Fix test verifier (Closes #85) --- test/verify.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/verify.rb b/test/verify.rb index 52a4c10..4df051d 100644 --- a/test/verify.rb +++ b/test/verify.rb @@ -24,7 +24,7 @@ class FakeFSVerifierTest < Test::Unit::TestCase real_class.instance_methods.each do |method| define_method("test #{method} instance method") do - assert fake_class.instance_methods.include?(method), "#{fake_class}##{name} not implemented" + assert fake_class.instance_methods.include?(method), "#{fake_class}##{method} not implemented" end end end From 70a4d2621d2807649fef521b2731bb15881f2226 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 25 Jun 2011 19:30:38 +0200 Subject: [PATCH 20/40] Remove Gemfile.lock --- Gemfile.lock | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 97e60c2..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,35 +0,0 @@ -GEM - remote: http://rubygems.org/ - specs: - diff-lcs (1.1.2) - git (1.2.5) - jeweler (1.6.0) - bundler (~> 1.0.0) - git (>= 1.2.5) - rake - json (1.5.1) - rake (0.8.7) - rdiscount (1.6.8) - rdoc (2.4.3) - rspec (2.5.0) - rspec-core (~> 2.5.0) - rspec-expectations (~> 2.5.0) - rspec-mocks (~> 2.5.0) - rspec-core (2.5.1) - rspec-expectations (2.5.0) - diff-lcs (~> 1.1.2) - rspec-mocks (2.5.0) - sdoc (0.2.20) - json (>= 1.1.3) - rdoc (= 2.4.3) - sdoc-helpers (0.1.4) - sdoc (~> 0.2) - -PLATFORMS - ruby - -DEPENDENCIES - jeweler - rdiscount - rspec - sdoc-helpers From e81d0bdad114348e725c9bf6d7f1ba90273b1050 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sat, 25 Jun 2011 20:00:52 +0200 Subject: [PATCH 21/40] Add Gemfile.lock to .gitignore (Closes #84) --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 144394f..57a1d0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.sw? pkg - +Gemfile.lock From 2b13555845e1a910ddba756874102db3a9caef0e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 16:31:23 -0400 Subject: [PATCH 22/40] Add a note about rails. Closes #67 --- README.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.markdown b/README.markdown index 33708af..64eece9 100644 --- a/README.markdown +++ b/README.markdown @@ -47,6 +47,13 @@ Don't Fake the FS Immediately # your code end +Rails +----- + +If you are using fakefs in a rails project with bundler, you'll probably want to specify the following in your Gemfile: + + gem "fakefs", :require => "fakefs/safe" + RSpec ----- From 6c7fd0291ac8f0164e0a7dab1039f58926b3a83f Mon Sep 17 00:00:00 2001 From: Andrius Chamentauskas Date: Sat, 19 Feb 2011 13:01:11 +0200 Subject: [PATCH 23/40] Fix for finding files by patterns that include {..,..} (#65) --- lib/fakefs/file_system.rb | 3 ++- test/fakefs_test.rb | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index 2ab9b1f..f09f74c 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -121,7 +121,8 @@ module FakeFS directories_under(dir) end else - dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values + regexp_pattern = /\A#{pattern.gsub('?','.').gsub('*', '.*').gsub(/\{(.*?)\}/) { "(#{$1.split(',').join('|')})" }}\Z/ + dir.reject {|k,v| regexp_pattern !~ k }.values end if parts.empty? # we're done recursing diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 80b4252..b443512 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -573,6 +573,8 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.cp_r '/path', '/otherpath' assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*'] + + assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}'] end def test_dir_glob_handles_root From 8b0d10b94b686a441caa37b194b079c04f4d7cc9 Mon Sep 17 00:00:00 2001 From: Andrius Chamentauskas Date: Mon, 14 Mar 2011 18:34:45 +0200 Subject: [PATCH 24/40] Fix for {} patterns that have last element empty (Closes #65) --- lib/fakefs/file_system.rb | 2 +- test/fakefs_test.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index f09f74c..2367244 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -121,7 +121,7 @@ module FakeFS directories_under(dir) end else - regexp_pattern = /\A#{pattern.gsub('?','.').gsub('*', '.*').gsub(/\{(.*?)\}/) { "(#{$1.split(',').join('|')})" }}\Z/ + regexp_pattern = /\A#{pattern.gsub('?','.').gsub('*', '.*').gsub(/\{(.*?)\}/) { "(#{$1.gsub(',', '|')})" }}\Z/ dir.reject {|k,v| regexp_pattern !~ k }.values end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index b443512..53290c0 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -575,6 +575,8 @@ class FakeFSTest < Test::Unit::TestCase assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*'] assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}'] + + assert_equal ['/path/bar', '/path/bar2'], Dir['/path/bar{2,}'] end def test_dir_glob_handles_root From 1597e76c7f67edfd048a173b94f865e106883162 Mon Sep 17 00:00:00 2001 From: Jared Luxenberg Date: Wed, 23 Mar 2011 09:55:30 -0700 Subject: [PATCH 25/40] adding 'parent' attr_accessor to FakeFS::FakeSymlink (#69) --- lib/fakefs/fake/symlink.rb | 2 +- test/fakefs_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/fake/symlink.rb b/lib/fakefs/fake/symlink.rb index 5132542..750d095 100644 --- a/lib/fakefs/fake/symlink.rb +++ b/lib/fakefs/fake/symlink.rb @@ -1,6 +1,6 @@ module FakeFS class FakeSymlink - attr_accessor :name, :target + attr_accessor :name, :target, :parent alias_method :to_s, :name def initialize(target) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 53290c0..9d999f8 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -141,6 +141,12 @@ class FakeFSTest < Test::Unit::TestCase assert_equal target, File.readlink(link) end + def test_symlinks_in_different_directories + FileUtils.mkdir_p(target = "/path/to/foo/target") + FileUtils.ln_s(target, link = "/path/to/bar/symlink") + assert_equal target, File.readlink(link) + end + def test_knows_symlinks_are_symlinks FileUtils.mkdir_p(target = "/path/to/target") FileUtils.ln_s(target, link = "/path/to/symlink") From d98a06473e5988aefbf484318090fcb4b2571fb6 Mon Sep 17 00:00:00 2001 From: Jared Luxenberg Date: Wed, 23 Mar 2011 10:05:40 -0700 Subject: [PATCH 26/40] fixing FakeFS::FakeSymLink#to_s (Closes #69) --- lib/fakefs/fake/symlink.rb | 5 ++++- test/fakefs_test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/fake/symlink.rb b/lib/fakefs/fake/symlink.rb index 750d095..132eead 100644 --- a/lib/fakefs/fake/symlink.rb +++ b/lib/fakefs/fake/symlink.rb @@ -1,7 +1,6 @@ module FakeFS class FakeSymlink attr_accessor :name, :target, :parent - alias_method :to_s, :name def initialize(target) @target = target @@ -19,6 +18,10 @@ module FakeFS parent.delete(self) end + def to_s + File.join(parent.to_s, name) + end + def respond_to?(method) entry.respond_to?(method) end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 9d999f8..9384e59 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -147,6 +147,13 @@ class FakeFSTest < Test::Unit::TestCase assert_equal target, File.readlink(link) end + def test_symlinks_to_symlinks + FileUtils.mkdir_p(target = "/path/to/foo/target") + FileUtils.ln_s(target, link1 = "/path/to/bar/symlink") + FileUtils.ln_s(link1, link2 = "/path/to/bar2/symlink") + assert_equal link1, File.readlink(link2) + end + def test_knows_symlinks_are_symlinks FileUtils.mkdir_p(target = "/path/to/target") FileUtils.ln_s(target, link = "/path/to/symlink") From 6a2a821c8a405d15089a3078453ae82d24c69a3c Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:10:00 -0400 Subject: [PATCH 27/40] Raise a non implemented error --- lib/fakefs/dir.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index fa88476..379db08 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -66,7 +66,7 @@ module FakeFS end def self.chroot(string) - # Not implemented yet + raise NotImplementedError end def self.delete(string) From 973a2ae1f3c80ed0ac10ae62578431be8c9a2587 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:18:29 -0400 Subject: [PATCH 28/40] FileUtils.ln_s should raise an error if symlinking to a subpath which does not exist (and :force => true is not provided) --- lib/fakefs/dir.rb | 4 ++++ lib/fakefs/fileutils.rb | 10 +++++++++- test/fakefs_test.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 379db08..5d96a7d 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -61,6 +61,10 @@ module FakeFS glob(pattern) end + def self.exists?(path) + File.exists?(path) && File.directory?(path) + end + def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index 8a47856..d5a904a 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -40,9 +40,17 @@ module FakeFS def ln_s(target, path, options = {}) options = { :force => false }.merge(options) - (FileSystem.find(path) and !options[:force]) ? raise(Errno::EEXIST, path) : FileSystem.delete(path) + (FileSystem.find(path) && !options[:force]) ? + raise(Errno::EEXIST, path) : + FileSystem.delete(path) + + if !options[:force] && !Dir.exists?(File.dirname(path)) + raise Errno::ENOENT, path + end + FileSystem.add(path, FakeSymlink.new(target)) end + def ln_sf(target, path) ln_s(target, path, { :force => true }) end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 9384e59..445fc75 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -142,18 +142,33 @@ class FakeFSTest < Test::Unit::TestCase end def test_symlinks_in_different_directories + FileUtils.mkdir_p("/path/to/bar") FileUtils.mkdir_p(target = "/path/to/foo/target") + FileUtils.ln_s(target, link = "/path/to/bar/symlink") assert_equal target, File.readlink(link) end def test_symlinks_to_symlinks FileUtils.mkdir_p(target = "/path/to/foo/target") + FileUtils.mkdir_p("/path/to/bar") + FileUtils.mkdir_p("/path/to/bar2") + FileUtils.ln_s(target, link1 = "/path/to/bar/symlink") FileUtils.ln_s(link1, link2 = "/path/to/bar2/symlink") assert_equal link1, File.readlink(link2) end + def test_symlink_to_symlinks_should_raise_error_if_dir_doesnt_exist + FileUtils.mkdir_p(target = "/path/to/foo/target") + + assert !Dir.exists?("/path/to/bar") + + assert_raise Errno::ENOENT do + FileUtils.ln_s(target, "/path/to/bar/symlink") + end + end + def test_knows_symlinks_are_symlinks FileUtils.mkdir_p(target = "/path/to/target") FileUtils.ln_s(target, link = "/path/to/symlink") From 492d628994f99666cb4c4674a22c6ced04cd891d Mon Sep 17 00:00:00 2001 From: Eero Saynatkari Date: Sun, 8 May 2011 22:31:44 +0300 Subject: [PATCH 29/40] File.open fails given nonexistent path or path to a dir. Closes #82. * Removed unnecessary paths from existing tests (they don't need to exercise paths). * Added tests for nonexistent, existing and trying to open a directory. --- lib/fakefs/file.rb | 16 ++++++- test/fakefs_test.rb | 102 ++++++++++++++++++++++++++++++-------------- test/safe_test.rb | 2 +- 3 files changed, 87 insertions(+), 33 deletions(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 3c42208..1698b3a 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -398,8 +398,22 @@ module FakeFS (@mode & mask) != 0 if @mode.is_a?(Integer) end + # Create a missing file if the path's valid. + # def create_missing_file - if !File.exists?(@path) + raise Errno::EISDIR, "Is a directory - #{path}" if File.directory?(@path) + + if !File.exists?(@path) # Unnecessary check, probably. + dirname = RealFile.dirname @path + + unless dirname == "." + dir = FileSystem.find dirname + + unless dir.kind_of? FakeDir + raise Errno::ENOENT, "No such file or directory - #{path}" + end + end + @file = FileSystem.add(path, FakeFile.new) end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 445fc75..b59f3d4 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -175,8 +175,8 @@ class FakeFSTest < Test::Unit::TestCase assert File.symlink?(link) end - def test_can_create_files - path = '/path/to/file.txt' + def test_can_create_files_in_current_dir + path = 'file.txt' File.open(path, 'w') do |f| f.write "Yatta!" end @@ -186,7 +186,44 @@ class FakeFSTest < Test::Unit::TestCase assert File.writable?(path) end + def test_can_create_files_in_existing_dir + FileUtils.mkdir_p "/path/to" + path = "/path/to/file.txt" + + File.open(path, 'w') do |f| + f.write "Yatta!" + end + + assert File.exists?(path) + assert File.readable?(path) + assert File.writable?(path) + end + + def test_raises_ENOENT_trying_to_create_files_in_nonexistent_dir + path = "/path/to/file.txt" + + assert_raises(Errno::ENOENT) { + File.open(path, 'w') do |f| + f.write "Yatta!" + end + } + end + + def test_raises_EISDIR_if_trying_to_open_existing_directory_name + path = "/path/to" + + FileUtils.mkdir_p path + + assert_raises(Errno::EISDIR) { + File.open(path, 'w') do |f| + f.write "Yatta!" + end + } + end + def test_can_create_files_with_bitmasks + FileUtils.mkdir_p("/path/to") + path = '/path/to/file.txt' File.open(path, File::RDWR | File::CREAT) do |f| f.write "Yatta!" @@ -305,7 +342,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_read_files_once_written - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f.write "Yatta!" end @@ -314,7 +351,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_write_to_files - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << 'Yada Yada' end @@ -328,11 +365,12 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_open_file_in_binary_mode - File.open("/foo", "wb") { |x| x << "a" } - assert_equal "a", File.read("/foo") + File.open("foo", "wb") { |x| x << "a" } + assert_equal "a", File.read("foo") end def test_can_chunk_io_when_reading + FileUtils.mkdir_p "/path/to" path = '/path/to/file.txt' File.open(path, 'w') do |f| f << 'Yada Yada' @@ -346,7 +384,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_get_size_of_files - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << 'Yada Yada' end @@ -354,20 +392,20 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_check_if_file_has_size? - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << 'Yada Yada' end assert File.size?(path) - assert_nil File.size?("/path/to/other.txt") + assert_nil File.size?("other.txt") end def test_can_check_size_of_empty_file - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << '' end - assert_nil File.size?("/path/to/file.txt") + assert_nil File.size?("file.txt") end def test_raises_error_on_mtime_if_file_does_not_exist @@ -377,16 +415,16 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_return_mtime_on_existing_file - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << '' end - assert File.mtime('/path/to/file.txt').is_a?(Time) + assert File.mtime('file.txt').is_a?(Time) end def test_raises_error_on_ctime_if_file_does_not_exist assert_raise Errno::ENOENT do - File.ctime('/path/to/file.txt') + File.ctime('file.txt') end end @@ -446,16 +484,16 @@ class FakeFSTest < Test::Unit::TestCase def test_can_call_utime_on_an_existing_file time = Time.now - 300 # Not now - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f << '' end File.utime(time, time, path) - assert_equal time, File.mtime('/path/to/file.txt') + assert_equal time, File.mtime('file.txt') end def test_utime_returns_number_of_paths - path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt' + path1, path2 = 'file.txt', 'another_file.txt' [path1, path2].each do |path| File.open(path, 'w') do |f| f << '' @@ -465,7 +503,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_read_with_File_readlines - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f.puts "Yatta!", "Gatta!" f.puts ["woot","toot"] @@ -475,7 +513,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_File_close_disallows_further_access - path = '/path/to/file.txt' + path = 'file.txt' file = File.open(path, 'w') file.write 'Yada' file.close @@ -485,7 +523,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_File_close_disallows_further_writes - path = '/path/to/file.txt' + path = 'file.txt' file = File.open(path, 'w') file.write 'Yada' file.close @@ -495,7 +533,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_can_read_from_file_objects - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f.write "Yatta!" end @@ -510,7 +548,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_knows_files_are_files - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f.write "Yatta!" end @@ -519,17 +557,17 @@ class FakeFSTest < Test::Unit::TestCase end def test_File_io_returns_self - f = File.open("/foo", "w") + f = File.open("foo", "w") assert_equal f, f.to_io end def test_File_to_i_is_alias_for_filno - f = File.open("/foo", "w") + f = File.open("foo", "w") assert_equal f.method(:to_i), f.method(:fileno) end def test_knows_symlink_files_are_files - path = '/path/to/file.txt' + path = 'file.txt' File.open(path, 'w') do |f| f.write "Yatta!" end @@ -615,6 +653,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_dir_glob_handles_recursive_globs + FileUtils.mkdir_p "/one/two/three" File.open('/one/two/three/four.rb', 'w') File.open('/one/five.rb', 'w') assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb'] @@ -623,6 +662,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs + FileUtils.mkdir_p "/one/two/three" File.open('/one/two/three/four.rb', 'w') File.open('/one/five.rb', 'w') assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*'] @@ -640,7 +680,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_should_report_pos_as_0_when_opening - File.open("/foo", "w") do |f| + File.open("foo", "w") do |f| f << "foobar" f.rewind @@ -649,7 +689,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_should_report_pos_as_1_when_seeking_one_char - File.open("/foo", "w") do |f| + File.open("foo", "w") do |f| f << "foobar" f.rewind @@ -660,22 +700,22 @@ class FakeFSTest < Test::Unit::TestCase end def test_should_set_pos - File.open("/foo", "w") do |f| + File.open("foo", "w") do |f| f << "foo" end - fp = File.open("/foo", "r") + fp = File.open("foo", "r") fp.pos = 1 assert_equal 1, fp.pos end def test_should_set_pos_with_tell_method - File.open("/foo", "w") do |f| + File.open("foo", "w") do |f| f << "foo" end - fp = File.open("/foo", "r") + fp = File.open("foo", "r") fp.tell = 1 assert_equal 1, fp.pos diff --git a/test/safe_test.rb b/test/safe_test.rb index f16dc4e..3404e59 100644 --- a/test/safe_test.rb +++ b/test/safe_test.rb @@ -10,7 +10,7 @@ class FakeFSSafeTest < Test::Unit::TestCase end def test_FakeFS_method_does_not_intrude_on_global_namespace - path = '/path/to/file.txt' + path = 'file.txt' FakeFS do File.open(path, 'w') { |f| f.write "Yatta!" } From 9cbf2228a9f4a3bc4e31967b43bc5485e9ec5667 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:26:11 -0400 Subject: [PATCH 30/40] Rebase 492d62899 --- test/fakefs_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index b59f3d4..ae6d232 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -1669,6 +1669,8 @@ class FakeFSTest < Test::Unit::TestCase end def test_filetest_file_returns_correct_values + FileUtils.mkdir_p("/path/to") + path = '/path/to/file.txt' File.open(path, 'w') { |f| f.write "Yatta!" } assert FileTest.file?(path) From 552dc8edb08ffc78a3de7622d6eb5616db7c5801 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:27:02 -0400 Subject: [PATCH 31/40] minor comment change --- lib/fakefs/file.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 1698b3a..04e30c7 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -398,12 +398,12 @@ module FakeFS (@mode & mask) != 0 if @mode.is_a?(Integer) end - # Create a missing file if the path's valid. + # Create a missing file if the path is valid. # def create_missing_file raise Errno::EISDIR, "Is a directory - #{path}" if File.directory?(@path) - if !File.exists?(@path) # Unnecessary check, probably. + if !File.exists?(@path) # Unnecessary check, probably. dirname = RealFile.dirname @path unless dirname == "." From 47b34075a87505f791427e9ac5fd57008586b317 Mon Sep 17 00:00:00 2001 From: Eero Saynatkari Date: Sun, 8 May 2011 22:44:03 +0300 Subject: [PATCH 32/40] Added some tests for relative nonexistent dirs to open. --- test/fakefs_test.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index ae6d232..bcb487a 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -209,6 +209,36 @@ class FakeFSTest < Test::Unit::TestCase } end + def test_raises_ENOENT_trying_to_create_files_in_relative_nonexistent_dir + FileUtils.mkdir_p "/some/path" + + Dir.chdir("/some/path") { + assert_raises(Errno::ENOENT) { + File.open("../foo") {|f| f.write "moo" } + } + } + end + + def test_raises_ENOENT_trying_to_create_files_in_obscured_nonexistent_dir + FileUtils.mkdir_p "/some/path" + + assert_raises(Errno::ENOENT) { + File.open("/some/path/../foo") {|f| f.write "moo" } + } + end + + def test_raises_ENOENT_trying_to_create_tilde_referenced_nonexistent_dir + path = "~/fakefs_test_#{$$}_0000" + + while File.exist? path + path = path.succ + end + + assert_raises(Errno::ENOENT) { + File.open("#{path}/foo") {|f| f.write "moo" } + } + end + def test_raises_EISDIR_if_trying_to_open_existing_directory_name path = "/path/to" From 4da1d58d66a729c2e40947c2212109cbbde39b16 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:34:09 -0400 Subject: [PATCH 33/40] FileUtils#rm and File#delete should raise Errno::ENOENT when any file does not exist (Closes #70) --- lib/fakefs/file_system.rb | 1 + lib/fakefs/fileutils.rb | 2 +- test/fakefs_test.rb | 14 ++++++-------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index 2367244..1c6f851 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -68,6 +68,7 @@ module FakeFS def delete(path) if node = FileSystem.find(path) node.delete + true end end diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index d5a904a..81ff2d7 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -30,7 +30,7 @@ module FakeFS def rm(list, options = {}) Array(list).each do |path| - FileSystem.delete(path) + FileSystem.delete(path) or raise Errno::ENOENT.new(path) end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index bcb487a..eda6ea2 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -65,6 +65,12 @@ class FakeFSTest < Test::Unit::TestCase assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] end + def test_unlink_errors_on_file_not_found + assert_raise Errno::ENOENT do + FileUtils.rm("/foo") + end + end + def test_can_delete_directories FileUtils.mkdir_p("/path/to/dir") FileUtils.rmdir("/path/to/dir") @@ -1582,14 +1588,6 @@ class FakeFSTest < Test::Unit::TestCase end end - def test_delete_does_not_raise_error_when_second_file_does_not_exist - FileUtils.touch("/foo") - - assert_nothing_raised do - File.delete("/foo", "/bar") - end - end - def test_unlink_is_alias_for_delete assert_equal File.method(:unlink), File.method(:delete) end From 600b1150ef498636c42979abbd394f434fa37568 Mon Sep 17 00:00:00 2001 From: Jared Luxenberg Date: Wed, 23 Mar 2011 17:33:25 -0700 Subject: [PATCH 34/40] * fixing File#exist? for symlinks (should return false when target of symlink does not exist) * changing name of root directory to '/' * support for relative targets for symlinks - File#readlink should return the target of the symlink (Closes #71) --- lib/fakefs/file.rb | 9 +++++++-- lib/fakefs/file_system.rb | 2 +- test/fakefs_test.rb | 32 +++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 04e30c7..2ed69e2 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -37,7 +37,12 @@ module FakeFS end def self.exist?(path) - !!FileSystem.find(path) + if(File.symlink?(path)) then + referent = File.expand_path(File.readlink(path), File.dirname(path)) + exist?(referent) + else + !!FileSystem.find(path) + end end class << self @@ -132,7 +137,7 @@ module FakeFS def self.readlink(path) symlink = FileSystem.find(path) - FileSystem.find(symlink.target).to_s + symlink.target end def self.read(path) diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index 1c6f851..a5c8087 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -7,7 +7,7 @@ module FakeFS end def fs - @fs ||= FakeDir.new('.') + @fs ||= FakeDir.new('/') end def clear diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index eda6ea2..ffb1169 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -117,6 +117,11 @@ class FakeFSTest < Test::Unit::TestCase assert File.exists?(path) end + def test_symlink_with_missing_refferent_does_not_exist + File.symlink('/foo', '/bar') + assert !File.exists?('/bar') + end + def test_can_create_symlinks FileUtils.mkdir_p(target = "/path/to/target") FileUtils.ln_s(target, "/path/to/link") @@ -155,6 +160,27 @@ class FakeFSTest < Test::Unit::TestCase assert_equal target, File.readlink(link) end + def test_symlink_with_relative_path_exists + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file", link = "/a/b/symlink") + assert File.exist?('/a/b/symlink') + end + + def test_symlink_with_relative_path_and_nonexistant_file_does_not_exist + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file_foo", link = "/a/b/symlink") + assert !File.exist?('/a/b/symlink') + end + + def test_symlink_with_relative_path_has_correct_target + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file", link = "/a/b/symlink") + assert_equal "../../file", File.readlink(link) + end + def test_symlinks_to_symlinks FileUtils.mkdir_p(target = "/path/to/foo/target") FileUtils.mkdir_p("/path/to/bar") @@ -685,7 +711,7 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.mkdir_p '/path' # this fails. the root dir should be named '/' but it is '.' - #assert_equal ['/'], Dir['/'] + assert_equal ['/'], Dir['/'] end def test_dir_glob_handles_recursive_globs @@ -781,14 +807,14 @@ class FakeFSTest < Test::Unit::TestCase def test_chdir_changes_directories_like_a_boss # I know memes! FileUtils.mkdir_p '/path' - assert_equal '.', FileSystem.fs.name + assert_equal '/', FileSystem.fs.name assert_equal({}, FileSystem.fs['path']) Dir.chdir '/path' do File.open('foo', 'w') { |f| f.write 'foo'} File.open('foobar', 'w') { |f| f.write 'foo'} end - assert_equal '.', FileSystem.fs.name + assert_equal '/', FileSystem.fs.name assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort) c = nil From e4cb4085583a8e355834000439751d2849edeab3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:50:29 -0400 Subject: [PATCH 35/40] Update contributors --- CONTRIBUTORS | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7fc39ac..3a342a2 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -4,27 +4,32 @@ Chris Wanstrath Myles Eftos Jeff Hodges Matt Freels +Eero Saynatkari Víctor Martínez Nick Quaranto Aaron Suggs Victor Costan Eric MSP Veith Jon Yurek +Jared Luxenberg +Lars Gierth Greg Campbell Ben Mabey Mark Sam Goldstein +Ryan McGeary Noah Paessel dmathieu <42@dmathieu.com> Mariusz Pietrzyk -Lars Gierth marano jameswilding Tymon Tobolski Scott Barron +Andrius Chamentauskas Keita Urashima David Reese msassak +Andrius Chamentauskas timo3377 Mislav Marohnić Rob Sanheim From da5e6210b285384a5df77c403c6161d267718cfc Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:54:14 -0400 Subject: [PATCH 36/40] Version bump - 0.4 --- lib/fakefs/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fakefs/version.rb b/lib/fakefs/version.rb index 4cf4940..aa0ad20 100644 --- a/lib/fakefs/version.rb +++ b/lib/fakefs/version.rb @@ -1,6 +1,6 @@ module FakeFS module Version - VERSION = "0.3.2" + VERSION = "0.4.0" def self.to_s VERSION From 8fedf0dc1ba1967d2761e1edf2025d9e6414cd5d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:55:55 -0400 Subject: [PATCH 37/40] Update gemspec for 0.4.0 --- fakefs.gemspec | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fakefs.gemspec b/fakefs.gemspec index 4a96b83..f2ab64a 100644 --- a/fakefs.gemspec +++ b/fakefs.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{fakefs} - s.version = "0.3.2" + s.version = "0.4.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"] - s.date = %q{2011-05-02} + s.date = %q{2011-09-05} s.description = %q{A fake filesystem. Use it in your tests.} s.email = %q{chris@ozmm.org} s.extra_rdoc_files = [ @@ -21,7 +21,6 @@ Gem::Specification.new do |s| ".rspec", "CONTRIBUTORS", "Gemfile", - "Gemfile.lock", "LICENSE", "README.markdown", "Rakefile", From ec9c54e373055e1a4c38d0b3e64a22d59a551273 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 18:08:15 -0400 Subject: [PATCH 38/40] Add releasing notes --- README.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.markdown b/README.markdown index 64eece9..938130f 100644 --- a/README.markdown +++ b/README.markdown @@ -128,3 +128,10 @@ Meta [0]: http://help.github.com/forking/ [1]: http://github.com/defunkt/fakefs/issues + +Releasing +--------- + +1. Update version in lib/fakefs/version.rb +2. Commit it +3. rake publish \ No newline at end of file From 900be5173feb08597786960bb9d1499afd11973c Mon Sep 17 00:00:00 2001 From: marano Date: Fri, 19 Nov 2010 17:13:53 -0200 Subject: [PATCH 39/40] Dir glob will accept multiple arguments. --- lib/fakefs/dir.rb | 14 ++++++++++---- test/fakefs_test.rb | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 32c5234..eef9b6c 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -52,8 +52,8 @@ module FakeFS @contents[integer] end - def self.[](pattern) - glob(pattern) + def self.[](*pattern) + glob pattern end def self.chdir(dir, &blk) @@ -79,8 +79,14 @@ module FakeFS end def self.glob(pattern, &block) - files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort - block_given? ? files.each { |file| block.call(file) } : files + matches_for_pattern = lambda { |matcher| [FileSystem.find(matcher) || []].flatten.map{|e| e.to_s}.sort } + + if pattern.is_a? Array + files = pattern.collect { |matcher| matches_for_pattern.call matcher }.flatten + else + files = matches_for_pattern.call pattern + end + return block_given? ? files.each { |file| block.call(file) } : files end def self.mkdir(string, integer = 0) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index f041baf..8ba5ed5 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -561,6 +561,9 @@ class FakeFSTest < Test::Unit::TestCase assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*'] + assert_equal ['/path/bar/baz', '/path/bar2/baz'], Dir['/path/bar/**/*', '/path/bar2/**/*'] + assert_equal ['/path/bar/baz', '/path/bar2/baz', '/path/bar/baz'], Dir['/path/ba*/**/*', '/path/bar/**/*'] + FileUtils.cp_r '/path', '/otherpath' assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*'] From 743477f4f421c3b4f7f1c20e661b2d9e8ee0c7a4 Mon Sep 17 00:00:00 2001 From: Maarten Hoogendoorn Date: Tue, 25 Oct 2011 16:44:13 +0200 Subject: [PATCH 40/40] Add support for atime --- lib/fakefs/fake/dir.rb | 3 ++- lib/fakefs/fake/file.rb | 3 ++- lib/fakefs/file.rb | 15 +++++++++++-- test/fakefs_test.rb | 48 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/lib/fakefs/fake/dir.rb b/lib/fakefs/fake/dir.rb index f72ea4f..068a427 100644 --- a/lib/fakefs/fake/dir.rb +++ b/lib/fakefs/fake/dir.rb @@ -1,13 +1,14 @@ module FakeFS class FakeDir < Hash attr_accessor :name, :parent - attr_reader :ctime, :mtime + attr_reader :ctime, :mtime, :atime def initialize(name = nil, parent = nil) @name = name @parent = parent @ctime = Time.now @mtime = @ctime + @atime = @ctime end def entry diff --git a/lib/fakefs/fake/file.rb b/lib/fakefs/fake/file.rb index ec5086f..10bdf7f 100644 --- a/lib/fakefs/fake/file.rb +++ b/lib/fakefs/fake/file.rb @@ -1,6 +1,6 @@ module FakeFS class FakeFile - attr_accessor :name, :parent, :content, :mtime + attr_accessor :name, :parent, :content, :mtime, :atime attr_reader :ctime class Inode @@ -34,6 +34,7 @@ module FakeFS @inode = Inode.new(self) @ctime = Time.now @mtime = @ctime + @atime = @ctime end attr_accessor :inode diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 2ed69e2..5684ede 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -69,9 +69,18 @@ module FakeFS end end + def self.atime(path) + if exists?(path) + FileSystem.find(path).atime + else + raise Errno::ENOENT + end + end + def self.utime(atime, mtime, *paths) paths.each do |path| if exists?(path) + FileSystem.find(path).atime = atime FileSystem.find(path).mtime = mtime else raise Errno::ENOENT @@ -143,6 +152,7 @@ module FakeFS def self.read(path) file = new(path) if file.exists? + FileSystem.find(path).atime = Time.now file.read else raise Errno::ENOENT @@ -225,7 +235,7 @@ module FakeFS end class Stat - attr_reader :ctime, :mtime + attr_reader :ctime, :mtime, :atime def initialize(file, __lstat = false) if !File.exists?(file) @@ -237,6 +247,7 @@ module FakeFS @__lstat = __lstat @ctime = @fake_file.ctime @mtime = @fake_file.mtime + @atime = @fake_file.atime end def symlink? @@ -326,7 +337,7 @@ module FakeFS end def atime - raise NotImplementedError + self.class.atime(@path) end def chmod(mode_int) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index ffb1169..6223b51 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -495,27 +495,45 @@ class FakeFSTest < Test::Unit::TestCase assert File.ctime('foo').is_a?(Time) end - def test_ctime_and_mtime_are_equal_for_new_files + def test_raises_error_on_atime_if_file_does_not_exist + assert_raise Errno::ENOENT do + File.atime('file.txt') + end + end + + def test_can_return_atime_on_existing_file + File.open("foo", "w") { |f| f << "some content" } + assert File.atime('foo').is_a?(Time) + end + + def test_ctime_mtime_and_atime_are_equal_for_new_files File.open("foo", "w") { |f| f << "some content" } ctime = File.ctime("foo") mtime = File.mtime("foo") + atime = File.atime("foo") assert ctime.is_a?(Time) assert mtime.is_a?(Time) + assert atime.is_a?(Time) assert_equal ctime, mtime + assert_equal ctime, atime File.open("foo", "r") do |f| assert_equal ctime, f.ctime assert_equal mtime, f.mtime + assert_equal atime, f.atime end end - def test_ctime_and_mtime_are_equal_for_new_directories + def test_ctime_mtime_and_atime_are_equal_for_new_directories FileUtils.mkdir_p("foo") ctime = File.ctime("foo") mtime = File.mtime("foo") + atime = File.atime("foo") assert ctime.is_a?(Time) assert mtime.is_a?(Time) + assert atime.is_a?(Time) assert_equal ctime, mtime + assert_equal ctime, atime end def test_file_ctime_is_equal_to_file_stat_ctime @@ -538,6 +556,16 @@ class FakeFSTest < Test::Unit::TestCase assert_equal File.stat("foo").mtime, File.mtime("foo") end + def test_file_atime_is_equal_to_file_stat_atime + File.open("foo", "w") { |f| f << "some content" } + assert_equal File.stat("foo").atime, File.atime("foo") + end + + def test_directory_atime_is_equal_to_directory_stat_atime + FileUtils.mkdir_p("foo") + assert_equal File.stat("foo").atime, File.atime("foo") + end + def test_utime_raises_error_if_path_does_not_exist assert_raise Errno::ENOENT do File.utime(Time.now, Time.now, '/path/to/file.txt') @@ -552,6 +580,7 @@ class FakeFSTest < Test::Unit::TestCase end File.utime(time, time, path) assert_equal time, File.mtime('file.txt') + assert_equal time, File.atime('file.txt') end def test_utime_returns_number_of_paths @@ -564,6 +593,21 @@ class FakeFSTest < Test::Unit::TestCase assert_equal 2, File.utime(Time.now, Time.now, path1, path2) end + def test_file_a_time_updated_when_file_is_read + old_atime = Time.now - 300 + + path = "file.txt" + File.open(path, "w") do |f| + f << "Hello" + end + + File.utime(old_atime, File.mtime(path), path) + + assert_equal File.atime(path), old_atime + File.read(path) + assert File.atime(path) != old_atime + end + def test_can_read_with_File_readlines path = 'file.txt' File.open(path, 'w') do |f|