ktheory-fakefs 0.2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +5 -0
- data/.gitignore +1 -0
- data/CONTRIBUTORS +17 -0
- data/LICENSE +20 -0
- data/README.markdown +123 -0
- data/Rakefile +60 -0
- data/lib/fakefs.rb +3 -0
- data/lib/fakefs/base.rb +45 -0
- data/lib/fakefs/dir.rb +115 -0
- data/lib/fakefs/fake/dir.rb +45 -0
- data/lib/fakefs/fake/file.rb +79 -0
- data/lib/fakefs/fake/symlink.rb +32 -0
- data/lib/fakefs/file.rb +376 -0
- data/lib/fakefs/file_system.rb +136 -0
- data/lib/fakefs/file_test.rb +7 -0
- data/lib/fakefs/fileutils.rb +131 -0
- data/lib/fakefs/safe.rb +11 -0
- data/lib/fakefs/spec_helpers.rb +46 -0
- data/lib/fakefs/version.rb +9 -0
- data/spec/fakefs/spec_helpers_spec.rb +57 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +3 -0
- data/test/fake/file/join_test.rb +19 -0
- data/test/fake/file/lstat_test.rb +59 -0
- data/test/fake/file/stat_test.rb +39 -0
- data/test/fake/file/sysseek_test.rb +44 -0
- data/test/fake/file/syswrite_test.rb +62 -0
- data/test/fake/file_test.rb +86 -0
- data/test/fake/symlink_test.rb +10 -0
- data/test/fakefs_test.rb +1466 -0
- data/test/file/stat_test.rb +73 -0
- data/test/safe_test.rb +44 -0
- data/test/test_helper.rb +8 -0
- data/test/verify.rb +31 -0
- metadata +103 -0
data/.autotest
ADDED
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.sw?
|
data/CONTRIBUTORS
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
* Chris Wanstrath
|
2
|
+
* Scott Taylor
|
3
|
+
* Jeff Hodges
|
4
|
+
* Pat Nakajima
|
5
|
+
* Myles Eftos
|
6
|
+
* Matt Freels
|
7
|
+
* Nick Quaranto
|
8
|
+
* Tymon Tobolski
|
9
|
+
* Ben Mabey
|
10
|
+
* Jon Yurek
|
11
|
+
* Scott Barron
|
12
|
+
* dmathieu
|
13
|
+
* Rob Sanheim
|
14
|
+
* David Reese
|
15
|
+
* msassak
|
16
|
+
* Sam Goldstein
|
17
|
+
* jameswilding
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Chris Wanstrath
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
FakeFS
|
2
|
+
======
|
3
|
+
|
4
|
+
Mocha is great. But when your library is all about manipulating the
|
5
|
+
filesystem, you really want to test the behavior and not the implementation.
|
6
|
+
|
7
|
+
If you're mocking and stubbing every call to FileUtils or File, you're
|
8
|
+
tightly coupling your tests with the implementation.
|
9
|
+
|
10
|
+
def test_creates_directory
|
11
|
+
FileUtils.expects(:mkdir).with("directory").once
|
12
|
+
Library.add "directory"
|
13
|
+
end
|
14
|
+
|
15
|
+
The above test will break if we decide to use `mkdir_p` in our code. Refactoring
|
16
|
+
code shouldn't necessitate refactoring tests.
|
17
|
+
|
18
|
+
With FakeFS:
|
19
|
+
|
20
|
+
def test_creates_directory
|
21
|
+
Library.add "directory"
|
22
|
+
assert File.directory?("directory")
|
23
|
+
end
|
24
|
+
|
25
|
+
Woot.
|
26
|
+
|
27
|
+
|
28
|
+
Usage
|
29
|
+
-----
|
30
|
+
|
31
|
+
require 'fakefs'
|
32
|
+
|
33
|
+
# That's it.
|
34
|
+
|
35
|
+
|
36
|
+
Don't Fake the FS Immediately
|
37
|
+
-----------------------------
|
38
|
+
|
39
|
+
require 'fakefs/safe'
|
40
|
+
|
41
|
+
FakeFS.activate!
|
42
|
+
# your code
|
43
|
+
FakeFS.deactivate!
|
44
|
+
|
45
|
+
# or
|
46
|
+
FakeFS do
|
47
|
+
# your code
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
RSpec
|
52
|
+
-----
|
53
|
+
|
54
|
+
The above approach works with RSpec as well. In addition you may include
|
55
|
+
FakeFS::SpecHelpers to turn FakeFS on and off in a given example group:
|
56
|
+
|
57
|
+
require 'fakefs/spec_helpers'
|
58
|
+
|
59
|
+
describe "my spec" do
|
60
|
+
include FakeFS::SpecHelpers
|
61
|
+
end
|
62
|
+
|
63
|
+
See `lib/fakefs/spec_helpers.rb` for more info.
|
64
|
+
|
65
|
+
|
66
|
+
How is this different than MockFS?
|
67
|
+
----------------------------------
|
68
|
+
|
69
|
+
FakeFS provides a test suite and works with symlinks. It's also strictly a
|
70
|
+
test-time dependency: your actual library does not need to use or know about
|
71
|
+
FakeFS.
|
72
|
+
|
73
|
+
|
74
|
+
Caveats
|
75
|
+
-------
|
76
|
+
|
77
|
+
FakeFS internally uses the `Pathname` and `FileUtils` constants. If you use
|
78
|
+
these in your app, be certain you're properly requiring them and not counting
|
79
|
+
on FakeFS' own require.
|
80
|
+
|
81
|
+
|
82
|
+
Speed?
|
83
|
+
------
|
84
|
+
|
85
|
+
<http://gist.github.com/156091>
|
86
|
+
|
87
|
+
|
88
|
+
Installation
|
89
|
+
------------
|
90
|
+
|
91
|
+
### [Gemcutter](http://gemcutter.org/)
|
92
|
+
|
93
|
+
$ gem install fakefs
|
94
|
+
|
95
|
+
### [Rip](http://hellorip.com)
|
96
|
+
|
97
|
+
$ rip install git://github.com/defunkt/fakefs.git
|
98
|
+
|
99
|
+
|
100
|
+
Contributing
|
101
|
+
------------
|
102
|
+
|
103
|
+
Once you've made your great commits:
|
104
|
+
|
105
|
+
1. [Fork][0] FakeFS
|
106
|
+
2. Create a topic branch - `git checkout -b my_branch`
|
107
|
+
3. Push to your branch - `git push origin my_branch`
|
108
|
+
4. Create an [Issue][1] with a link to your branch
|
109
|
+
5. That's it!
|
110
|
+
|
111
|
+
Meta
|
112
|
+
----
|
113
|
+
|
114
|
+
* Code: `git clone git://github.com/defunkt/fakefs.git`
|
115
|
+
* Home: <http://github.com/defunkt/fakefs>
|
116
|
+
* Docs: <http://defunkt.github.com/fakefs>
|
117
|
+
* Bugs: <http://github.com/defunkt/fakefs/issues>
|
118
|
+
* List: <http://groups.google.com/group/fakefs>
|
119
|
+
* Test: <http://runcoderun.com/defunkt/fakefs>
|
120
|
+
* Gems: <http://gemcutter.org/gems/fakefs>
|
121
|
+
|
122
|
+
[0]: http://help.github.com/forking/
|
123
|
+
[1]: http://github.com/defunkt/fakefs/issues
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'test')
|
2
|
+
|
3
|
+
desc "Run tests"
|
4
|
+
task :test do
|
5
|
+
Dir['test/**/*_test.rb'].each { |file| require file }
|
6
|
+
end
|
7
|
+
|
8
|
+
task :default => [:test, :spec]
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'spec/rake/spectask'
|
12
|
+
|
13
|
+
desc "Run specs"
|
14
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
15
|
+
t.spec_files = FileList["spec/**/*.rb"]
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Spec task can't be loaded. `gem install rspec`"
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
require 'jeweler'
|
23
|
+
|
24
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
|
25
|
+
require 'fakefs/version'
|
26
|
+
|
27
|
+
Jeweler::Tasks.new do |gemspec|
|
28
|
+
gemspec.name = "fakefs"
|
29
|
+
gemspec.summary = "A fake filesystem. Use it in your tests."
|
30
|
+
gemspec.email = "chris@ozmm.org"
|
31
|
+
gemspec.homepage = "http://github.com/defunkt/fakefs"
|
32
|
+
gemspec.description = "A fake filesystem. Use it in your tests."
|
33
|
+
gemspec.authors = ["Chris Wanstrath"]
|
34
|
+
gemspec.has_rdoc = false
|
35
|
+
gemspec.version = FakeFS::Version.to_s
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
puts "Jeweler not available."
|
39
|
+
puts "Install it with: gem install jeweler"
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
require 'sdoc_helpers'
|
44
|
+
rescue LoadError
|
45
|
+
puts "sdoc support not enabled. Please gem install sdoc-helpers."
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Build a gem"
|
49
|
+
task :gem => [ :gemspec, :build ]
|
50
|
+
|
51
|
+
desc "Push a new version to Gemcutter"
|
52
|
+
task :publish => [ :gemspec, :build ] do
|
53
|
+
abort("Tests failed!") unless system("rake test")
|
54
|
+
system "git tag v#{FakeFS::Version}"
|
55
|
+
system "git push origin v#{FakeFS::Version}"
|
56
|
+
system "git push origin master"
|
57
|
+
system "gem push pkg/fakefs-#{FakeFS::Version}.gem"
|
58
|
+
system "git clean -fd"
|
59
|
+
exec "rake pages"
|
60
|
+
end
|
data/lib/fakefs.rb
ADDED
data/lib/fakefs/base.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
RealFile = File
|
2
|
+
RealFileTest = FileTest
|
3
|
+
RealFileUtils = FileUtils
|
4
|
+
RealDir = Dir
|
5
|
+
|
6
|
+
module FakeFS
|
7
|
+
def self.activate!
|
8
|
+
Object.class_eval do
|
9
|
+
remove_const(:Dir)
|
10
|
+
remove_const(:File)
|
11
|
+
remove_const(:FileTest)
|
12
|
+
remove_const(:FileUtils)
|
13
|
+
|
14
|
+
const_set(:Dir, FakeFS::Dir)
|
15
|
+
const_set(:File, FakeFS::File)
|
16
|
+
const_set(:FileUtils, FakeFS::FileUtils)
|
17
|
+
const_set(:FileTest, FakeFS::FileTest)
|
18
|
+
end
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.deactivate!
|
23
|
+
Object.class_eval do
|
24
|
+
remove_const(:Dir)
|
25
|
+
remove_const(:File)
|
26
|
+
remove_const(:FileTest)
|
27
|
+
remove_const(:FileUtils)
|
28
|
+
|
29
|
+
const_set(:Dir, RealDir)
|
30
|
+
const_set(:File, RealFile)
|
31
|
+
const_set(:FileTest, RealFileTest)
|
32
|
+
const_set(:FileUtils, RealFileUtils)
|
33
|
+
end
|
34
|
+
true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def FakeFS
|
39
|
+
return ::FakeFS unless block_given?
|
40
|
+
::FakeFS.activate!
|
41
|
+
yield
|
42
|
+
ensure
|
43
|
+
::FakeFS.deactivate!
|
44
|
+
end
|
45
|
+
|
data/lib/fakefs/dir.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
module FakeFS
|
2
|
+
class Dir
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(string)
|
6
|
+
raise Errno::ENOENT, string unless FileSystem.find(string)
|
7
|
+
@path = string
|
8
|
+
@open = true
|
9
|
+
@pointer = 0
|
10
|
+
@contents = [ '.', '..', ] + FileSystem.find(@path).values
|
11
|
+
end
|
12
|
+
|
13
|
+
def close
|
14
|
+
@open = false
|
15
|
+
@pointer = nil
|
16
|
+
@contents = nil
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def each(&block)
|
21
|
+
while f = read
|
22
|
+
yield f
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def path
|
27
|
+
@path
|
28
|
+
end
|
29
|
+
|
30
|
+
def pos
|
31
|
+
@pointer
|
32
|
+
end
|
33
|
+
|
34
|
+
def pos=(integer)
|
35
|
+
@pointer = integer
|
36
|
+
end
|
37
|
+
|
38
|
+
def read
|
39
|
+
raise IOError, "closed directory" if @pointer == nil
|
40
|
+
n = @contents[@pointer]
|
41
|
+
@pointer += 1
|
42
|
+
n.to_s.gsub(path + '/', '') if n
|
43
|
+
end
|
44
|
+
|
45
|
+
def rewind
|
46
|
+
@pointer = 0
|
47
|
+
end
|
48
|
+
|
49
|
+
def seek(integer)
|
50
|
+
raise IOError, "closed directory" if @pointer == nil
|
51
|
+
@pointer = integer
|
52
|
+
@contents[integer]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.[](pattern)
|
56
|
+
glob(pattern)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.chdir(dir, &blk)
|
60
|
+
FileSystem.chdir(dir, &blk)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.chroot(string)
|
64
|
+
# Not implemented yet
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.delete(string)
|
68
|
+
raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty?
|
69
|
+
FileSystem.delete(string)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.entries(dirname)
|
73
|
+
raise SystemCallError, dirname unless FileSystem.find(dirname)
|
74
|
+
Dir.new(dirname).map { |file| File.basename(file) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.foreach(dirname, &block)
|
78
|
+
Dir.open(dirname) { |file| yield file }
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.glob(pattern)
|
82
|
+
[FileSystem.find(pattern) || []].flatten.map{|e| e.to_s}.sort
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.mkdir(string, integer = 0)
|
86
|
+
parent = string.split('/')
|
87
|
+
parent.pop
|
88
|
+
raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || FileSystem.find(parent.join('/'))
|
89
|
+
raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string)
|
90
|
+
FileUtils.mkdir_p(string)
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.open(string, &block)
|
94
|
+
if block_given?
|
95
|
+
Dir.new(string).each { |file| yield(file) }
|
96
|
+
else
|
97
|
+
Dir.new(string)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.tmpdir
|
102
|
+
'/tmp'
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.pwd
|
106
|
+
FileSystem.current_dir.to_s
|
107
|
+
end
|
108
|
+
|
109
|
+
class << self
|
110
|
+
alias_method :getwd, :pwd
|
111
|
+
alias_method :rmdir, :delete
|
112
|
+
alias_method :unlink, :delete
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FakeFS
|
2
|
+
class FakeDir < Hash
|
3
|
+
attr_accessor :name, :parent
|
4
|
+
|
5
|
+
def initialize(name = nil, parent = nil)
|
6
|
+
@name = name
|
7
|
+
@parent = parent
|
8
|
+
end
|
9
|
+
|
10
|
+
def entry
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"(FakeDir name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{size})"
|
16
|
+
end
|
17
|
+
|
18
|
+
def clone(parent = nil)
|
19
|
+
clone = Marshal.load(Marshal.dump(self))
|
20
|
+
clone.each do |key, value|
|
21
|
+
value.parent = clone
|
22
|
+
end
|
23
|
+
clone.parent = parent if parent
|
24
|
+
clone
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
if parent && parent.to_s != '.'
|
29
|
+
File.join(parent.to_s, name)
|
30
|
+
elsif parent && parent.to_s == '.'
|
31
|
+
"#{File::PATH_SEPARATOR}#{name}"
|
32
|
+
else
|
33
|
+
name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(node = self)
|
38
|
+
if node == self
|
39
|
+
parent.delete(self)
|
40
|
+
else
|
41
|
+
super(node.name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|