fakefs 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/CONTRIBUTORS +12 -0
- data/README.markdown +40 -7
- data/Rakefile +27 -20
- data/lib/fakefs/base.rb +2 -1
- data/lib/fakefs/dir.rb +93 -2
- data/lib/fakefs/fake/dir.rb +8 -0
- data/lib/fakefs/fake/file.rb +53 -3
- data/lib/fakefs/fake/symlink.rb +8 -2
- data/lib/fakefs/file.rb +175 -17
- data/lib/fakefs/file_system.rb +25 -7
- data/lib/fakefs/fileutils.rb +23 -2
- data/lib/fakefs/spec_helpers.rb +46 -0
- data/lib/fakefs/version.rb +1 -1
- 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_test.rb +88 -0
- data/test/fake/symlink_test.rb +12 -0
- data/test/fakefs_test.rb +766 -53
- data/test/file/stat_test.rb +70 -0
- data/test/safe_test.rb +22 -0
- metadata +17 -3
data/lib/fakefs/fileutils.rb
CHANGED
|
@@ -5,17 +5,38 @@ module FakeFS
|
|
|
5
5
|
def mkdir_p(path)
|
|
6
6
|
FileSystem.add(path, FakeDir.new)
|
|
7
7
|
end
|
|
8
|
+
alias_method :mkpath, :mkdir_p
|
|
9
|
+
|
|
10
|
+
def rmdir(list, options = {})
|
|
11
|
+
list = [ list ] unless list.is_a?(Array)
|
|
12
|
+
list.each do |l|
|
|
13
|
+
parent = l.split('/')
|
|
14
|
+
parent.pop
|
|
15
|
+
raise Errno::ENOENT, "No such file or directory - #{l}" unless parent.join == "" || FileSystem.find(parent.join('/'))
|
|
16
|
+
raise Errno::ENOENT, l unless FileSystem.find(l)
|
|
17
|
+
raise Errno::ENOTEMPTY, l unless FileSystem.find(l).values.empty?
|
|
18
|
+
rm(l)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
8
21
|
|
|
9
22
|
def rm(path)
|
|
10
23
|
FileSystem.delete(path)
|
|
11
24
|
end
|
|
25
|
+
|
|
12
26
|
alias_method :rm_rf, :rm
|
|
13
27
|
alias_method :rm_r, :rm
|
|
28
|
+
alias_method :rm_f, :rm
|
|
14
29
|
|
|
15
|
-
def ln_s(target, path)
|
|
16
|
-
|
|
30
|
+
def ln_s(target, path, options = {})
|
|
31
|
+
options = { :force => false }.merge(options)
|
|
32
|
+
(FileSystem.find(path) and !options[:force]) ? raise(Errno::EEXIST, path) : FileSystem.delete(path)
|
|
17
33
|
FileSystem.add(path, FakeSymlink.new(target))
|
|
18
34
|
end
|
|
35
|
+
def ln_sf(target, path)
|
|
36
|
+
ln_s(target, path, { :force => true })
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
|
|
19
40
|
|
|
20
41
|
def cp(src, dest)
|
|
21
42
|
dst_file = FileSystem.find(dest)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# FakeFS::SpecHelpers provides a simple macro for RSpec example groups to turn FakeFS on and off.
|
|
2
|
+
# To use it simply require 'fakefs/spec_helpers', then include FakeFS::SpecHelpers into any
|
|
3
|
+
# example groups that you wish to use FakeFS in. For example:
|
|
4
|
+
#
|
|
5
|
+
# require 'fakefs/spec_helpers'
|
|
6
|
+
#
|
|
7
|
+
# describe "Some specs that deal with files" do
|
|
8
|
+
# include FakeFS::SpecHelpers
|
|
9
|
+
# ...
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# Alternatively, you can include FakeFS::SpecHelpers in all your example groups using RSpec's
|
|
13
|
+
# configuration block in your spec helper:
|
|
14
|
+
#
|
|
15
|
+
# require 'fakefs/spec_helpers'
|
|
16
|
+
#
|
|
17
|
+
# Spec::Runner.configure do |config|
|
|
18
|
+
# config.include FakeFS::SpecHelpers
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# If you do the above then use_fakefs will be available in all of your example groups.
|
|
22
|
+
#
|
|
23
|
+
require 'fakefs/safe'
|
|
24
|
+
|
|
25
|
+
module FakeFS
|
|
26
|
+
module SpecHelpers
|
|
27
|
+
def self.extended(example_group)
|
|
28
|
+
example_group.use_fakefs(example_group)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.included(example_group)
|
|
32
|
+
example_group.extend self
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def use_fakefs(describe_block)
|
|
36
|
+
describe_block.before :each do
|
|
37
|
+
FakeFS.activate!
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe_block.after :each do
|
|
41
|
+
FakeFS.deactivate!
|
|
42
|
+
FakeFS::FileSystem.clear
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/fakefs/version.rb
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module FakeFS
|
|
4
|
+
describe SpecHelpers do
|
|
5
|
+
before do
|
|
6
|
+
@rspec_example_group = Class.new do
|
|
7
|
+
def self.before(sym = :each)
|
|
8
|
+
yield if block_given?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.after(sym = :each)
|
|
12
|
+
yield if block_given?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "when extending" do
|
|
18
|
+
context "before each" do
|
|
19
|
+
it "should call it" do
|
|
20
|
+
@rspec_example_group.should_receive(:before).with(:each)
|
|
21
|
+
@rspec_example_group.extend FakeFS::SpecHelpers
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "should call FakeFS.activate!" do
|
|
25
|
+
FakeFS.should_receive(:activate!)
|
|
26
|
+
@rspec_example_group.extend FakeFS::SpecHelpers
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
context "after each" do
|
|
31
|
+
it "should call it" do
|
|
32
|
+
@rspec_example_group.should_receive(:after).with(:each)
|
|
33
|
+
@rspec_example_group.extend FakeFS::SpecHelpers
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "should deactivate fakefs" do
|
|
37
|
+
FakeFS.should_receive(:deactivate!)
|
|
38
|
+
@rspec_example_group.extend FakeFS::SpecHelpers
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should clear the fakefs filesystem for the next run" do
|
|
42
|
+
FakeFS::FileSystem.should_receive(:clear)
|
|
43
|
+
@rspec_example_group.extend FakeFS::SpecHelpers
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
describe "when including" do
|
|
49
|
+
it "should call before :each" do
|
|
50
|
+
@rspec_example_group.should_receive(:before)
|
|
51
|
+
@rspec_example_group.class_eval do
|
|
52
|
+
include FakeFS::SpecHelpers
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/spec/spec.opts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
|
2
|
+
require 'fakefs/safe'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
|
|
5
|
+
class FakeFileTest < Test::Unit::TestCase
|
|
6
|
+
include FakeFS
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
FileSystem.clear
|
|
10
|
+
|
|
11
|
+
@file = FakeFile.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_fake_file_has_empty_content_by_default
|
|
15
|
+
assert_equal "", @file.content
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_fake_file_can_read_and_write_to_content
|
|
19
|
+
@file.content = "foobar"
|
|
20
|
+
assert_equal "foobar", @file.content
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_fake_file_has_1_link_by_default
|
|
24
|
+
assert_equal [@file], @file.links
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_fake_file_can_create_link
|
|
28
|
+
other_file = FakeFile.new
|
|
29
|
+
|
|
30
|
+
@file.link(other_file)
|
|
31
|
+
|
|
32
|
+
assert_equal [@file, other_file], @file.links
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_fake_file_wont_add_link_to_same_file_twice
|
|
36
|
+
other_file = FakeFile.new
|
|
37
|
+
|
|
38
|
+
@file.link other_file
|
|
39
|
+
@file.link other_file
|
|
40
|
+
|
|
41
|
+
assert_equal [@file, other_file], @file.links
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_links_are_mutual
|
|
45
|
+
other_file = FakeFile.new
|
|
46
|
+
|
|
47
|
+
@file.link(other_file)
|
|
48
|
+
|
|
49
|
+
assert_equal [@file, other_file], other_file.links
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_can_link_multiple_files
|
|
53
|
+
file_two = FakeFile.new
|
|
54
|
+
file_three = FakeFile.new
|
|
55
|
+
|
|
56
|
+
@file.link file_two
|
|
57
|
+
@file.link file_three
|
|
58
|
+
|
|
59
|
+
assert_equal [@file, file_two, file_three], @file.links
|
|
60
|
+
assert_equal [@file, file_two, file_three], file_two.links
|
|
61
|
+
assert_equal [@file, file_two, file_three], file_three.links
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_links_share_same_content
|
|
65
|
+
other_file = FakeFile.new
|
|
66
|
+
|
|
67
|
+
@file.link other_file
|
|
68
|
+
|
|
69
|
+
@file.content = "foobar"
|
|
70
|
+
|
|
71
|
+
assert_equal "foobar", other_file.content
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_clone_creates_new_inode
|
|
75
|
+
clone = @file.clone
|
|
76
|
+
assert !clone.inode.equal?(@file.inode)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def test_cloning_does_not_use_same_content_object
|
|
80
|
+
clone = @file.clone
|
|
81
|
+
|
|
82
|
+
clone.content = "foo"
|
|
83
|
+
@file.content = "bar"
|
|
84
|
+
|
|
85
|
+
assert_equal "foo", clone.content
|
|
86
|
+
assert_equal "bar", @file.content
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
|
2
|
+
require 'fakefs/safe'
|
|
3
|
+
require 'test/unit'
|
|
4
|
+
|
|
5
|
+
class FakeSymlinkTest < Test::Unit::TestCase
|
|
6
|
+
include FakeFS
|
|
7
|
+
|
|
8
|
+
def test_symlink_has_method_missing_as_private
|
|
9
|
+
methods = FakeSymlink.private_instance_methods.map { |m| m.to_s }
|
|
10
|
+
assert methods.include?("method_missing")
|
|
11
|
+
end
|
|
12
|
+
end
|