fakefs 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +9 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +31 -0
- data/README.markdown +65 -29
- data/Rakefile +16 -13
- data/fakefs.gemspec +11 -11
- data/lib/fakefs/base.rb +69 -32
- data/lib/fakefs/dir.rb +22 -15
- data/lib/fakefs/fake/dir.rb +36 -10
- data/lib/fakefs/fake/file.rb +7 -2
- data/lib/fakefs/file.rb +90 -17
- data/lib/fakefs/file_system.rb +18 -10
- data/lib/fakefs/fileutils.rb +84 -41
- data/lib/fakefs/pathname.rb +864 -0
- data/lib/fakefs/safe.rb +2 -0
- data/lib/fakefs/version.rb +1 -1
- data/test/fakefs_test.rb +355 -49
- data/test/file/stat_test.rb +36 -0
- data/test/safe_test.rb +41 -3
- data/test/test_helper.rb +8 -1
- metadata +61 -60
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.8.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rdoc
|
11
|
+
json (1.7.5)
|
12
|
+
rake (10.0.0)
|
13
|
+
rdiscount (1.6.8)
|
14
|
+
rdoc (3.12)
|
15
|
+
json (~> 1.4)
|
16
|
+
rspec (2.12.0)
|
17
|
+
rspec-core (~> 2.12.0)
|
18
|
+
rspec-expectations (~> 2.12.0)
|
19
|
+
rspec-mocks (~> 2.12.0)
|
20
|
+
rspec-core (2.12.0)
|
21
|
+
rspec-expectations (2.12.0)
|
22
|
+
diff-lcs (~> 1.1.3)
|
23
|
+
rspec-mocks (2.12.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
jeweler
|
30
|
+
rdiscount
|
31
|
+
rspec
|
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
FakeFS
|
1
|
+
FakeFS [![build status](https://secure.travis-ci.org/defunkt/fakefs.png)](https://secure.travis-ci.org/defunkt/fakefs)
|
2
2
|
======
|
3
3
|
|
4
4
|
Mocha is great. But when your library is all about manipulating the
|
@@ -7,20 +7,24 @@ filesystem, you really want to test the behavior and not the implementation.
|
|
7
7
|
If you're mocking and stubbing every call to FileUtils or File, you're
|
8
8
|
tightly coupling your tests with the implementation.
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
``` ruby
|
11
|
+
def test_creates_directory
|
12
|
+
FileUtils.expects(:mkdir).with("directory").once
|
13
|
+
Library.add "directory"
|
14
|
+
end
|
15
|
+
```
|
14
16
|
|
15
17
|
The above test will break if we decide to use `mkdir_p` in our code. Refactoring
|
16
18
|
code shouldn't necessitate refactoring tests.
|
17
19
|
|
18
20
|
With FakeFS:
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
``` ruby
|
23
|
+
def test_creates_directory
|
24
|
+
Library.add "directory"
|
25
|
+
assert File.directory?("directory")
|
26
|
+
end
|
27
|
+
```
|
24
28
|
|
25
29
|
Woot.
|
26
30
|
|
@@ -28,24 +32,29 @@ Woot.
|
|
28
32
|
Usage
|
29
33
|
-----
|
30
34
|
|
31
|
-
|
32
|
-
|
33
|
-
# That's it.
|
35
|
+
``` ruby
|
36
|
+
require 'fakefs'
|
34
37
|
|
38
|
+
# That's it.
|
39
|
+
```
|
35
40
|
|
36
41
|
Don't Fake the FS Immediately
|
37
42
|
-----------------------------
|
38
43
|
|
39
|
-
|
44
|
+
``` ruby
|
45
|
+
gem "fakefs", :require => "fakefs/safe"
|
40
46
|
|
41
|
-
|
42
|
-
# your code
|
43
|
-
FakeFS.deactivate!
|
47
|
+
require 'fakefs/safe'
|
44
48
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
FakeFS.activate!
|
50
|
+
# your code
|
51
|
+
FakeFS.deactivate!
|
52
|
+
|
53
|
+
# or
|
54
|
+
FakeFS do
|
55
|
+
# your code
|
56
|
+
end
|
57
|
+
```
|
49
58
|
|
50
59
|
Rails
|
51
60
|
-----
|
@@ -61,15 +70,35 @@ RSpec
|
|
61
70
|
The above approach works with RSpec as well. In addition you may include
|
62
71
|
FakeFS::SpecHelpers to turn FakeFS on and off in a given example group:
|
63
72
|
|
64
|
-
|
73
|
+
``` ruby
|
74
|
+
require 'fakefs/spec_helpers'
|
65
75
|
|
66
|
-
|
67
|
-
|
68
|
-
|
76
|
+
describe "my spec" do
|
77
|
+
include FakeFS::SpecHelpers
|
78
|
+
end
|
79
|
+
```
|
69
80
|
|
70
81
|
See `lib/fakefs/spec_helpers.rb` for more info.
|
71
82
|
|
72
83
|
|
84
|
+
Integrating with other filesystem libraries
|
85
|
+
--------------------------------------------
|
86
|
+
Third-party libraries may add methods to filesystem-related classes. FakeFS
|
87
|
+
doesn't support these methods out of the box, but you can define fake versions
|
88
|
+
yourself on the equivalent FakeFS classes. For example,
|
89
|
+
[FileMagic](https://rubygems.org/gems/ruby-filemagic) adds `File#content_type`.
|
90
|
+
A fake version can be provided as follows:
|
91
|
+
|
92
|
+
``` ruby
|
93
|
+
module FakeFS
|
94
|
+
class File
|
95
|
+
def content_type
|
96
|
+
'fake/file'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
73
102
|
How is this different than MockFS?
|
74
103
|
----------------------------------
|
75
104
|
|
@@ -95,7 +124,7 @@ Speed?
|
|
95
124
|
Installation
|
96
125
|
------------
|
97
126
|
|
98
|
-
### [
|
127
|
+
### [RubyGems](http://rubygems.org/)
|
99
128
|
|
100
129
|
$ gem install fakefs
|
101
130
|
|
@@ -112,7 +141,7 @@ Once you've made your great commits:
|
|
112
141
|
1. [Fork][0] FakeFS
|
113
142
|
2. Create a topic branch - `git checkout -b my_branch`
|
114
143
|
3. Push to your branch - `git push origin my_branch`
|
115
|
-
|
144
|
+
5. Open a [Pull Request][1]
|
116
145
|
5. That's it!
|
117
146
|
|
118
147
|
Meta
|
@@ -123,8 +152,15 @@ Meta
|
|
123
152
|
* Docs: <http://defunkt.github.com/fakefs>
|
124
153
|
* Bugs: <http://github.com/defunkt/fakefs/issues>
|
125
154
|
* List: <http://groups.google.com/group/fakefs>
|
126
|
-
* Test: <http://
|
127
|
-
* Gems: <http://
|
155
|
+
* Test: <http://travisci.org/#!/defunkt/fakefs>
|
156
|
+
* Gems: <http://rubygems.org/gems/fakefs>
|
128
157
|
|
129
158
|
[0]: http://help.github.com/forking/
|
130
|
-
[1]: http://github.com/
|
159
|
+
[1]: http://help.github.com/send-pull-requests/
|
160
|
+
|
161
|
+
Releasing
|
162
|
+
---------
|
163
|
+
|
164
|
+
1. Update version in lib/fakefs/version.rb
|
165
|
+
2. Commit it
|
166
|
+
3. rake publish
|
data/Rakefile
CHANGED
@@ -1,16 +1,25 @@
|
|
1
1
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'test')
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
require "bundler/setup"
|
5
|
+
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs << "test"
|
10
|
+
t.test_files = FileList['test/**/*test.rb']
|
11
|
+
t.verbose = true
|
7
12
|
end
|
8
13
|
|
9
|
-
|
14
|
+
begin
|
15
|
+
require 'rspec/core/rake_task'
|
16
|
+
desc "Run specs"
|
17
|
+
RSpec::Core::RakeTask.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Spec task can't be loaded. `gem install rspec`"
|
20
|
+
end
|
10
21
|
|
11
|
-
|
12
|
-
desc "Run specs"
|
13
|
-
RSpec::Core::RakeTask.new
|
22
|
+
task :default => [:test, :spec]
|
14
23
|
|
15
24
|
begin
|
16
25
|
require 'jeweler'
|
@@ -33,12 +42,6 @@ rescue LoadError
|
|
33
42
|
puts "Install it with: gem install jeweler"
|
34
43
|
end
|
35
44
|
|
36
|
-
begin
|
37
|
-
require 'sdoc_helpers'
|
38
|
-
rescue LoadError
|
39
|
-
puts "sdoc support not enabled. Please gem install sdoc-helpers."
|
40
|
-
end
|
41
|
-
|
42
45
|
desc "Build a gem"
|
43
46
|
task :gem => [ :gemspec, :build ]
|
44
47
|
|
data/fakefs.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.4.
|
7
|
+
s.name = "fakefs"
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chris Wanstrath", "Scott Taylor", "Jeff Hodges", "Pat Nakajima"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2012-11-14"
|
13
|
+
s.description = "A fake filesystem. Use it in your tests."
|
14
|
+
s.email = "chris@ozmm.org"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.markdown"
|
@@ -19,8 +19,10 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".autotest",
|
21
21
|
".rspec",
|
22
|
+
".travis.yml",
|
22
23
|
"CONTRIBUTORS",
|
23
24
|
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
24
26
|
"LICENSE",
|
25
27
|
"README.markdown",
|
26
28
|
"Rakefile",
|
@@ -35,6 +37,7 @@ Gem::Specification.new do |s|
|
|
35
37
|
"lib/fakefs/file_system.rb",
|
36
38
|
"lib/fakefs/file_test.rb",
|
37
39
|
"lib/fakefs/fileutils.rb",
|
40
|
+
"lib/fakefs/pathname.rb",
|
38
41
|
"lib/fakefs/safe.rb",
|
39
42
|
"lib/fakefs/spec_helpers.rb",
|
40
43
|
"lib/fakefs/version.rb",
|
@@ -54,10 +57,10 @@ Gem::Specification.new do |s|
|
|
54
57
|
"test/test_helper.rb",
|
55
58
|
"test/verify.rb"
|
56
59
|
]
|
57
|
-
s.homepage =
|
60
|
+
s.homepage = "http://github.com/defunkt/fakefs"
|
58
61
|
s.require_paths = ["lib"]
|
59
|
-
s.rubygems_version =
|
60
|
-
s.summary =
|
62
|
+
s.rubygems_version = "1.8.24"
|
63
|
+
s.summary = "A fake filesystem. Use it in your tests."
|
61
64
|
|
62
65
|
if s.respond_to? :specification_version then
|
63
66
|
s.specification_version = 3
|
@@ -65,18 +68,15 @@ Gem::Specification.new do |s|
|
|
65
68
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
66
69
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
67
70
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
68
|
-
s.add_development_dependency(%q<sdoc-helpers>, [">= 0"])
|
69
71
|
s.add_development_dependency(%q<rdiscount>, [">= 0"])
|
70
72
|
else
|
71
73
|
s.add_dependency(%q<rspec>, [">= 0"])
|
72
74
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
73
|
-
s.add_dependency(%q<sdoc-helpers>, [">= 0"])
|
74
75
|
s.add_dependency(%q<rdiscount>, [">= 0"])
|
75
76
|
end
|
76
77
|
else
|
77
78
|
s.add_dependency(%q<rspec>, [">= 0"])
|
78
79
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
79
|
-
s.add_dependency(%q<sdoc-helpers>, [">= 0"])
|
80
80
|
s.add_dependency(%q<rdiscount>, [">= 0"])
|
81
81
|
end
|
82
82
|
end
|
data/lib/fakefs/base.rb
CHANGED
@@ -2,44 +2,81 @@ RealFile = File
|
|
2
2
|
RealFileTest = FileTest
|
3
3
|
RealFileUtils = FileUtils
|
4
4
|
RealDir = Dir
|
5
|
+
RealPathname = Pathname
|
5
6
|
|
6
7
|
module FakeFS
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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)
|
8
|
+
@activated = false
|
9
|
+
class << self
|
10
|
+
def activated?
|
11
|
+
@activated
|
18
12
|
end
|
19
|
-
true
|
20
|
-
end
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
14
|
+
def activate!
|
15
|
+
@activated = true
|
16
|
+
Object.class_eval do
|
17
|
+
remove_const(:Dir)
|
18
|
+
remove_const(:File)
|
19
|
+
remove_const(:FileTest)
|
20
|
+
remove_const(:FileUtils)
|
21
|
+
remove_const(:Pathname) if RUBY_VERSION >= "1.9.3"
|
22
|
+
|
23
|
+
const_set(:Dir, FakeFS::Dir)
|
24
|
+
const_set(:File, FakeFS::File)
|
25
|
+
const_set(:FileUtils, FakeFS::FileUtils)
|
26
|
+
const_set(:FileTest, FakeFS::FileTest)
|
27
|
+
const_set(:Pathname, FakeFS::Pathname) if RUBY_VERSION >= "1.9.3"
|
28
|
+
end
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def deactivate!
|
33
|
+
@activated = false
|
34
|
+
|
35
|
+
Object.class_eval do
|
36
|
+
remove_const(:Dir)
|
37
|
+
remove_const(:File)
|
38
|
+
remove_const(:FileTest)
|
39
|
+
remove_const(:FileUtils)
|
40
|
+
remove_const(:Pathname) if RUBY_VERSION >= "1.9.3"
|
41
|
+
|
42
|
+
const_set(:Dir, RealDir)
|
43
|
+
const_set(:File, RealFile)
|
44
|
+
const_set(:FileTest, RealFileTest)
|
45
|
+
const_set(:FileUtils, RealFileUtils)
|
46
|
+
const_set(:Pathname, RealPathname) if RUBY_VERSION >= "1.9.3"
|
47
|
+
end
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
def with
|
52
|
+
if activated?
|
53
|
+
yield
|
54
|
+
else
|
55
|
+
begin
|
56
|
+
activate!
|
57
|
+
yield
|
58
|
+
ensure
|
59
|
+
deactivate!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def without
|
65
|
+
if !activated?
|
66
|
+
yield
|
67
|
+
else
|
68
|
+
begin
|
69
|
+
deactivate!
|
70
|
+
yield
|
71
|
+
ensure
|
72
|
+
activate!
|
73
|
+
end
|
74
|
+
end
|
33
75
|
end
|
34
|
-
true
|
35
76
|
end
|
36
77
|
end
|
37
78
|
|
38
|
-
def FakeFS
|
39
|
-
return ::FakeFS unless
|
40
|
-
::FakeFS.
|
41
|
-
yield
|
42
|
-
ensure
|
43
|
-
::FakeFS.deactivate!
|
79
|
+
def FakeFS(&block)
|
80
|
+
return ::FakeFS unless block
|
81
|
+
::FakeFS.with(&block)
|
44
82
|
end
|
45
|
-
|
data/lib/fakefs/dir.rb
CHANGED
@@ -12,7 +12,7 @@ module FakeFS
|
|
12
12
|
@path = string
|
13
13
|
@open = true
|
14
14
|
@pointer = 0
|
15
|
-
@contents = [ '.', '..', ] + FileSystem.find(@path).
|
15
|
+
@contents = [ '.', '..', ] + FileSystem.find(@path).entries
|
16
16
|
end
|
17
17
|
|
18
18
|
def close
|
@@ -57,8 +57,8 @@ module FakeFS
|
|
57
57
|
@contents[integer]
|
58
58
|
end
|
59
59
|
|
60
|
-
def self.[](pattern)
|
61
|
-
glob
|
60
|
+
def self.[](*pattern)
|
61
|
+
glob pattern
|
62
62
|
end
|
63
63
|
|
64
64
|
def self.exists?(path)
|
@@ -75,7 +75,7 @@ module FakeFS
|
|
75
75
|
|
76
76
|
def self.delete(string)
|
77
77
|
_check_for_valid_file(string)
|
78
|
-
raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).
|
78
|
+
raise Errno::ENOTEMPTY, "Directory not empty - #{string}" unless FileSystem.find(string).empty?
|
79
79
|
|
80
80
|
FileSystem.delete(string)
|
81
81
|
end
|
@@ -91,20 +91,27 @@ module FakeFS
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def self.glob(pattern, &block)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
def self.mkdir(string, integer = 0)
|
99
|
-
parent = string.split('/')
|
100
|
-
parent.pop
|
94
|
+
matches_for_pattern = lambda do |matcher|
|
95
|
+
[FileSystem.find(matcher) || []].flatten.map{|e|
|
96
|
+
Dir.pwd.match(%r[\A/?\z]) || !e.to_s.match(%r[\A#{Dir.pwd}/?]) ? e.to_s : e.to_s.match(%r[\A#{Dir.pwd}/?]).post_match}.sort
|
97
|
+
end
|
101
98
|
|
102
|
-
|
99
|
+
if pattern.is_a? Array
|
100
|
+
files = pattern.collect { |matcher| matches_for_pattern.call matcher }.flatten
|
101
|
+
else
|
102
|
+
files = matches_for_pattern.call pattern
|
103
|
+
end
|
104
|
+
return block_given? ? files.each { |file| block.call(file) } : files
|
105
|
+
end
|
103
106
|
|
104
|
-
|
105
|
-
|
107
|
+
if RUBY_VERSION >= "1.9"
|
108
|
+
def self.home(user = nil)
|
109
|
+
RealDir.home(user)
|
110
|
+
end
|
111
|
+
end
|
106
112
|
|
107
|
-
|
113
|
+
def self.mkdir(string, integer = 0)
|
114
|
+
FileUtils.mkdir(string)
|
108
115
|
end
|
109
116
|
|
110
117
|
def self.open(string, &block)
|