mockfs 0.1.1 → 0.1.2
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/lib/mockfs/override.rb +55 -0
- data/lib/mockfs/override.rb~ +32 -0
- data/lib/mockfs.rb +159 -53
- data/lib/mockfs.rb~ +159 -53
- metadata +8 -4
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'mockfs'
|
2
|
+
|
3
|
+
MockFS.mock = true
|
4
|
+
|
5
|
+
Object.send( :remove_const, :FileUtils )
|
6
|
+
module FileUtils
|
7
|
+
def self.method_missing( symbol, *args, &action )
|
8
|
+
MockFS.file_utils.send( symbol, *args, &action )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
getwd = Dir.getwd
|
13
|
+
Object.send( :remove_const, :Dir )
|
14
|
+
module Dir
|
15
|
+
def self.method_missing( symbol, *args, &action )
|
16
|
+
MockFS.dir.send( symbol, *args, &action )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
Dir.module_eval "def self.getwd; '#{ getwd }'; end"
|
20
|
+
|
21
|
+
$join_method = File.method :join
|
22
|
+
$file_constants = {}
|
23
|
+
File.constants.map do |const_str|
|
24
|
+
$file_constants[const_str] = File.const_get const_str.to_sym
|
25
|
+
end
|
26
|
+
Object.send( :remove_const, :File )
|
27
|
+
module File
|
28
|
+
$file_constants.each do |const_str, const_val|
|
29
|
+
self.const_set( const_str, const_val )
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.join( *args )
|
33
|
+
$join_method.call *args
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.method_missing( symbol, *args, &action )
|
37
|
+
MockFS.file.send( symbol, *args, &action )
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
$orig_require = Kernel.method :require
|
42
|
+
def require( library_name )
|
43
|
+
begin
|
44
|
+
super
|
45
|
+
rescue LoadError => err
|
46
|
+
file = library_name
|
47
|
+
file += '.rb' unless library_name =~ /\.rb$/
|
48
|
+
if File.exist? file
|
49
|
+
contents = File.open( file ) do |f| f.gets( nil ); end
|
50
|
+
eval contents
|
51
|
+
else
|
52
|
+
raise
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'mockfs'
|
2
|
+
|
3
|
+
MockFS.mock = true
|
4
|
+
|
5
|
+
Object.send( :remove_const, :FileUtils )
|
6
|
+
module FileUtils
|
7
|
+
def self.method_missing( symbol, *args )
|
8
|
+
MockFS.file_utils.send( symbol, *args )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
getwd = Dir.getwd
|
13
|
+
Object.send( :remove_const, :Dir )
|
14
|
+
module Dir
|
15
|
+
def self.method_missing( symbol, *args )
|
16
|
+
MockFS.dir.send( symbol, *args )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
Dir.module_eval "def self.getwd; '#{ getwd }'; end"
|
20
|
+
|
21
|
+
$join_method = File.method :join
|
22
|
+
Object.send( :remove_const, :File )
|
23
|
+
module File
|
24
|
+
def self.join( *args )
|
25
|
+
$join_method.call *args
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.method_missing( symbol, *args )
|
29
|
+
MockFS.file.send( symbol, *args )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
data/lib/mockfs.rb
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
# It provides mock objects that clone the functionality of File, FileUtils,
|
3
3
|
# Dir, and other in-Ruby file-access libraries.
|
4
4
|
#
|
5
|
-
# To use MockFS in your production code, call MockFS.
|
5
|
+
# To use MockFS in your production code, call MockFS.[ class ].
|
6
6
|
#
|
7
|
-
# MockFS.
|
8
|
-
# MockFS.
|
9
|
-
# MockFS.
|
10
|
-
# MockFS.
|
7
|
+
# MockFS.file => File
|
8
|
+
# MockFS.file_utils => FileUtils
|
9
|
+
# MockFS.dir => Dir
|
10
|
+
# MockFS.dir.entries( '.' ) => [".", "..", ...]
|
11
11
|
#
|
12
12
|
# Then, to turn these into mock instances for a test case, simply call
|
13
13
|
#
|
@@ -16,14 +16,75 @@
|
|
16
16
|
# When you've done this, the normal calls will actually return adapters that
|
17
17
|
# pretend to be the class in question:
|
18
18
|
#
|
19
|
-
# MockFS.
|
20
|
-
# MockFS.
|
21
|
-
# MockFS.
|
22
|
-
# MockFS.
|
19
|
+
# MockFS.file => MockFS::FileAdapter
|
20
|
+
# MockFS.file_utils => MockFS::FileUtilsAdapter
|
21
|
+
# MockFS.dir => MockFS::DirAdapter
|
22
|
+
# MockFS.dir.entries( '.' ) => [".", "..", ...]
|
23
23
|
#
|
24
24
|
# You can get direct access to the enclosed MockFileSystem by calling
|
25
25
|
# MockFS.mock_file_system.
|
26
26
|
#
|
27
|
+
# == Testing example
|
28
|
+
#
|
29
|
+
# For example, let's test a simple function that moves a log file into
|
30
|
+
# somebody's home directory.
|
31
|
+
#
|
32
|
+
# require 'mockfs'
|
33
|
+
#
|
34
|
+
# def move_log
|
35
|
+
# MockFS.file_utils.mv( '/var/log/httpd/access_log', '/home/francis/logs/' )
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# A test of this functionality might look like this:
|
39
|
+
#
|
40
|
+
# require 'test/unit'
|
41
|
+
#
|
42
|
+
# class TestMoveLog < Test::Unit::TestCase
|
43
|
+
# def test_move_log
|
44
|
+
# # Set MockFS to use the mock file system
|
45
|
+
# MockFS.mock = true
|
46
|
+
#
|
47
|
+
# # Fill certain directories
|
48
|
+
# MockFS.fill_path '/var/log/httpd/'
|
49
|
+
# MockFS.fill_path '/home/francis/logs/'
|
50
|
+
#
|
51
|
+
# # Create the access log
|
52
|
+
# MockFS.file.open( '/var/log/httpd/access_log', File::CREAT ) do |f|
|
53
|
+
# f.puts "line 1 of the access log"
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # Run the method
|
57
|
+
# move_log
|
58
|
+
#
|
59
|
+
# # Test that it was moved, along with its contents
|
60
|
+
# assert( MockFS.file.exist?( '/home/francis/logs/access_log' ) )
|
61
|
+
# assert( !MockFS.file.exist?( '/var/log/httpd/access_log' ) )
|
62
|
+
# contents = MockFS.file.open( '/home/francis/logs/access_log' ) do |f|
|
63
|
+
# f.gets( nil )
|
64
|
+
# end
|
65
|
+
# assert_equal( "line 1 of the access log\n", contents )
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# This test will be successful, and it won't litter your real file-system with
|
70
|
+
# testing files.
|
71
|
+
#
|
72
|
+
# == override.rb
|
73
|
+
#
|
74
|
+
# Reading the testing example above, you may be struck by one thing: Using
|
75
|
+
# MockFS requires you to remember to reference it everywhere, making calls such
|
76
|
+
# as MockFS.file_utils.mv instead of just FileUtils.mv. As another option, you
|
77
|
+
# can use File, FileUtils, and Dir directly, and then in your tests, substitute
|
78
|
+
# them by including mockfs/override.rb. I'd recommend using these with caution;
|
79
|
+
# substituting these low-level classes can have unpredictable results.
|
80
|
+
#
|
81
|
+
# Including override.rb also allows Kernel.require to look in your
|
82
|
+
# mock file system for mock Ruby files to include. You might find this useful
|
83
|
+
# if you have configuration files written in Ruby, and you'd like to swap them
|
84
|
+
# out for tests. This is pretty experimental, too.
|
85
|
+
#
|
86
|
+
# == Project page
|
87
|
+
#
|
27
88
|
# You can view the Rubyforge project page at
|
28
89
|
# http://rubyforge.org/projects/mockfs.
|
29
90
|
|
@@ -33,30 +94,23 @@ require 'fileutils'
|
|
33
94
|
require 'singleton'
|
34
95
|
|
35
96
|
module MockFS
|
36
|
-
Version = '0.1.
|
97
|
+
Version = '0.1.2'
|
37
98
|
|
38
99
|
@@mock = false
|
39
100
|
|
40
101
|
def self.method_missing( symbol, *args ) #:nodoc:
|
41
102
|
class_name = nil
|
42
103
|
if symbol.id2name =~ /^get_(.*)/
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
if @@mock
|
51
|
-
return_class = Class.by_name( 'MockFS::' + class_name + 'Adapter' )
|
104
|
+
result = self.send( $1, *args )
|
105
|
+
warn "MockFS.#{ symbol.id2name } is deprecated, use #{ $1 } instead"
|
106
|
+
result
|
107
|
+
else
|
108
|
+
klass = real_class_or_mock_class symbol
|
109
|
+
if klass
|
110
|
+
klass
|
52
111
|
else
|
53
|
-
|
112
|
+
@@mock ? mock_file_system.send( symbol, *args ) : super
|
54
113
|
end
|
55
|
-
return_class || super
|
56
|
-
elsif @@mock
|
57
|
-
mock_file_system.send( symbol, *args )
|
58
|
-
else
|
59
|
-
super
|
60
114
|
end
|
61
115
|
end
|
62
116
|
|
@@ -70,6 +124,20 @@ module MockFS
|
|
70
124
|
@@mock ? MockFileSystem.instance : ( raise RuntimeError )
|
71
125
|
end
|
72
126
|
|
127
|
+
def self.real_class_or_mock_class( symbol ) #:nodoc:
|
128
|
+
class_name = symbol.id2name.capitalize
|
129
|
+
class_name.gsub!( /_(\w)/ ) { |s| $1.capitalize }
|
130
|
+
if %w( Dir File FileUtils ).include?( class_name )
|
131
|
+
if @@mock
|
132
|
+
Class.by_name( 'MockFS::' + class_name + 'Adapter' )
|
133
|
+
else
|
134
|
+
Class.by_name( class_name )
|
135
|
+
end
|
136
|
+
else
|
137
|
+
nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
73
141
|
module Adapter #:nodoc:
|
74
142
|
@@delegated_methods = [ :delete, :entries, :mtime ]
|
75
143
|
|
@@ -101,7 +169,10 @@ module MockFS
|
|
101
169
|
unless get_node( dirname ).class == MockFileSystem::MockDir
|
102
170
|
raise Errno::ENOTDIR
|
103
171
|
end
|
172
|
+
@dirname = dirname
|
104
173
|
end
|
174
|
+
|
175
|
+
def entries; self.class.entries( @dirname ); end
|
105
176
|
end
|
106
177
|
|
107
178
|
class FileAdapter #:nodoc:
|
@@ -124,38 +195,53 @@ module MockFS
|
|
124
195
|
alias_method :exists?, :exist?
|
125
196
|
end
|
126
197
|
|
127
|
-
def self.
|
128
|
-
if
|
129
|
-
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
133
|
-
if mode_string == File::RDONLY
|
134
|
-
node = get_node( fd )
|
135
|
-
if node && node.permissions && node.permissions[0] == 0
|
136
|
-
raise Errno::EACCES
|
137
|
-
end
|
138
|
-
result = action.call( node )
|
139
|
-
node.rewind
|
140
|
-
result
|
198
|
+
def self.get_mock_file( fd, mode ) #:nodoc:
|
199
|
+
if mode.read_only?
|
200
|
+
mock_file = get_node( fd )
|
201
|
+
raise Errno::EACCES if mock_file and !mock_file.readable?
|
141
202
|
else
|
142
203
|
path = Path.new( fd ).absolute
|
143
204
|
dir = get_node( path.parent )
|
144
|
-
if
|
205
|
+
if mode.append?
|
145
206
|
mock_file = get_node( fd )
|
146
207
|
else
|
147
208
|
mock_file = MockFileSystem::MockFile.new( dir, path.node, '' )
|
148
209
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
210
|
+
end
|
211
|
+
mock_file
|
212
|
+
end
|
213
|
+
|
214
|
+
def self.open( fd, mode_string = File::RDONLY, &action )
|
215
|
+
mode = Mode.new mode_string
|
216
|
+
mock_file = get_mock_file( fd, mode )
|
217
|
+
mock_file.pos = mock_file.size if mode.append?
|
218
|
+
if action
|
219
|
+
result = action.call( mock_file )
|
153
220
|
mock_file.rewind
|
154
|
-
if
|
155
|
-
|
221
|
+
if !mode.read_only? and !mode.append?
|
222
|
+
mock_file.parent[Path.new( fd ).absolute.node] = mock_file
|
156
223
|
end
|
224
|
+
result
|
225
|
+
else
|
226
|
+
mock_file
|
157
227
|
end
|
158
228
|
end
|
229
|
+
|
230
|
+
class Mode
|
231
|
+
def initialize( string_or_bitwise )
|
232
|
+
if string_or_bitwise.is_a?( String )
|
233
|
+
if string_or_bitwise == 'w'
|
234
|
+
@bitwise = File::WRONLY
|
235
|
+
end
|
236
|
+
else
|
237
|
+
@bitwise = string_or_bitwise
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def append?; @bitwise & File::APPEND == File::APPEND; end
|
242
|
+
|
243
|
+
def read_only?; @bitwise == File::RDONLY; end
|
244
|
+
end
|
159
245
|
end
|
160
246
|
|
161
247
|
class FileUtilsAdapter #:nodoc:
|
@@ -164,17 +250,27 @@ module MockFS
|
|
164
250
|
def self.cp( src, dest, options = {} )
|
165
251
|
file = get_node( src ).clone
|
166
252
|
dest_path = Path.new( dest ).absolute
|
167
|
-
|
253
|
+
if MockFS.file.exist?( dest )
|
254
|
+
maybe_dest_dir = get_node dest_path
|
255
|
+
if maybe_dest_dir.is_a? MockFileSystem::MockDir
|
256
|
+
dest_path << ( '/' + Path.new( src ).absolute.node )
|
257
|
+
end
|
258
|
+
end
|
259
|
+
dest_dir = get_node dest_path.parent
|
168
260
|
dest_dir[dest_path.node] = file
|
169
261
|
file.name = dest_path.node
|
170
262
|
file.parent = dest_dir
|
171
263
|
end
|
172
264
|
|
173
|
-
|
174
|
-
|
175
|
-
|
265
|
+
class << self
|
266
|
+
def mv( src, dest, options = {} )
|
267
|
+
cp( src, dest, options )
|
268
|
+
MockFS.file.delete( src )
|
269
|
+
end
|
270
|
+
|
271
|
+
alias_method :move, :mv
|
176
272
|
end
|
177
|
-
|
273
|
+
|
178
274
|
def self.touch( file )
|
179
275
|
begin
|
180
276
|
get_node( file ).mtime = Time.now
|
@@ -215,6 +311,10 @@ module MockFS
|
|
215
311
|
|
216
312
|
module Node #:nodoc:
|
217
313
|
attr_accessor :mtime, :name, :parent, :permissions
|
314
|
+
|
315
|
+
def readable?
|
316
|
+
!permissions or permissions[0] != 0
|
317
|
+
end
|
218
318
|
end
|
219
319
|
|
220
320
|
class MockDir < DelegateClass( Hash ) #:nodoc:
|
@@ -264,8 +364,12 @@ module MockFS
|
|
264
364
|
|
265
365
|
def fill_path( dirname )
|
266
366
|
if dirname.size > 1
|
267
|
-
|
268
|
-
|
367
|
+
if dirname.first != '..'
|
368
|
+
dir = fill_dir( dirname.first )
|
369
|
+
dir.fill_path( dirname[1..-1] )
|
370
|
+
else
|
371
|
+
parent.fill_path( dirname[1..-1] )
|
372
|
+
end
|
269
373
|
else
|
270
374
|
fill_dir( dirname )
|
271
375
|
end
|
@@ -290,6 +394,8 @@ module MockFS
|
|
290
394
|
clone.mtime = @mtime
|
291
395
|
clone
|
292
396
|
end
|
397
|
+
|
398
|
+
def close; rewind; end
|
293
399
|
|
294
400
|
def delete; parent.delete( self ); end
|
295
401
|
|
data/lib/mockfs.rb~
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
# It provides mock objects that clone the functionality of File, FileUtils,
|
3
3
|
# Dir, and other in-Ruby file-access libraries.
|
4
4
|
#
|
5
|
-
# To use MockFS in your production code, call MockFS.
|
5
|
+
# To use MockFS in your production code, call MockFS.[ class ].
|
6
6
|
#
|
7
|
-
# MockFS.
|
8
|
-
# MockFS.
|
9
|
-
# MockFS.
|
10
|
-
# MockFS.
|
7
|
+
# MockFS.file => File
|
8
|
+
# MockFS.file_utils => FileUtils
|
9
|
+
# MockFS.dir => Dir
|
10
|
+
# MockFS.dir.entries( '.' ) => [".", "..", ...]
|
11
11
|
#
|
12
12
|
# Then, to turn these into mock instances for a test case, simply call
|
13
13
|
#
|
@@ -16,14 +16,75 @@
|
|
16
16
|
# When you've done this, the normal calls will actually return adapters that
|
17
17
|
# pretend to be the class in question:
|
18
18
|
#
|
19
|
-
# MockFS.
|
20
|
-
# MockFS.
|
21
|
-
# MockFS.
|
22
|
-
# MockFS.
|
19
|
+
# MockFS.file => MockFS::FileAdapter
|
20
|
+
# MockFS.file_utils => MockFS::FileUtilsAdapter
|
21
|
+
# MockFS.dir => MockFS::DirAdapter
|
22
|
+
# MockFS.dir.entries( '.' ) => [".", "..", ...]
|
23
23
|
#
|
24
24
|
# You can get direct access to the enclosed MockFileSystem by calling
|
25
25
|
# MockFS.mock_file_system.
|
26
26
|
#
|
27
|
+
# == Testing example
|
28
|
+
#
|
29
|
+
# For example, let's test a simple function that moves a log file into
|
30
|
+
# somebody's home directory.
|
31
|
+
#
|
32
|
+
# require 'mockfs'
|
33
|
+
#
|
34
|
+
# def move_log
|
35
|
+
# MockFS.file_utils.mv( '/var/log/httpd/access_log', '/home/francis/logs/' )
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# A test of this functionality might look like this:
|
39
|
+
#
|
40
|
+
# require 'test/unit'
|
41
|
+
#
|
42
|
+
# class TestMoveLog < Test::Unit::TestCase
|
43
|
+
# def test_move_log
|
44
|
+
# # Set MockFS to use the mock file system
|
45
|
+
# MockFS.mock = true
|
46
|
+
#
|
47
|
+
# # Fill certain directories
|
48
|
+
# MockFS.fill_path '/var/log/httpd/'
|
49
|
+
# MockFS.fill_path '/home/francis/logs/'
|
50
|
+
#
|
51
|
+
# # Create the access log
|
52
|
+
# MockFS.file.open( '/var/log/httpd/access_log', File::CREAT ) do |f|
|
53
|
+
# f.puts "line 1 of the access log"
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # Run the method
|
57
|
+
# move_log
|
58
|
+
#
|
59
|
+
# # Test that it was moved, along with its contents
|
60
|
+
# assert( MockFS.file.exist?( '/home/francis/logs/access_log' ) )
|
61
|
+
# assert( !MockFS.file.exist?( '/var/log/httpd/access_log' ) )
|
62
|
+
# contents = MockFS.file.open( '/home/francis/logs/access_log' ) do |f|
|
63
|
+
# f.gets( nil )
|
64
|
+
# end
|
65
|
+
# assert_equal( "line 1 of the access log\n", contents )
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# This test will be successful, and it won't litter your real file-system with
|
70
|
+
# testing files.
|
71
|
+
#
|
72
|
+
# == override.rb
|
73
|
+
#
|
74
|
+
# Reading the testing example above, you may be struck by one thing: Using
|
75
|
+
# MockFS requires you to remember to reference it everywhere, making calls such
|
76
|
+
# as MockFS.file_utils.mv instead of just FileUtils.mv. As another option, you
|
77
|
+
# can use File, FileUtils, and Dir directly, and then in your tests, substitute
|
78
|
+
# them by including mockfs/override.rb. I'd recommend using these with caution;
|
79
|
+
# substituting these low-level classes can have unpredictable results.
|
80
|
+
#
|
81
|
+
# Including override.rb also allows Kernel.require to look in your
|
82
|
+
# mock file system for mock Ruby files to include. You might find this useful
|
83
|
+
# if you have configuration files written in Ruby, and you'd like to swap them
|
84
|
+
# out for tests. This is pretty experimental, too.
|
85
|
+
#
|
86
|
+
# == Project page
|
87
|
+
#
|
27
88
|
# You can view the Rubyforge project page at
|
28
89
|
# http://rubyforge.org/projects/mockfs.
|
29
90
|
|
@@ -33,30 +94,23 @@ require 'fileutils'
|
|
33
94
|
require 'singleton'
|
34
95
|
|
35
96
|
module MockFS
|
36
|
-
Version = '0.1.
|
97
|
+
Version = '0.1.1'
|
37
98
|
|
38
99
|
@@mock = false
|
39
100
|
|
40
101
|
def self.method_missing( symbol, *args ) #:nodoc:
|
41
102
|
class_name = nil
|
42
103
|
if symbol.id2name =~ /^get_(.*)/
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
if @@mock
|
51
|
-
return_class = Class.by_name( 'MockFS::' + class_name + 'Adapter' )
|
104
|
+
result = self.send( $1, *args )
|
105
|
+
warn "MockFS.#{ symbol.id2name } is deprecated, use #{ $1 } instead"
|
106
|
+
result
|
107
|
+
else
|
108
|
+
klass = real_class_or_mock_class symbol
|
109
|
+
if klass
|
110
|
+
klass
|
52
111
|
else
|
53
|
-
|
112
|
+
@@mock ? mock_file_system.send( symbol, *args ) : super
|
54
113
|
end
|
55
|
-
return_class || super
|
56
|
-
elsif @@mock
|
57
|
-
mock_file_system.send( symbol, *args )
|
58
|
-
else
|
59
|
-
super
|
60
114
|
end
|
61
115
|
end
|
62
116
|
|
@@ -70,6 +124,20 @@ module MockFS
|
|
70
124
|
@@mock ? MockFileSystem.instance : ( raise RuntimeError )
|
71
125
|
end
|
72
126
|
|
127
|
+
def self.real_class_or_mock_class( symbol ) #:nodoc:
|
128
|
+
class_name = symbol.id2name.capitalize
|
129
|
+
class_name.gsub!( /_(\w)/ ) { |s| $1.capitalize }
|
130
|
+
if %w( Dir File FileUtils ).include?( class_name )
|
131
|
+
if @@mock
|
132
|
+
Class.by_name( 'MockFS::' + class_name + 'Adapter' )
|
133
|
+
else
|
134
|
+
Class.by_name( class_name )
|
135
|
+
end
|
136
|
+
else
|
137
|
+
nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
73
141
|
module Adapter #:nodoc:
|
74
142
|
@@delegated_methods = [ :delete, :entries, :mtime ]
|
75
143
|
|
@@ -101,7 +169,10 @@ module MockFS
|
|
101
169
|
unless get_node( dirname ).class == MockFileSystem::MockDir
|
102
170
|
raise Errno::ENOTDIR
|
103
171
|
end
|
172
|
+
@dirname = dirname
|
104
173
|
end
|
174
|
+
|
175
|
+
def entries; self.class.entries( @dirname ); end
|
105
176
|
end
|
106
177
|
|
107
178
|
class FileAdapter #:nodoc:
|
@@ -124,38 +195,53 @@ module MockFS
|
|
124
195
|
alias_method :exists?, :exist?
|
125
196
|
end
|
126
197
|
|
127
|
-
def self.
|
128
|
-
if
|
129
|
-
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
133
|
-
if mode_string == File::RDONLY
|
134
|
-
node = get_node( fd )
|
135
|
-
if node && node.permissions && node.permissions[0] == 0
|
136
|
-
raise Errno::EACCES
|
137
|
-
end
|
138
|
-
result = action.call( node )
|
139
|
-
node.rewind
|
140
|
-
result
|
198
|
+
def self.get_mock_file( fd, mode ) #:nodoc:
|
199
|
+
if mode.read_only?
|
200
|
+
mock_file = get_node( fd )
|
201
|
+
raise Errno::EACCES if mock_file and !mock_file.readable?
|
141
202
|
else
|
142
203
|
path = Path.new( fd ).absolute
|
143
204
|
dir = get_node( path.parent )
|
144
|
-
if
|
205
|
+
if mode.append?
|
145
206
|
mock_file = get_node( fd )
|
146
207
|
else
|
147
208
|
mock_file = MockFileSystem::MockFile.new( dir, path.node, '' )
|
148
209
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
210
|
+
end
|
211
|
+
mock_file
|
212
|
+
end
|
213
|
+
|
214
|
+
def self.open( fd, mode_string = File::RDONLY, &action )
|
215
|
+
mode = Mode.new mode_string
|
216
|
+
mock_file = get_mock_file( fd, mode )
|
217
|
+
mock_file.pos = mock_file.size if mode.append?
|
218
|
+
if action
|
219
|
+
result = action.call( mock_file )
|
153
220
|
mock_file.rewind
|
154
|
-
if
|
155
|
-
|
221
|
+
if !mode.read_only? and !mode.append?
|
222
|
+
mock_file.parent[Path.new( fd ).absolute.node] = mock_file
|
156
223
|
end
|
224
|
+
result
|
225
|
+
else
|
226
|
+
mock_file
|
157
227
|
end
|
158
228
|
end
|
229
|
+
|
230
|
+
class Mode
|
231
|
+
def initialize( string_or_bitwise )
|
232
|
+
if string_or_bitwise.is_a?( String )
|
233
|
+
if string_or_bitwise == 'w'
|
234
|
+
@bitwise = File::WRONLY
|
235
|
+
end
|
236
|
+
else
|
237
|
+
@bitwise = string_or_bitwise
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def append?; @bitwise & File::APPEND == File::APPEND; end
|
242
|
+
|
243
|
+
def read_only?; @bitwise == File::RDONLY; end
|
244
|
+
end
|
159
245
|
end
|
160
246
|
|
161
247
|
class FileUtilsAdapter #:nodoc:
|
@@ -164,17 +250,27 @@ module MockFS
|
|
164
250
|
def self.cp( src, dest, options = {} )
|
165
251
|
file = get_node( src ).clone
|
166
252
|
dest_path = Path.new( dest ).absolute
|
167
|
-
|
253
|
+
if MockFS.file.exist?( dest )
|
254
|
+
maybe_dest_dir = get_node dest_path
|
255
|
+
if maybe_dest_dir.is_a? MockFileSystem::MockDir
|
256
|
+
dest_path << ( '/' + Path.new( src ).absolute.node )
|
257
|
+
end
|
258
|
+
end
|
259
|
+
dest_dir = get_node dest_path.parent
|
168
260
|
dest_dir[dest_path.node] = file
|
169
261
|
file.name = dest_path.node
|
170
262
|
file.parent = dest_dir
|
171
263
|
end
|
172
264
|
|
173
|
-
|
174
|
-
|
175
|
-
|
265
|
+
class << self
|
266
|
+
def mv( src, dest, options = {} )
|
267
|
+
cp( src, dest, options )
|
268
|
+
MockFS.file.delete( src )
|
269
|
+
end
|
270
|
+
|
271
|
+
alias_method :move, :mv
|
176
272
|
end
|
177
|
-
|
273
|
+
|
178
274
|
def self.touch( file )
|
179
275
|
begin
|
180
276
|
get_node( file ).mtime = Time.now
|
@@ -215,6 +311,10 @@ module MockFS
|
|
215
311
|
|
216
312
|
module Node #:nodoc:
|
217
313
|
attr_accessor :mtime, :name, :parent, :permissions
|
314
|
+
|
315
|
+
def readable?
|
316
|
+
!permissions or permissions[0] != 0
|
317
|
+
end
|
218
318
|
end
|
219
319
|
|
220
320
|
class MockDir < DelegateClass( Hash ) #:nodoc:
|
@@ -264,8 +364,12 @@ module MockFS
|
|
264
364
|
|
265
365
|
def fill_path( dirname )
|
266
366
|
if dirname.size > 1
|
267
|
-
|
268
|
-
|
367
|
+
if dirname.first != '..'
|
368
|
+
dir = fill_dir( dirname.first )
|
369
|
+
dir.fill_path( dirname[1..-1] )
|
370
|
+
else
|
371
|
+
parent.fill_path( dirname[1..-1] )
|
372
|
+
end
|
269
373
|
else
|
270
374
|
fill_dir( dirname )
|
271
375
|
end
|
@@ -290,6 +394,8 @@ module MockFS
|
|
290
394
|
clone.mtime = @mtime
|
291
395
|
clone
|
292
396
|
end
|
397
|
+
|
398
|
+
def close; rewind; end
|
293
399
|
|
294
400
|
def delete; parent.delete( self ); end
|
295
401
|
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.6
|
3
3
|
specification_version: 1
|
4
4
|
name: mockfs
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2005-
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2005-09-19
|
8
8
|
summary: MockFS is a test-obsessed library for mocking out the entire file system.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
author: Francis Hwang
|
12
11
|
email: sera@fhwang.net
|
13
12
|
homepage: http://mockfs.rubyforge.org/
|
14
13
|
rubyforge_project:
|
@@ -27,9 +26,14 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
27
26
|
version: 0.0.0
|
28
27
|
version:
|
29
28
|
platform: ruby
|
29
|
+
authors:
|
30
|
+
- Francis Hwang
|
30
31
|
files:
|
32
|
+
- lib/mockfs
|
31
33
|
- lib/mockfs.rb
|
32
34
|
- lib/mockfs.rb~
|
35
|
+
- lib/mockfs/override.rb
|
36
|
+
- lib/mockfs/override.rb~
|
33
37
|
test_files: []
|
34
38
|
rdoc_options: []
|
35
39
|
extra_rdoc_files: []
|