fakefs 0.5.4 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +50 -0
- data/.travis.yml +0 -1
- data/Rakefile +16 -9
- data/fakefs.gemspec +1 -0
- data/lib/fakefs/base.rb +6 -5
- data/lib/fakefs/dir.rb +55 -45
- data/lib/fakefs/fake/dir.rb +5 -3
- data/lib/fakefs/fake/file.rb +9 -4
- data/lib/fakefs/fake/symlink.rb +2 -1
- data/lib/fakefs/file.rb +98 -103
- data/lib/fakefs/file_system.rb +50 -33
- data/lib/fakefs/file_test.rb +1 -0
- data/lib/fakefs/fileutils.rb +72 -71
- data/lib/fakefs/kernel.rb +8 -7
- data/lib/fakefs/pathname.rb +356 -202
- data/lib/fakefs/safe.rb +1 -1
- data/lib/fakefs/spec_helpers.rb +19 -11
- data/lib/fakefs/version.rb +2 -1
- data/spec/fakefs/fakefs_bug_ruby_2.1.0-preview2_spec.rb +2 -2
- data/spec/fakefs/spec_helpers_spec.rb +24 -23
- data/test/dir/tempfile_test.rb +1 -0
- data/test/fake/file/join_test.rb +4 -3
- data/test/fake/file/lstat_test.rb +22 -21
- data/test/fake/file/stat_test.rb +12 -11
- data/test/fake/file/sysseek_test.rb +15 -14
- data/test/fake/file/syswrite_test.rb +25 -24
- data/test/fake/file_test.rb +12 -11
- data/test/fake/symlink_test.rb +18 -10
- data/test/fakefs_test.rb +543 -542
- data/test/file/stat_test.rb +45 -41
- data/test/kernel_test.rb +5 -8
- data/test/safe_test.rb +9 -7
- data/test/test_helper.rb +2 -1
- data/test/verify.rb +6 -3
- metadata +28 -14
data/lib/fakefs/safe.rb
CHANGED
data/lib/fakefs/spec_helpers.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
# FakeFS::SpecHelpers provides a simple macro for RSpec example
|
2
|
-
#
|
3
|
-
#
|
1
|
+
# FakeFS::SpecHelpers provides a simple macro for RSpec example
|
2
|
+
# groups to turn FakeFS on and off.
|
3
|
+
# To use it simply require 'fakefs/spec_helpers', then include
|
4
|
+
# FakeFS::SpecHelpers into any example groups that you wish
|
5
|
+
# to use FakeFS in. For example:
|
4
6
|
#
|
5
7
|
# require 'fakefs/spec_helpers'
|
6
8
|
#
|
@@ -9,12 +11,14 @@
|
|
9
11
|
# ...
|
10
12
|
# end
|
11
13
|
#
|
12
|
-
# By default, including FakeFS::SpecHelpers will run for each
|
13
|
-
#
|
14
|
-
#
|
14
|
+
# By default, including FakeFS::SpecHelpers will run for each
|
15
|
+
# example inside a describe block.
|
16
|
+
# If you want to turn on FakeFS one time only for all your examples,
|
17
|
+
# you will need to include FakeFS::SpecHelpers::All.
|
15
18
|
#
|
16
|
-
# Alternatively, you can include FakeFS::SpecHelpers in all your
|
17
|
-
# configuration block in
|
19
|
+
# Alternatively, you can include FakeFS::SpecHelpers in all your
|
20
|
+
# example groups using RSpec's configuration block in
|
21
|
+
# your spec helper:
|
18
22
|
#
|
19
23
|
# require 'fakefs/spec_helpers'
|
20
24
|
#
|
@@ -22,10 +26,12 @@
|
|
22
26
|
# config.include FakeFS::SpecHelpers
|
23
27
|
# end
|
24
28
|
#
|
25
|
-
# If you do the above then use_fakefs will be available in all of
|
29
|
+
# If you do the above then use_fakefs will be available in all of
|
30
|
+
# your example groups.
|
26
31
|
#
|
27
32
|
require 'fakefs/safe'
|
28
33
|
|
34
|
+
# FakeFS module
|
29
35
|
module FakeFS
|
30
36
|
def use_fakefs(describe_block, opts)
|
31
37
|
describe_block.before opts[:with] do
|
@@ -38,22 +44,24 @@ module FakeFS
|
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
47
|
+
# Module SpecHelpers
|
41
48
|
module SpecHelpers
|
42
49
|
include ::FakeFS
|
43
50
|
|
44
51
|
def self.extended(example_group)
|
45
|
-
example_group.use_fakefs(example_group, :
|
52
|
+
example_group.use_fakefs(example_group, with: :each)
|
46
53
|
end
|
47
54
|
|
48
55
|
def self.included(example_group)
|
49
56
|
example_group.extend self
|
50
57
|
end
|
51
58
|
|
59
|
+
# Module All
|
52
60
|
module All
|
53
61
|
include ::FakeFS
|
54
62
|
|
55
63
|
def self.extended(example_group)
|
56
|
-
example_group.use_fakefs(example_group, :
|
64
|
+
example_group.use_fakefs(example_group, with: :all)
|
57
65
|
end
|
58
66
|
|
59
67
|
def self.included(example_group)
|
data/lib/fakefs/version.rb
CHANGED
@@ -3,11 +3,11 @@ require 'fakefs/spec_helpers'
|
|
3
3
|
|
4
4
|
RSpec.configure do |c|
|
5
5
|
c.mock_with(:rspec)
|
6
|
-
c.include(FakeFS::SpecHelpers, :
|
6
|
+
c.include(FakeFS::SpecHelpers, fakefs: true)
|
7
7
|
end
|
8
8
|
|
9
9
|
if RUBY_VERSION >= '2.1'
|
10
|
-
describe 'Find.find', :
|
10
|
+
describe 'Find.find', fakefs: true do
|
11
11
|
it 'does not give an ArgumentError' do
|
12
12
|
FileUtils.mkdir_p('/tmp/foo')
|
13
13
|
found = Find.find('/tmp').to_a
|
@@ -1,52 +1,53 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
# FakeFs module for tests
|
3
4
|
module FakeFS
|
4
5
|
describe SpecHelpers do
|
5
6
|
before do
|
6
7
|
@rspec_example_group = Class.new do
|
7
|
-
def self.before(
|
8
|
+
def self.before(_sym = :each)
|
8
9
|
yield if block_given?
|
9
10
|
end
|
10
11
|
|
11
|
-
def self.after(
|
12
|
+
def self.after(_sym = :each)
|
12
13
|
yield if block_given?
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
17
|
-
describe
|
18
|
-
context
|
19
|
-
it
|
18
|
+
describe 'when extending' do
|
19
|
+
context 'before each' do
|
20
|
+
it 'should call it' do
|
20
21
|
@rspec_example_group.should_receive(:before).with(:each)
|
21
22
|
@rspec_example_group.extend FakeFS::SpecHelpers
|
22
23
|
end
|
23
24
|
|
24
|
-
it
|
25
|
+
it 'should call FakeFS.activate!' do
|
25
26
|
FakeFS.should_receive(:activate!)
|
26
27
|
@rspec_example_group.extend FakeFS::SpecHelpers
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
context
|
31
|
-
it
|
31
|
+
context 'after each' do
|
32
|
+
it 'should call it' do
|
32
33
|
@rspec_example_group.should_receive(:after).with(:each)
|
33
34
|
@rspec_example_group.extend FakeFS::SpecHelpers
|
34
35
|
end
|
35
36
|
|
36
|
-
it
|
37
|
+
it 'should deactivate fakefs' do
|
37
38
|
FakeFS.should_receive(:deactivate!)
|
38
39
|
@rspec_example_group.extend FakeFS::SpecHelpers
|
39
40
|
end
|
40
41
|
|
41
|
-
it
|
42
|
+
it 'should clear the fakefs filesystem for the next run' do
|
42
43
|
FakeFS::FileSystem.should_receive(:clear)
|
43
44
|
@rspec_example_group.extend FakeFS::SpecHelpers
|
44
45
|
end
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
describe
|
49
|
-
it
|
49
|
+
describe 'when including' do
|
50
|
+
it 'should call before :each' do
|
50
51
|
@rspec_example_group.should_receive(:before)
|
51
52
|
@rspec_example_group.class_eval do
|
52
53
|
include FakeFS::SpecHelpers
|
@@ -55,40 +56,40 @@ module FakeFS
|
|
55
56
|
end
|
56
57
|
|
57
58
|
describe SpecHelpers::All do
|
58
|
-
describe
|
59
|
-
context
|
60
|
-
it
|
59
|
+
describe 'when extending' do
|
60
|
+
context 'before :all' do
|
61
|
+
it 'should call it' do
|
61
62
|
@rspec_example_group.should_receive(:before).with(:all)
|
62
63
|
@rspec_example_group.extend FakeFS::SpecHelpers::All
|
63
64
|
end
|
64
65
|
|
65
|
-
it
|
66
|
+
it 'should call FakeFS.activate!' do
|
66
67
|
FakeFS.should_receive(:activate!)
|
67
68
|
@rspec_example_group.extend FakeFS::SpecHelpers::All
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
71
|
-
context
|
72
|
-
it
|
72
|
+
context 'after :all' do
|
73
|
+
it 'should call it' do
|
73
74
|
@rspec_example_group.should_receive(:after).with(:all)
|
74
75
|
@rspec_example_group.extend FakeFS::SpecHelpers::All
|
75
76
|
end
|
76
77
|
|
77
|
-
it
|
78
|
+
it 'should call FakeFS.deactivate!' do
|
78
79
|
FakeFS.should_receive(:deactivate!)
|
79
80
|
@rspec_example_group.extend FakeFS::SpecHelpers::All
|
80
81
|
end
|
81
82
|
|
82
|
-
it
|
83
|
+
it 'should not call FakeFS::FileSystem.clear' do
|
83
84
|
FakeFS::FileSystem.should_not_receive(:clear)
|
84
85
|
@rspec_example_group.extend FakeFS::SpecHelpers::All
|
85
86
|
end
|
86
87
|
end
|
87
88
|
end
|
88
89
|
|
89
|
-
describe
|
90
|
-
context
|
91
|
-
it
|
90
|
+
describe 'when including' do
|
91
|
+
context 'before :all' do
|
92
|
+
it 'should call it' do
|
92
93
|
@rspec_example_group.should_receive(:before)
|
93
94
|
@rspec_example_group.class_eval do
|
94
95
|
include FakeFS::SpecHelpers::All
|
data/test/dir/tempfile_test.rb
CHANGED
data/test/fake/file/join_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
+
# File join test class
|
3
4
|
class FileJoin < Test::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
FakeFS.activate!
|
@@ -10,10 +11,10 @@ class FileJoin < Test::Unit::TestCase
|
|
10
11
|
end
|
11
12
|
|
12
13
|
[
|
13
|
-
|
14
|
+
%w(a b), %w(a/ b), %w(a /b), %w(a/ /b), %w(a / b)
|
14
15
|
].each_with_index do |args, i|
|
15
16
|
define_method "test_file_join_#{i}" do
|
16
17
|
assert_equal RealFile.join(args), File.join(args)
|
17
18
|
end
|
18
19
|
end
|
19
|
-
end
|
20
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
+
# File stat test class
|
3
4
|
class FileStat < Test::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
FakeFS.activate!
|
@@ -11,49 +12,49 @@ class FileStat < Test::Unit::TestCase
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_calling_lstat_should_create_a_new_file_stat_object
|
14
|
-
File.open(
|
15
|
-
f <<
|
15
|
+
File.open('foo', 'w') do |f|
|
16
|
+
f << 'bar'
|
16
17
|
end
|
17
18
|
|
18
|
-
File.open(
|
19
|
+
File.open('foo') do |f|
|
19
20
|
assert_equal File::Stat, f.lstat.class
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
def test_lstat_should_use_correct_file
|
24
|
-
File.open(
|
25
|
-
f <<
|
25
|
+
File.open('bar', 'w') do |f|
|
26
|
+
f << '1'
|
26
27
|
end
|
27
28
|
|
28
|
-
File.open(
|
29
|
+
File.open('bar') do |f|
|
29
30
|
assert_equal 1, f.lstat.size
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
34
|
def test_lstat_should_report_on_symlink_itself
|
34
|
-
File.open(
|
35
|
-
File.symlink
|
35
|
+
File.open('foo', 'w') { |f| f << 'some content' }
|
36
|
+
File.symlink 'foo', 'my_symlink'
|
36
37
|
|
37
|
-
assert_not_equal File.lstat(
|
38
|
+
assert_not_equal File.lstat('my_symlink').size, File.lstat('foo').size
|
38
39
|
end
|
39
40
|
|
40
41
|
def test_should_report_on_symlink_itself_with_size_instance_method
|
41
|
-
File.open(
|
42
|
-
File.symlink
|
42
|
+
File.open('foo', 'w') { |f| f << 'some content' }
|
43
|
+
File.symlink 'foo', 'my_symlink'
|
43
44
|
|
44
|
-
file = File.open(
|
45
|
-
symlink = File.open(
|
45
|
+
file = File.open('foo')
|
46
|
+
symlink = File.open('my_symlink')
|
46
47
|
|
47
48
|
assert_not_equal file.lstat.size, symlink.lstat.size
|
48
49
|
end
|
49
50
|
|
50
51
|
def test_symlink_size_is_size_of_path_pointed_to
|
51
|
-
File.open(
|
52
|
-
File.symlink
|
53
|
-
assert_equal 1, File.lstat(
|
52
|
+
File.open('a', 'w') { |x| x << 'foobarbazfoobarbaz' }
|
53
|
+
File.symlink 'a', 'one_char_symlink'
|
54
|
+
assert_equal 1, File.lstat('one_char_symlink').size
|
54
55
|
|
55
|
-
File.open(
|
56
|
-
File.symlink
|
57
|
-
assert_equal 2, File.lstat(
|
56
|
+
File.open('ab', 'w') { |x| x << 'foobarbazfoobarbaz' }
|
57
|
+
File.symlink 'ab', 'two_char_symlink'
|
58
|
+
assert_equal 2, File.lstat('two_char_symlink').size
|
58
59
|
end
|
59
|
-
end
|
60
|
+
end
|
data/test/fake/file/stat_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
+
# File stat test class
|
3
4
|
class FileStat < Test::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
FakeFS.activate!
|
@@ -11,29 +12,29 @@ class FileStat < Test::Unit::TestCase
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_calling_stat_should_create_a_new_file_stat_object
|
14
|
-
File.open(
|
15
|
-
f <<
|
15
|
+
File.open('foo', 'w') do |f|
|
16
|
+
f << 'bar'
|
16
17
|
end
|
17
18
|
|
18
|
-
File.open(
|
19
|
+
File.open('foo') do |f|
|
19
20
|
assert_equal File::Stat, f.stat.class
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
def test_stat_should_use_correct_file
|
24
|
-
File.open(
|
25
|
-
f <<
|
25
|
+
File.open('bar', 'w') do |f|
|
26
|
+
f << '1'
|
26
27
|
end
|
27
28
|
|
28
|
-
File.open(
|
29
|
+
File.open('bar') do |f|
|
29
30
|
assert_equal 1, f.stat.size
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
34
|
def test_stat_should_report_on_symlink_pointer
|
34
|
-
File.open(
|
35
|
-
File.symlink
|
35
|
+
File.open('foo', 'w') { |f| f << 'some content' }
|
36
|
+
File.symlink 'foo', 'my_symlink'
|
36
37
|
|
37
|
-
assert_equal File.stat(
|
38
|
+
assert_equal File.stat('my_symlink').size, File.stat('foo').size
|
38
39
|
end
|
39
|
-
end
|
40
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
+
# File SysSeek test class
|
3
4
|
class FileSysSeek < Test::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
FakeFS.activate!
|
@@ -11,11 +12,11 @@ class FileSysSeek < Test::Unit::TestCase
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_should_seek_to_position
|
14
|
-
|
15
|
-
f <<
|
15
|
+
File.open('foo', 'w') do |f|
|
16
|
+
f << '0123456789'
|
16
17
|
end
|
17
18
|
|
18
|
-
File.open(
|
19
|
+
File.open('foo', 'r') do |f|
|
19
20
|
f.sysseek(3)
|
20
21
|
assert_equal 3, f.pos
|
21
22
|
|
@@ -25,20 +26,20 @@ class FileSysSeek < Test::Unit::TestCase
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def test_seek_returns_offset_into_file
|
28
|
-
File.open(
|
29
|
+
File.open('foo', 'w') do |f|
|
29
30
|
# 66 chars long
|
30
|
-
str =
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
str = '0123456789' \
|
32
|
+
'0123456789' \
|
33
|
+
'0123456789' \
|
34
|
+
'0123456789' \
|
35
|
+
'0123456789' \
|
36
|
+
'0123456789' \
|
37
|
+
'012345'
|
37
38
|
|
38
39
|
f << str
|
39
40
|
end
|
40
41
|
|
41
|
-
f = File.open(
|
42
|
+
f = File.open('foo')
|
42
43
|
assert_equal 53, f.sysseek(-13, IO::SEEK_END)
|
43
44
|
end
|
44
|
-
end
|
45
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
|
+
# File SysWrite test class
|
3
4
|
class FileSysWriteTest < Test::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
FakeFS.activate!
|
@@ -11,52 +12,52 @@ class FileSysWriteTest < Test::Unit::TestCase
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def test_returns_one_byte_when_written
|
14
|
-
f = File.open
|
15
|
-
result = f.syswrite
|
15
|
+
f = File.open 'foo', 'w'
|
16
|
+
result = f.syswrite 'a'
|
16
17
|
assert_equal 1, result
|
17
18
|
end
|
18
19
|
|
19
20
|
def test_returns_two_bytes_when_two_written
|
20
|
-
f = File.open
|
21
|
-
result = f.syswrite
|
21
|
+
f = File.open 'foo', 'w'
|
22
|
+
result = f.syswrite 'ab'
|
22
23
|
assert_equal 2, result
|
23
24
|
end
|
24
25
|
|
25
26
|
def test_syswrite_writes_file
|
26
|
-
f = File.open
|
27
|
-
f.syswrite
|
27
|
+
f = File.open 'foo', 'w'
|
28
|
+
f.syswrite 'abcdef'
|
28
29
|
f.close
|
29
30
|
|
30
|
-
assert_equal
|
31
|
+
assert_equal 'abcdef', File.read('foo')
|
31
32
|
end
|
32
33
|
|
33
34
|
def test_writes_to_the_actual_position_when_called_after_buffered_io_read
|
34
|
-
File.open(
|
35
|
-
file.syswrite(
|
35
|
+
File.open('foo', 'w') do |file|
|
36
|
+
file.syswrite('012345678901234567890123456789')
|
36
37
|
end
|
37
38
|
|
38
|
-
file = File.open(
|
39
|
+
file = File.open('foo', 'r+')
|
39
40
|
file.read(5)
|
40
|
-
file.syswrite(
|
41
|
+
file.syswrite('abcde')
|
41
42
|
|
42
|
-
File.open(
|
43
|
-
assert_equal
|
43
|
+
File.open('foo') do |f|
|
44
|
+
assert_equal '01234abcde', f.sysread(10)
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
47
48
|
def test_writes_all_of_the_strings_bytes_but_does_not_buffer_them
|
48
|
-
File.open(
|
49
|
-
file.syswrite(
|
49
|
+
File.open('foo', 'w') do |file|
|
50
|
+
file.syswrite('012345678901234567890123456789')
|
50
51
|
end
|
51
52
|
|
52
|
-
file = File.open(
|
53
|
-
|
53
|
+
file = File.open('foo', 'r+')
|
54
|
+
file.syswrite('abcde')
|
54
55
|
|
55
|
-
File.open(
|
56
|
-
assert_equal
|
57
|
-
|
58
|
-
|
59
|
-
assert_equal
|
56
|
+
File.open('foo') do |f|
|
57
|
+
assert_equal 'abcde56789', f.sysread(10)
|
58
|
+
f.seek(0)
|
59
|
+
f.fsync
|
60
|
+
assert_equal 'abcde56789', f.sysread(10)
|
60
61
|
end
|
61
62
|
end
|
62
|
-
end
|
63
|
+
end
|