Compare commits

...

45 Commits

Author SHA1 Message Date
John Bintz
1b88eccb5f merge up to master 2011-11-08 06:45:33 -05:00
Eero Saynatkari
b363a8d470 Merge pull request #101 from marano/issue_100
Dir glob will accept multiple arguments.
2011-10-25 09:51:47 -07:00
Eero Saynatkari
69e300abd4 Merge pull request #108 from moretea/support_for_atime
Added support for atime
2011-10-25 08:24:10 -07:00
Maarten Hoogendoorn
743477f4f4 Add support for atime 2011-10-25 16:44:13 +02:00
marano
900be5173f Dir glob will accept multiple arguments. 2011-09-15 19:30:54 -03:00
Scott Taylor
ec9c54e373 Add releasing notes 2011-09-05 18:08:15 -04:00
Scott Taylor
8fedf0dc1b Update gemspec for 0.4.0 2011-09-05 17:55:55 -04:00
Scott Taylor
da5e6210b2 Version bump - 0.4 2011-09-05 17:54:14 -04:00
Scott Taylor
e4cb408558 Update contributors 2011-09-05 17:50:29 -04:00
Jared Luxenberg
600b1150ef * 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)
2011-09-05 17:38:39 -04:00
Scott Taylor
4da1d58d66 FileUtils#rm and File#delete should raise Errno::ENOENT when any file
does not exist (Closes #70)
2011-09-05 17:34:09 -04:00
Eero Saynatkari
47b34075a8 Added some tests for relative nonexistent dirs to open. 2011-09-05 17:27:17 -04:00
Scott Taylor
552dc8edb0 minor comment change 2011-09-05 17:27:02 -04:00
Scott Taylor
9cbf2228a9 Rebase 492d62899 2011-09-05 17:26:11 -04:00
Eero Saynatkari
492d628994 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.
2011-09-05 17:25:18 -04:00
Scott Taylor
973a2ae1f3 FileUtils.ln_s should raise an error if symlinking to a subpath
which does not exist (and :force => true is not provided)
2011-09-05 17:19:52 -04:00
Scott Taylor
6a2a821c8a Raise a non implemented error 2011-09-05 17:19:52 -04:00
Jared Luxenberg
d98a06473e fixing FakeFS::FakeSymLink#to_s (Closes #69) 2011-09-05 17:19:39 -04:00
Jared Luxenberg
1597e76c7f adding 'parent' attr_accessor to FakeFS::FakeSymlink (#69) 2011-09-05 16:51:10 -04:00
Andrius Chamentauskas
8b0d10b94b Fix for {} patterns that have last element empty (Closes #65) 2011-09-05 16:46:26 -04:00
Andrius Chamentauskas
6c7fd0291a Fix for finding files by patterns that include {..,..} (#65) 2011-09-05 16:45:35 -04:00
Scott Taylor
2b13555845 Add a note about rails. Closes #67 2011-09-05 16:31:23 -04:00
Lars Gierth
e81d0bdad1 Add Gemfile.lock to .gitignore (Closes #84) 2011-09-05 14:38:46 -04:00
Lars Gierth
70a4d2621d Remove Gemfile.lock 2011-09-05 14:38:34 -04:00
Lars Gierth
54def31e4a Fix test verifier (Closes #85) 2011-09-05 14:37:32 -04:00
Scott Taylor
019a7f06a3 Minor refactoring 2011-09-05 14:24:48 -04:00
Ryan McGeary
c24b338a2e Improved error handling for Dir.delete and Dir.entries
This better matches the behavior of Ruby 1.8 and 1.9

(Closes #91)
2011-09-05 14:20:21 -04:00
Ryan McGeary
14367140a3 Added basic File#to_path support (Closes #92) 2011-09-05 14:15:12 -04:00
Andrius Chamentauskas
a0e02ed974 Added fake FileTest.file? (Closes #94) 2011-09-05 14:10:16 -04:00
John Bintz
7363b6f13b flock can't break 2011-05-21 10:10:23 -04:00
Scott Taylor
1e074bf636 update gemspec 2011-05-02 23:06:29 -04:00
Scott Taylor
3762fa2b35 update version to 0.3.2 2011-05-02 23:05:08 -04:00
Scott Taylor
1f948a0fd7 update contributors 2011-05-02 22:54:26 -04:00
Mariusz Pietrzyk
8bf4b5aeb0 Raising Errno::ENOENT exception with a particular path instead of src, which may be an array.
Closes #59
2011-05-02 22:32:02 -04:00
Noah Paessel
3916ffc91d Be polite; add new test to bottom of test file. 2011-05-02 22:11:53 -04:00
Noah Paessel
2e55319dc0 add file#split. Closes #61 2011-05-02 22:11:21 -04:00
Scott Taylor
129b557160 add dev dependencies: jeweler, sdoc-helpers, rdiscount 2011-05-02 22:02:59 -04:00
Scott Taylor
c2d7f46bcb move around and reformat code 2011-05-02 22:02:59 -04:00
Scott Taylor
903b8c5fd8 Add source :rubygems to Gemfile 2011-05-02 22:02:59 -04:00
Eric MSP Veith
564387b6a3 Added Dir.mktmpdir and FileTest.directory? Closes #74 2011-05-02 22:00:05 -04:00
Scott Taylor
f5913573ef add .rspec file to run specs with color 2011-05-02 21:39:52 -04:00
Scott Taylor
b556f7ba24 Add rspec 2 as a dev only dependency 2011-05-02 21:39:52 -04:00
Scott Taylor
fa612b194b Update rake tasks to use rspec 2. 2011-05-02 21:39:52 -04:00
timo3377
12532c8e37 Alias mkdir_p + makedirs for compatability with Rails3. Closes #79 2011-05-02 21:37:37 -04:00
John Bintz
005ddaaeb2 allow for {p,q} literals in globs 2011-02-18 16:14:13 -05:00
20 changed files with 541 additions and 143 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
*.sw? *.sw?
pkg pkg
Gemfile.lock

1
.rspec Normal file
View File

@ -0,0 +1 @@
--color

View File

@ -4,24 +4,32 @@ Chris Wanstrath <chris@ozmm.org>
Myles Eftos <myles@madpilot.com.au> Myles Eftos <myles@madpilot.com.au>
Jeff Hodges <jeff@somethingsimilar.com> Jeff Hodges <jeff@somethingsimilar.com>
Matt Freels <matt@freels.name> Matt Freels <matt@freels.name>
Eero Saynatkari <projects@kittensoft.org>
Víctor Martínez <knoopx@gmail.com> Víctor Martínez <knoopx@gmail.com>
Nick Quaranto <nick@quaran.to> Nick Quaranto <nick@quaran.to>
Aaron Suggs <aaron@ktheory.com> Aaron Suggs <aaron@ktheory.com>
Victor Costan <costan@gmail.com> Victor Costan <costan@gmail.com>
Eric MSP Veith <eveith@wwweb-library.net>
Jon Yurek <jyurek@thoughtbot.com> Jon Yurek <jyurek@thoughtbot.com>
Jared Luxenberg <jared@jaredlux.com>
Lars Gierth <lars.gierth@gmail.com>
Greg Campbell <gtcampbell@gmail.com> Greg Campbell <gtcampbell@gmail.com>
Ben Mabey <ben@benmabey.com> Ben Mabey <ben@benmabey.com>
Mark <mark@amerine.net> Mark <mark@amerine.net>
Sam Goldstein <sam@aboutus.org> Sam Goldstein <sam@aboutus.org>
Ryan McGeary <ryan@mcgeary.org>
Noah Paessel <knowuh@gmail.com>
dmathieu <42@dmathieu.com> dmathieu <42@dmathieu.com>
Lars Gierth <lars.gierth@gmail.com> Mariusz Pietrzyk <wijet@wijet.pl>
marano <thiagomarano@gmail.com> marano <thiagomarano@gmail.com>
jameswilding <james@jameswilding.net> jameswilding <james@jameswilding.net>
Scott Barron <scott@elitists.net>
Tymon Tobolski <i@teamon.eu> Tymon Tobolski <i@teamon.eu>
Mariusz Pietrzyk <wijet@wijet.pl> Scott Barron <scott@elitists.net>
Andrius Chamentauskas <andrius.chamentauskas@gmail.com>
Keita Urashima <ursm@ursm.jp> Keita Urashima <ursm@ursm.jp>
David Reese <david@whatcould.com> David Reese <david@whatcould.com>
msassak <msassak@gmail.com> msassak <msassak@gmail.com>
Andrius Chamentauskas <sinsiliux@gmail.com>
timo3377 <tim.linquist@gmail.com>
Mislav Marohnić <mislav.marohnic@gmail.com> Mislav Marohnić <mislav.marohnic@gmail.com>
Rob Sanheim <rsanheim@gmail.com> Rob Sanheim <rsanheim@gmail.com>

8
Gemfile Normal file
View File

@ -0,0 +1,8 @@
source :rubygems
group :development do
gem 'rspec'
gem 'jeweler'
gem 'sdoc-helpers'
gem 'rdiscount'
end

View File

@ -47,6 +47,13 @@ Don't Fake the FS Immediately
# your code # your code
end 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 RSpec
----- -----
@ -121,3 +128,10 @@ Meta
[0]: http://help.github.com/forking/ [0]: http://help.github.com/forking/
[1]: http://github.com/defunkt/fakefs/issues [1]: http://github.com/defunkt/fakefs/issues
Releasing
---------
1. Update version in lib/fakefs/version.rb
2. Commit it
3. rake publish

View File

@ -8,16 +8,9 @@ end
task :default => [:test, :spec] task :default => [:test, :spec]
begin require 'rspec/core/rake_task'
require 'spec/rake/spectask'
desc "Run specs" desc "Run specs"
Spec::Rake::SpecTask.new(:spec) do |t| RSpec::Core::RakeTask.new
t.spec_files = FileList["spec/**/*.rb"]
end
rescue LoadError
puts "Spec task can't be loaded. `gem install rspec`"
end
begin begin
require 'jeweler' require 'jeweler'

View File

@ -1,15 +1,15 @@
# Generated by jeweler # Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY # 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 -*- # -*- encoding: utf-8 -*-
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{fakefs} s.name = %q{fakefs}
s.version = "0.3.1" s.version = "0.4.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 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.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"]
s.date = %q{2010-12-18} s.date = %q{2011-09-05}
s.description = %q{A fake filesystem. Use it in your tests.} s.description = %q{A fake filesystem. Use it in your tests.}
s.email = %q{chris@ozmm.org} s.email = %q{chris@ozmm.org}
s.extra_rdoc_files = [ s.extra_rdoc_files = [
@ -18,8 +18,9 @@ Gem::Specification.new do |s|
] ]
s.files = [ s.files = [
".autotest", ".autotest",
".gitignore", ".rspec",
"CONTRIBUTORS", "CONTRIBUTORS",
"Gemfile",
"LICENSE", "LICENSE",
"README.markdown", "README.markdown",
"Rakefile", "Rakefile",
@ -40,6 +41,7 @@ Gem::Specification.new do |s|
"spec/fakefs/spec_helpers_spec.rb", "spec/fakefs/spec_helpers_spec.rb",
"spec/spec.opts", "spec/spec.opts",
"spec/spec_helper.rb", "spec/spec_helper.rb",
"test/fake/file/join_test.rb",
"test/fake/file/lstat_test.rb", "test/fake/file/lstat_test.rb",
"test/fake/file/stat_test.rb", "test/fake/file/stat_test.rb",
"test/fake/file/sysseek_test.rb", "test/fake/file/sysseek_test.rb",
@ -53,34 +55,29 @@ Gem::Specification.new do |s|
"test/verify.rb" "test/verify.rb"
] ]
s.homepage = %q{http://github.com/defunkt/fakefs} s.homepage = %q{http://github.com/defunkt/fakefs}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] 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.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 if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3 s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 0"])
s.add_development_dependency(%q<jeweler>, [">= 0"])
s.add_development_dependency(%q<sdoc-helpers>, [">= 0"])
s.add_development_dependency(%q<rdiscount>, [">= 0"])
else else
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
s.add_dependency(%q<sdoc-helpers>, [">= 0"])
s.add_dependency(%q<rdiscount>, [">= 0"])
end end
else else
s.add_dependency(%q<rspec>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
s.add_dependency(%q<sdoc-helpers>, [">= 0"])
s.add_dependency(%q<rdiscount>, [">= 0"])
end end
end end

View File

@ -2,8 +2,13 @@ module FakeFS
class Dir class Dir
include Enumerable 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) def initialize(string)
raise Errno::ENOENT, string unless FileSystem.find(string) self.class._check_for_valid_file(string)
@path = string @path = string
@open = true @open = true
@pointer = 0 @pointer = 0
@ -52,8 +57,12 @@ module FakeFS
@contents[integer] @contents[integer]
end end
def self.[](pattern) def self.[](*pattern)
glob(pattern) glob pattern
end
def self.exists?(path)
File.exists?(path) && File.directory?(path)
end end
def self.chdir(dir, &blk) def self.chdir(dir, &blk)
@ -61,16 +70,19 @@ module FakeFS
end end
def self.chroot(string) def self.chroot(string)
# Not implemented yet raise NotImplementedError
end end
def self.delete(string) def self.delete(string)
raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty? _check_for_valid_file(string)
raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).values.empty?
FileSystem.delete(string) FileSystem.delete(string)
end end
def self.entries(dirname) def self.entries(dirname)
raise SystemCallError, dirname unless FileSystem.find(dirname) _check_for_valid_file(dirname)
Dir.new(dirname).map { |file| File.basename(file) } Dir.new(dirname).map { |file| File.basename(file) }
end end
@ -79,15 +91,25 @@ module FakeFS
end end
def self.glob(pattern, &block) def self.glob(pattern, &block)
files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort matches_for_pattern = lambda { |matcher| [FileSystem.find(matcher) || []].flatten.map{|e| e.to_s}.sort }
block_given? ? files.each { |file| block.call(file) } : files
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 end
def self.mkdir(string, integer = 0) def self.mkdir(string, integer = 0)
parent = string.split('/') parent = string.split('/')
parent.pop 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) raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)
FileUtils.mkdir_p(string) FileUtils.mkdir_p(string)
end end
@ -107,6 +129,52 @@ module FakeFS
FileSystem.current_dir.to_s FileSystem.current_dir.to_s
end 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 class << self
alias_method :getwd, :pwd alias_method :getwd, :pwd
alias_method :rmdir, :delete alias_method :rmdir, :delete

View File

@ -1,13 +1,14 @@
module FakeFS module FakeFS
class FakeDir < Hash class FakeDir < Hash
attr_accessor :name, :parent attr_accessor :name, :parent
attr_reader :ctime, :mtime attr_reader :ctime, :mtime, :atime
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)
@name = name @name = name
@parent = parent @parent = parent
@ctime = Time.now @ctime = Time.now
@mtime = @ctime @mtime = @ctime
@atime = @ctime
end end
def entry def entry

View File

@ -1,6 +1,6 @@
module FakeFS module FakeFS
class FakeFile class FakeFile
attr_accessor :name, :parent, :content, :mtime attr_accessor :name, :parent, :content, :mtime, :atime
attr_reader :ctime attr_reader :ctime
class Inode class Inode
@ -34,6 +34,7 @@ module FakeFS
@inode = Inode.new(self) @inode = Inode.new(self)
@ctime = Time.now @ctime = Time.now
@mtime = @ctime @mtime = @ctime
@atime = @ctime
end end
attr_accessor :inode attr_accessor :inode

View File

@ -1,7 +1,6 @@
module FakeFS module FakeFS
class FakeSymlink class FakeSymlink
attr_accessor :name, :target attr_accessor :name, :target, :parent
alias_method :to_s, :name
def initialize(target) def initialize(target)
@target = target @target = target
@ -19,6 +18,10 @@ module FakeFS
parent.delete(self) parent.delete(self)
end end
def to_s
File.join(parent.to_s, name)
end
def respond_to?(method) def respond_to?(method)
entry.respond_to?(method) entry.respond_to?(method)
end end

View File

@ -37,8 +37,13 @@ module FakeFS
end end
def self.exist?(path) def self.exist?(path)
if(File.symlink?(path)) then
referent = File.expand_path(File.readlink(path), File.dirname(path))
exist?(referent)
else
!!FileSystem.find(path) !!FileSystem.find(path)
end end
end
class << self class << self
alias_method :exists?, :exist? alias_method :exists?, :exist?
@ -64,9 +69,18 @@ module FakeFS
end end
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) def self.utime(atime, mtime, *paths)
paths.each do |path| paths.each do |path|
if exists?(path) if exists?(path)
FileSystem.find(path).atime = atime
FileSystem.find(path).mtime = mtime FileSystem.find(path).mtime = mtime
else else
raise Errno::ENOENT raise Errno::ENOENT
@ -132,12 +146,13 @@ module FakeFS
def self.readlink(path) def self.readlink(path)
symlink = FileSystem.find(path) symlink = FileSystem.find(path)
FileSystem.find(symlink.target).to_s symlink.target
end end
def self.read(path) def self.read(path)
file = new(path) file = new(path)
if file.exists? if file.exists?
FileSystem.find(path).atime = Time.now
file.read file.read
else else
raise Errno::ENOENT raise Errno::ENOENT
@ -220,7 +235,7 @@ module FakeFS
end end
class Stat class Stat
attr_reader :ctime, :mtime attr_reader :ctime, :mtime, :atime
def initialize(file, __lstat = false) def initialize(file, __lstat = false)
if !File.exists?(file) if !File.exists?(file)
@ -232,6 +247,7 @@ module FakeFS
@__lstat = __lstat @__lstat = __lstat
@ctime = @fake_file.ctime @ctime = @fake_file.ctime
@mtime = @fake_file.mtime @mtime = @fake_file.mtime
@atime = @fake_file.atime
end end
def symlink? def symlink?
@ -321,7 +337,7 @@ module FakeFS
end end
def atime def atime
raise NotImplementedError self.class.atime(@path)
end end
def chmod(mode_int) def chmod(mode_int)
@ -337,7 +353,7 @@ module FakeFS
end end
def flock(locking_constant) def flock(locking_constant)
raise NotImplementedError #raise NotImplementedError
end end
def mtime def mtime
@ -358,7 +374,7 @@ module FakeFS
end end
def to_path def to_path
raise NotImplementedError @path
end end
end end
@ -398,8 +414,22 @@ module FakeFS
(@mode & mask) != 0 if @mode.is_a?(Integer) (@mode & mask) != 0 if @mode.is_a?(Integer)
end end
# Create a missing file if the path is valid.
#
def create_missing_file 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) @file = FileSystem.add(path, FakeFile.new)
end end
end end

View File

@ -7,7 +7,7 @@ module FakeFS
end end
def fs def fs
@fs ||= FakeDir.new('.') @fs ||= FakeDir.new('/')
end end
def clear def clear
@ -68,6 +68,7 @@ module FakeFS
def delete(path) def delete(path)
if node = FileSystem.find(path) if node = FileSystem.find(path)
node.delete node.delete
true
end end
end end
@ -121,7 +122,8 @@ module FakeFS
directories_under(dir) directories_under(dir)
end end
else else
dir.reject {|k,v| /\A#{pattern.gsub('?','.').gsub('*', '.*')}\Z/ !~ k }.values regexp_pattern = /\A#{pattern.gsub('?','.').gsub('*', '.*').gsub(/\{(.*?)\}/) { "(#{$1.gsub(',', '|')})" }}\Z/
dir.reject {|k,v| regexp_pattern !~ k }.values
end end
if parts.empty? # we're done recursing if parts.empty? # we're done recursing

View File

@ -3,5 +3,13 @@ module FakeFS
def self.exist?(file_name) def self.exist?(file_name)
File.exist?(file_name) File.exist?(file_name)
end end
def self.directory?(file_name)
File.directory?(file_name)
end
def self.file?(file_name)
File.file?(file_name)
end
end end
end end

View File

@ -6,6 +6,7 @@ module FakeFS
FileSystem.add(path, FakeDir.new) FileSystem.add(path, FakeDir.new)
end end
alias_method :mkpath, :mkdir_p alias_method :mkpath, :mkdir_p
alias_method :makedirs, :mkdir_p
def mkdir(path) def mkdir(path)
parent = path.split('/') parent = path.split('/')
@ -29,7 +30,7 @@ module FakeFS
def rm(list, options = {}) def rm(list, options = {})
Array(list).each do |path| Array(list).each do |path|
FileSystem.delete(path) FileSystem.delete(path) or raise Errno::ENOENT.new(path)
end end
end end
@ -39,9 +40,17 @@ module FakeFS
def ln_s(target, path, options = {}) def ln_s(target, path, options = {})
options = { :force => false }.merge(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)) FileSystem.add(path, FakeSymlink.new(target))
end end
def ln_sf(target, path) def ln_sf(target, path)
ln_s(target, path, { :force => true }) ln_s(target, path, { :force => true })
end end
@ -101,7 +110,7 @@ module FakeFS
FileSystem.add(dest_path, target.entry.clone) FileSystem.add(dest_path, target.entry.clone)
FileSystem.delete(path) FileSystem.delete(path)
else else
raise Errno::ENOENT, src raise Errno::ENOENT, path
end end
end end
end end

View File

@ -1,6 +1,6 @@
module FakeFS module FakeFS
module Version module Version
VERSION = "0.3.1" VERSION = "0.4.0"
def self.to_s def self.to_s
VERSION VERSION

6
test.watchr Normal file
View File

@ -0,0 +1,6 @@
watch('test/.*_test.rb') { run_tests }
watch('lib/.*.rb') { run_tests }
def run_tests
system 'rake test'
end

View File

@ -63,6 +63,22 @@ class FakeFSTest < Test::Unit::TestCase
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir'] assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end 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_unlink_errors_on_file_not_found
assert_raise Errno::ENOENT do
FileUtils.rm("/foo")
end
end
def test_can_delete_directories def test_can_delete_directories
FileUtils.mkdir_p("/path/to/dir") FileUtils.mkdir_p("/path/to/dir")
FileUtils.rmdir("/path/to/dir") FileUtils.rmdir("/path/to/dir")
@ -109,6 +125,11 @@ class FakeFSTest < Test::Unit::TestCase
assert File.exists?(path) assert File.exists?(path)
end end
def test_symlink_with_missing_refferent_does_not_exist
File.symlink('/foo', '/bar')
assert !File.exists?('/bar')
end
def test_can_create_symlinks def test_can_create_symlinks
FileUtils.mkdir_p(target = "/path/to/target") FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link") FileUtils.ln_s(target, "/path/to/link")
@ -139,14 +160,63 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal target, File.readlink(link) assert_equal target, File.readlink(link)
end 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_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")
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 def test_knows_symlinks_are_symlinks
FileUtils.mkdir_p(target = "/path/to/target") FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, link = "/path/to/symlink") FileUtils.ln_s(target, link = "/path/to/symlink")
assert File.symlink?(link) assert File.symlink?(link)
end end
def test_can_create_files def test_can_create_files_in_current_dir
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -156,7 +226,74 @@ class FakeFSTest < Test::Unit::TestCase
assert File.writable?(path) assert File.writable?(path)
end 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_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"
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 def test_can_create_files_with_bitmasks
FileUtils.mkdir_p("/path/to")
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, File::RDWR | File::CREAT) do |f| File.open(path, File::RDWR | File::CREAT) do |f|
f.write "Yatta!" f.write "Yatta!"
@ -275,7 +412,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_read_files_once_written def test_can_read_files_once_written
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -284,7 +421,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_write_to_files def test_can_write_to_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
@ -298,11 +435,12 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_open_file_in_binary_mode def test_can_open_file_in_binary_mode
File.open("/foo", "wb") { |x| x << "a" } File.open("foo", "wb") { |x| x << "a" }
assert_equal "a", File.read("/foo") assert_equal "a", File.read("foo")
end end
def test_can_chunk_io_when_reading def test_can_chunk_io_when_reading
FileUtils.mkdir_p "/path/to"
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
@ -316,7 +454,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_get_size_of_files def test_can_get_size_of_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
@ -324,20 +462,20 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_check_if_file_has_size? def test_can_check_if_file_has_size?
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << 'Yada Yada' f << 'Yada Yada'
end end
assert File.size?(path) assert File.size?(path)
assert_nil File.size?("/path/to/other.txt") assert_nil File.size?("other.txt")
end end
def test_can_check_size_of_empty_file def test_can_check_size_of_empty_file
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
assert_nil File.size?("/path/to/file.txt") assert_nil File.size?("file.txt")
end end
def test_raises_error_on_mtime_if_file_does_not_exist def test_raises_error_on_mtime_if_file_does_not_exist
@ -347,16 +485,16 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_return_mtime_on_existing_file def test_can_return_mtime_on_existing_file
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
assert File.mtime('/path/to/file.txt').is_a?(Time) assert File.mtime('file.txt').is_a?(Time)
end end
def test_raises_error_on_ctime_if_file_does_not_exist def test_raises_error_on_ctime_if_file_does_not_exist
assert_raise Errno::ENOENT do assert_raise Errno::ENOENT do
File.ctime('/path/to/file.txt') File.ctime('file.txt')
end end
end end
@ -365,27 +503,45 @@ class FakeFSTest < Test::Unit::TestCase
assert File.ctime('foo').is_a?(Time) assert File.ctime('foo').is_a?(Time)
end 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" } File.open("foo", "w") { |f| f << "some content" }
ctime = File.ctime("foo") ctime = File.ctime("foo")
mtime = File.mtime("foo") mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time) assert ctime.is_a?(Time)
assert mtime.is_a?(Time) assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime assert_equal ctime, mtime
assert_equal ctime, atime
File.open("foo", "r") do |f| File.open("foo", "r") do |f|
assert_equal ctime, f.ctime assert_equal ctime, f.ctime
assert_equal mtime, f.mtime assert_equal mtime, f.mtime
assert_equal atime, f.atime
end end
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") FileUtils.mkdir_p("foo")
ctime = File.ctime("foo") ctime = File.ctime("foo")
mtime = File.mtime("foo") mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time) assert ctime.is_a?(Time)
assert mtime.is_a?(Time) assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime assert_equal ctime, mtime
assert_equal ctime, atime
end end
def test_file_ctime_is_equal_to_file_stat_ctime def test_file_ctime_is_equal_to_file_stat_ctime
@ -408,6 +564,16 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal File.stat("foo").mtime, File.mtime("foo") assert_equal File.stat("foo").mtime, File.mtime("foo")
end 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 def test_utime_raises_error_if_path_does_not_exist
assert_raise Errno::ENOENT do assert_raise Errno::ENOENT do
File.utime(Time.now, Time.now, '/path/to/file.txt') File.utime(Time.now, Time.now, '/path/to/file.txt')
@ -416,16 +582,17 @@ class FakeFSTest < Test::Unit::TestCase
def test_can_call_utime_on_an_existing_file def test_can_call_utime_on_an_existing_file
time = Time.now - 300 # Not now time = Time.now - 300 # Not now
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
end end
File.utime(time, time, path) File.utime(time, time, path)
assert_equal time, File.mtime('/path/to/file.txt') assert_equal time, File.mtime('file.txt')
assert_equal time, File.atime('file.txt')
end end
def test_utime_returns_number_of_paths 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| [path1, path2].each do |path|
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f << '' f << ''
@ -434,8 +601,23 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 2, File.utime(Time.now, Time.now, path1, path2) assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
end 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 def test_can_read_with_File_readlines
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.puts "Yatta!", "Gatta!" f.puts "Yatta!", "Gatta!"
f.puts ["woot","toot"] f.puts ["woot","toot"]
@ -445,7 +627,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_close_disallows_further_access def test_File_close_disallows_further_access
path = '/path/to/file.txt' path = 'file.txt'
file = File.open(path, 'w') file = File.open(path, 'w')
file.write 'Yada' file.write 'Yada'
file.close file.close
@ -455,7 +637,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_close_disallows_further_writes def test_File_close_disallows_further_writes
path = '/path/to/file.txt' path = 'file.txt'
file = File.open(path, 'w') file = File.open(path, 'w')
file.write 'Yada' file.write 'Yada'
file.close file.close
@ -465,7 +647,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_can_read_from_file_objects def test_can_read_from_file_objects
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -480,7 +662,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_knows_files_are_files def test_knows_files_are_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -489,17 +671,17 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_File_io_returns_self def test_File_io_returns_self
f = File.open("/foo", "w") f = File.open("foo", "w")
assert_equal f, f.to_io assert_equal f, f.to_io
end end
def test_File_to_i_is_alias_for_filno 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) assert_equal f.method(:to_i), f.method(:fileno)
end end
def test_knows_symlink_files_are_files def test_knows_symlink_files_are_files
path = '/path/to/file.txt' path = 'file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
f.write "Yatta!" f.write "Yatta!"
end end
@ -566,22 +748,29 @@ 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', '/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', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*'] 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' FileUtils.cp_r '/path', '/otherpath'
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*'] 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 end
def test_dir_glob_handles_root def test_dir_glob_handles_root
FileUtils.mkdir_p '/path' FileUtils.mkdir_p '/path'
# this fails. the root dir should be named '/' but it is '.' # this fails. the root dir should be named '/' but it is '.'
#assert_equal ['/'], Dir['/'] assert_equal ['/'], Dir['/']
end end
def test_dir_glob_handles_recursive_globs def test_dir_glob_handles_recursive_globs
FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w') File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w') File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb'] assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
@ -590,6 +779,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs 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/two/three/four.rb', 'w')
File.open('/one/five.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/**/*'] assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
@ -607,7 +797,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_report_pos_as_0_when_opening def test_should_report_pos_as_0_when_opening
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foobar" f << "foobar"
f.rewind f.rewind
@ -616,7 +806,7 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_report_pos_as_1_when_seeking_one_char 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 << "foobar"
f.rewind f.rewind
@ -627,22 +817,22 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_should_set_pos def test_should_set_pos
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foo" f << "foo"
end end
fp = File.open("/foo", "r") fp = File.open("foo", "r")
fp.pos = 1 fp.pos = 1
assert_equal 1, fp.pos assert_equal 1, fp.pos
end end
def test_should_set_pos_with_tell_method def test_should_set_pos_with_tell_method
File.open("/foo", "w") do |f| File.open("foo", "w") do |f|
f << "foo" f << "foo"
end end
fp = File.open("/foo", "r") fp = File.open("foo", "r")
fp.tell = 1 fp.tell = 1
assert_equal 1, fp.pos assert_equal 1, fp.pos
@ -672,14 +862,14 @@ class FakeFSTest < Test::Unit::TestCase
def test_chdir_changes_directories_like_a_boss def test_chdir_changes_directories_like_a_boss
# I know memes! # I know memes!
FileUtils.mkdir_p '/path' FileUtils.mkdir_p '/path'
assert_equal '.', FileSystem.fs.name assert_equal '/', FileSystem.fs.name
assert_equal({}, FileSystem.fs['path']) assert_equal({}, FileSystem.fs['path'])
Dir.chdir '/path' do Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'} File.open('foo', 'w') { |f| f.write 'foo'}
File.open('foobar', 'w') { |f| f.write 'foo'} File.open('foobar', 'w') { |f| f.write 'foo'}
end end
assert_equal '.', FileSystem.fs.name assert_equal '/', FileSystem.fs.name
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort) assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
c = nil c = nil
@ -801,6 +991,10 @@ class FakeFSTest < Test::Unit::TestCase
assert_raise(Errno::ENOENT) do assert_raise(Errno::ENOENT) do
FileUtils.mv 'blafgag', 'foo' FileUtils.mv 'blafgag', 'foo'
end end
exception = assert_raise(Errno::ENOENT) do
FileUtils.mv ['foo', 'bar'], 'destdir'
end
assert_equal "No such file or directory - foo", exception.message
end end
def test_mv_actually_works def test_mv_actually_works
@ -1206,11 +1400,17 @@ class FakeFSTest < Test::Unit::TestCase
FileUtils.touch("/this/path/should/be/here/#{f}") FileUtils.touch("/this/path/should/be/here/#{f}")
end end
assert_raises(SystemCallError) do assert_raises(Errno::ENOTEMPTY) do
Dir.delete('/this/path/should/be/here') Dir.delete('/this/path/should/be/here')
end end
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 def test_directory_entries
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ] test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
@ -1239,6 +1439,12 @@ class FakeFSTest < Test::Unit::TestCase
test.each { |t| assert yielded.include?(t) } test.each { |t| assert yielded.include?(t) }
end 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 def test_directory_foreach
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ] test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
@ -1463,14 +1669,6 @@ class FakeFSTest < Test::Unit::TestCase
end end
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 def test_unlink_is_alias_for_delete
assert_equal File.method(:unlink), File.method(:delete) assert_equal File.method(:unlink), File.method(:delete)
end end
@ -1571,6 +1769,28 @@ class FakeFSTest < Test::Unit::TestCase
assert !FileTest.exist?("/path/to/dir") assert !FileTest.exist?("/path/to/dir")
end 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_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)
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 def test_pathname_exists_returns_correct_value
FileUtils.touch "foo" FileUtils.touch "foo"
assert Pathname.new("foo").exist? assert Pathname.new("foo").exist?
@ -1578,6 +1798,20 @@ class FakeFSTest < Test::Unit::TestCase
assert !Pathname.new("bar").exist? assert !Pathname.new("bar").exist?
end 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 def test_activating_returns_true
FakeFS.deactivate! FakeFS.deactivate!
assert_equal true, FakeFS.activate! assert_equal true, FakeFS.activate!
@ -1587,6 +1821,15 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal true, FakeFS.deactivate! assert_equal true, FakeFS.deactivate!
end 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) def here(fname)
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname)) RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
end end
@ -1615,5 +1858,11 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal false, f.autoclose? assert_equal false, f.autoclose?
end end
end end
def test_to_path
File.new("foo", 'w') do |f|
assert_equal "foo", f.to_path
end
end
end end
end end

View File

@ -10,7 +10,7 @@ class FakeFSSafeTest < Test::Unit::TestCase
end end
def test_FakeFS_method_does_not_intrude_on_global_namespace def test_FakeFS_method_does_not_intrude_on_global_namespace
path = '/path/to/file.txt' path = 'file.txt'
FakeFS do FakeFS do
File.open(path, 'w') { |f| f.write "Yatta!" } File.open(path, 'w') { |f| f.write "Yatta!" }

View File

@ -24,7 +24,7 @@ class FakeFSVerifierTest < Test::Unit::TestCase
real_class.instance_methods.each do |method| real_class.instance_methods.each do |method|
define_method("test #{method} instance method") do 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 end
end end