mockfs 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mockfs/override.rb +31 -6
- data/lib/mockfs/override.rb~ +32 -6
- data/lib/mockfs.rb +143 -47
- data/lib/mockfs.rb~ +98 -43
- metadata +34 -29
data/lib/mockfs/override.rb
CHANGED
@@ -1,48 +1,73 @@
|
|
1
|
+
# Include this file to redefine FileUtils, Dir, and File. The point of this is so that you can use MockFS to test a codebase that calls these classes directly instead of going through MockFS.file_utils, etc.
|
2
|
+
#
|
3
|
+
# Please note that as of this writing (June 11, 2006), this file is still fairly experimental. Please exercise caution when relying on tests through this.
|
4
|
+
|
1
5
|
require 'mockfs'
|
2
6
|
|
3
7
|
MockFS.mock = true
|
4
8
|
|
9
|
+
MockFS::OriginalFileUtils = FileUtils
|
5
10
|
Object.send( :remove_const, :FileUtils )
|
6
|
-
module FileUtils
|
11
|
+
module FileUtils #:nodoc:
|
7
12
|
def self.method_missing( symbol, *args, &action )
|
8
|
-
MockFS.
|
13
|
+
file_utils = MockFS.mock? ? MockFS.file_utils : MockFS::OriginalFileUtils
|
14
|
+
file_utils.send( symbol, *args, &action )
|
9
15
|
end
|
10
16
|
end
|
11
17
|
|
12
18
|
getwd = Dir.getwd
|
19
|
+
MockFS::OriginalDir = Dir
|
13
20
|
Object.send( :remove_const, :Dir )
|
14
|
-
module Dir
|
21
|
+
module Dir #:nodoc:
|
15
22
|
def self.method_missing( symbol, *args, &action )
|
16
|
-
MockFS.
|
23
|
+
dir = MockFS.mock? ? MockFS.dir : MockFS::OriginalDir
|
24
|
+
dir.send( symbol, *args, &action )
|
17
25
|
end
|
18
26
|
end
|
19
27
|
Dir.module_eval "def self.getwd; '#{ getwd }'; end"
|
20
28
|
|
21
29
|
$join_method = File.method :join
|
30
|
+
$dirname_method = File.method :dirname
|
22
31
|
$file_constants = {}
|
23
32
|
File.constants.map do |const_str|
|
24
33
|
$file_constants[const_str] = File.const_get const_str.to_sym
|
25
34
|
end
|
35
|
+
MockFS::OriginalFile = File
|
26
36
|
Object.send( :remove_const, :File )
|
27
|
-
module File
|
37
|
+
module File #:nodoc:
|
28
38
|
$file_constants.each do |const_str, const_val|
|
29
39
|
self.const_set( const_str, const_val )
|
30
40
|
end
|
31
41
|
|
42
|
+
def self.dirname( *args ); $dirname_method.call( *args ); end
|
43
|
+
|
32
44
|
def self.join( *args )
|
33
45
|
$join_method.call *args
|
34
46
|
end
|
35
47
|
|
36
48
|
def self.method_missing( symbol, *args, &action )
|
37
|
-
MockFS.
|
49
|
+
file = MockFS.mock? ? MockFS.file : MockFS::OriginalFile
|
50
|
+
file.send( symbol, *args, &action )
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
41
54
|
$orig_require = Kernel.method :require
|
55
|
+
|
56
|
+
# mockfs/override.rb overrides Kernel.require to allow you to include Ruby files that are in the mock file system:
|
57
|
+
#
|
58
|
+
# require 'mockfs/override'
|
59
|
+
# MockFS.file.open( 'virtual.rb', 'w' ) do |f|
|
60
|
+
# f.puts "puts 'I am a ruby program living in a virtual file'"
|
61
|
+
# end
|
62
|
+
# require 'virtual.rb'
|
63
|
+
# require 'rexml' # real files are still accessible
|
42
64
|
def require( library_name )
|
43
65
|
begin
|
66
|
+
MockFS.mock = false
|
44
67
|
super
|
68
|
+
MockFS.mock = true
|
45
69
|
rescue LoadError => err
|
70
|
+
MockFS.mock = true
|
46
71
|
file = library_name
|
47
72
|
file += '.rb' unless library_name =~ /\.rb$/
|
48
73
|
if File.exist? file
|
data/lib/mockfs/override.rb~
CHANGED
@@ -4,29 +4,55 @@ MockFS.mock = true
|
|
4
4
|
|
5
5
|
Object.send( :remove_const, :FileUtils )
|
6
6
|
module FileUtils
|
7
|
-
def self.method_missing( symbol, *args )
|
8
|
-
MockFS.file_utils.send( symbol, *args )
|
7
|
+
def self.method_missing( symbol, *args, &action )
|
8
|
+
MockFS.file_utils.send( symbol, *args, &action )
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
getwd = Dir.getwd
|
13
13
|
Object.send( :remove_const, :Dir )
|
14
14
|
module Dir
|
15
|
-
def self.method_missing( symbol, *args )
|
16
|
-
MockFS.dir.send( symbol, *args )
|
15
|
+
def self.method_missing( symbol, *args, &action )
|
16
|
+
MockFS.dir.send( symbol, *args, &action )
|
17
17
|
end
|
18
18
|
end
|
19
19
|
Dir.module_eval "def self.getwd; '#{ getwd }'; end"
|
20
20
|
|
21
21
|
$join_method = File.method :join
|
22
|
+
$dirname_method = File.method :dirname
|
23
|
+
$file_constants = {}
|
24
|
+
File.constants.map do |const_str|
|
25
|
+
$file_constants[const_str] = File.const_get const_str.to_sym
|
26
|
+
end
|
22
27
|
Object.send( :remove_const, :File )
|
23
28
|
module File
|
29
|
+
$file_constants.each do |const_str, const_val|
|
30
|
+
self.const_set( const_str, const_val )
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.dirname( *args ); $dirname_method.call( *args ); end
|
34
|
+
|
24
35
|
def self.join( *args )
|
25
36
|
$join_method.call *args
|
26
37
|
end
|
27
38
|
|
28
|
-
def self.method_missing( symbol, *args )
|
29
|
-
MockFS.file.send( symbol, *args )
|
39
|
+
def self.method_missing( symbol, *args, &action )
|
40
|
+
MockFS.file.send( symbol, *args, &action )
|
30
41
|
end
|
31
42
|
end
|
32
43
|
|
44
|
+
$orig_require = Kernel.method :require
|
45
|
+
def require( library_name )
|
46
|
+
begin
|
47
|
+
super
|
48
|
+
rescue LoadError => err
|
49
|
+
file = library_name
|
50
|
+
file += '.rb' unless library_name =~ /\.rb$/
|
51
|
+
if File.exist? file
|
52
|
+
contents = File.open( file ) do |f| f.gets( nil ); end
|
53
|
+
eval contents
|
54
|
+
else
|
55
|
+
raise
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/mockfs.rb
CHANGED
@@ -94,7 +94,7 @@ require 'fileutils'
|
|
94
94
|
require 'singleton'
|
95
95
|
|
96
96
|
module MockFS
|
97
|
-
Version = '0.1.
|
97
|
+
Version = '0.1.3'
|
98
98
|
|
99
99
|
@@mock = false
|
100
100
|
|
@@ -118,6 +118,9 @@ module MockFS
|
|
118
118
|
# +is_mock+ should be a Boolean.
|
119
119
|
def self.mock= ( is_mock ); @@mock = is_mock; end
|
120
120
|
|
121
|
+
# Returns +true+ or +false+ depending on whether MockFS is using the mock file system.
|
122
|
+
def self.mock?; @@mock; end
|
123
|
+
|
121
124
|
# If we're in mock mode, this will return the MockFileSystem; otherwise it
|
122
125
|
# will raise a RuntimeError.
|
123
126
|
def self.mock_file_system
|
@@ -141,16 +144,16 @@ module MockFS
|
|
141
144
|
module Adapter #:nodoc:
|
142
145
|
@@delegated_methods = [ :delete, :entries, :mtime ]
|
143
146
|
|
144
|
-
def get_node( nodename ); MockFileSystem.instance.get_node( nodename ); end
|
145
|
-
|
146
147
|
def method_missing( sym, *args )
|
147
148
|
if @@delegated_methods.include?( sym )
|
148
|
-
|
149
|
+
node( args.first ).send( sym )
|
149
150
|
else
|
150
151
|
super
|
151
152
|
end
|
152
153
|
end
|
153
154
|
|
155
|
+
def node( nodename ); MockFileSystem.instance.node( nodename ); end
|
156
|
+
|
154
157
|
def respond_to?( sym )
|
155
158
|
@@delegated_methods.include?( sym ) ? true : super
|
156
159
|
end
|
@@ -160,32 +163,99 @@ module MockFS
|
|
160
163
|
extend Adapter
|
161
164
|
include Adapter
|
162
165
|
|
166
|
+
def self.[]( string ); glob( string, 0 ); end
|
167
|
+
|
168
|
+
def self.glob( string, flags = 0 )
|
169
|
+
DirAdapter.new( '.' ).send( :glob, string, flags ).map { |result|
|
170
|
+
Path.new( result )[1..-1]
|
171
|
+
}
|
172
|
+
end
|
173
|
+
|
163
174
|
def self.mkdir( dirname )
|
164
175
|
path = Path.new( dirname ).absolute
|
165
|
-
|
176
|
+
node( path.parent ).mkdir( path.node )
|
177
|
+
end
|
178
|
+
|
179
|
+
def self.rmdir( dirname )
|
180
|
+
path = Path.new( dirname ).absolute
|
181
|
+
node( path ).delete
|
166
182
|
end
|
167
183
|
|
184
|
+
attr_reader :path
|
185
|
+
|
168
186
|
def initialize( dirname )
|
169
|
-
unless
|
187
|
+
unless node( dirname ).class == MockFileSystem::MockDir
|
170
188
|
raise Errno::ENOTDIR
|
171
189
|
end
|
172
|
-
@
|
190
|
+
@path = dirname
|
191
|
+
end
|
192
|
+
|
193
|
+
def entries; self.class.entries( @path ); end
|
194
|
+
|
195
|
+
protected
|
196
|
+
|
197
|
+
def glob( string, flags = 0 )
|
198
|
+
glob_path = Path.new string
|
199
|
+
if glob_path.size > 1
|
200
|
+
if glob_path.first != '**'
|
201
|
+
subdir = DirAdapter.new( File.join( self.path, glob_path.first ) )
|
202
|
+
subdir.send( :glob, glob_path[1..-1], flags )
|
203
|
+
else
|
204
|
+
if glob_path.size > 2 and glob_path[1] == Path.new( path ).last
|
205
|
+
glob( glob_path[2..-1], flags )
|
206
|
+
else
|
207
|
+
if glob_path.size == 2
|
208
|
+
matches = match_entries( glob_path[-1], flags )
|
209
|
+
else
|
210
|
+
matches = []
|
211
|
+
end
|
212
|
+
DirAdapter.entries( path ).each do |entry|
|
213
|
+
unless %w( . .. ).include? entry
|
214
|
+
if FileAdapter.directory? File.join( path, entry )
|
215
|
+
subdir = DirAdapter.new File.join( self.path, entry )
|
216
|
+
matches << subdir.send( :glob, glob_path, flags )
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
matches.flatten.uniq
|
221
|
+
end
|
222
|
+
end
|
223
|
+
else
|
224
|
+
match_entries( string, flags )
|
225
|
+
end
|
173
226
|
end
|
174
227
|
|
175
|
-
def
|
228
|
+
def match_entries( string, flags )
|
229
|
+
string = string.gsub( /\./, '\.' )
|
230
|
+
string = string.gsub( /\?/, '.' )
|
231
|
+
string = string.gsub( /\*/, ".*" )
|
232
|
+
string = string.gsub( /\{(.+),(.+)\}/, '(\1|\2)' )
|
233
|
+
re = Regexp.new string
|
234
|
+
DirAdapter.entries( path ).select { |entry|
|
235
|
+
flags & File::FNM_DOTMATCH != 0 || !%w( . .. ).include?( entry )
|
236
|
+
}.select { |entry| entry =~ re }.map { |entry| File.join( path, entry ) }
|
237
|
+
end
|
176
238
|
end
|
177
239
|
|
178
240
|
class FileAdapter #:nodoc:
|
179
241
|
extend Adapter
|
180
242
|
|
181
243
|
def self.chmod( perms, *filenames )
|
182
|
-
|
244
|
+
node( filenames.first ).permissions = perms
|
245
|
+
end
|
246
|
+
|
247
|
+
def self.directory?( file_name )
|
248
|
+
begin
|
249
|
+
node( file_name ).is_a? MockFileSystem::MockDir
|
250
|
+
rescue Errno::ENOENT
|
251
|
+
false
|
252
|
+
end
|
183
253
|
end
|
184
254
|
|
185
255
|
class << self
|
186
256
|
def exist?( filename )
|
187
257
|
begin
|
188
|
-
|
258
|
+
node( filename )
|
189
259
|
true
|
190
260
|
rescue Errno::ENOENT
|
191
261
|
false
|
@@ -194,16 +264,24 @@ module MockFS
|
|
194
264
|
|
195
265
|
alias_method :exists?, :exist?
|
196
266
|
end
|
197
|
-
|
198
|
-
def self.
|
267
|
+
|
268
|
+
def self.file?( file_name )
|
269
|
+
begin
|
270
|
+
node( file_name ).is_a? MockFileSystem::MockFile
|
271
|
+
rescue Errno::ENOENT
|
272
|
+
false
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def self.mock_file( fd, mode ) #:nodoc:
|
199
277
|
if mode.read_only?
|
200
|
-
mock_file =
|
278
|
+
mock_file = node( fd )
|
201
279
|
raise Errno::EACCES if mock_file and !mock_file.readable?
|
202
280
|
else
|
203
281
|
path = Path.new( fd ).absolute
|
204
|
-
dir =
|
282
|
+
dir = node( path.parent )
|
205
283
|
if mode.append?
|
206
|
-
mock_file =
|
284
|
+
mock_file = node( fd )
|
207
285
|
else
|
208
286
|
mock_file = MockFileSystem::MockFile.new( dir, path.node, '' )
|
209
287
|
end
|
@@ -213,7 +291,7 @@ module MockFS
|
|
213
291
|
|
214
292
|
def self.open( fd, mode_string = File::RDONLY, &action )
|
215
293
|
mode = Mode.new mode_string
|
216
|
-
mock_file =
|
294
|
+
mock_file = mock_file( fd, mode )
|
217
295
|
mock_file.pos = mock_file.size if mode.append?
|
218
296
|
if action
|
219
297
|
result = action.call( mock_file )
|
@@ -227,11 +305,21 @@ module MockFS
|
|
227
305
|
end
|
228
306
|
end
|
229
307
|
|
230
|
-
|
308
|
+
def self.read( name )
|
309
|
+
mfile = node name
|
310
|
+
raise Errno::EISDIR if mfile.is_a? MockFileSystem::MockDir
|
311
|
+
mfile.read
|
312
|
+
end
|
313
|
+
|
314
|
+
class Mode #:nodoc:
|
231
315
|
def initialize( string_or_bitwise )
|
232
316
|
if string_or_bitwise.is_a?( String )
|
233
317
|
if string_or_bitwise == 'w'
|
234
318
|
@bitwise = File::WRONLY
|
319
|
+
elsif string_or_bitwise == 'r'
|
320
|
+
@bitwise = File::RDONLY
|
321
|
+
elsif string_or_bitwise == 'a'
|
322
|
+
@bitwise = File::APPEND
|
235
323
|
end
|
236
324
|
else
|
237
325
|
@bitwise = string_or_bitwise
|
@@ -248,15 +336,15 @@ module MockFS
|
|
248
336
|
extend Adapter
|
249
337
|
|
250
338
|
def self.cp( src, dest, options = {} )
|
251
|
-
file =
|
339
|
+
file = node( src ).clone
|
252
340
|
dest_path = Path.new( dest ).absolute
|
253
341
|
if MockFS.file.exist?( dest )
|
254
|
-
maybe_dest_dir =
|
342
|
+
maybe_dest_dir = node dest_path
|
255
343
|
if maybe_dest_dir.is_a? MockFileSystem::MockDir
|
256
344
|
dest_path << ( '/' + Path.new( src ).absolute.node )
|
257
345
|
end
|
258
346
|
end
|
259
|
-
dest_dir =
|
347
|
+
dest_dir = node dest_path.parent
|
260
348
|
dest_dir[dest_path.node] = file
|
261
349
|
file.name = dest_path.node
|
262
350
|
file.parent = dest_dir
|
@@ -273,10 +361,10 @@ module MockFS
|
|
273
361
|
|
274
362
|
def self.touch( file )
|
275
363
|
begin
|
276
|
-
|
364
|
+
node( file ).mtime = Time.now
|
277
365
|
rescue Errno::ENOENT
|
278
366
|
path = Path.new( file ).absolute
|
279
|
-
parent_dir =
|
367
|
+
parent_dir = node( path.parent )
|
280
368
|
file = MockFileSystem::MockFile.new( parent_dir, path.node, nil )
|
281
369
|
parent_dir[path.node] = file
|
282
370
|
end
|
@@ -297,10 +385,6 @@ module MockFS
|
|
297
385
|
# Flushes all file information out of the MockFileSystem.
|
298
386
|
def flush; @root = MockRoot.new( '' ); fill_path( '.' ); end
|
299
387
|
|
300
|
-
def get_node( dirname ) #:nodoc:
|
301
|
-
@root.get_node( dirname )
|
302
|
-
end
|
303
|
-
|
304
388
|
# Use this method to fill in directory paths. This is the same as calling mkdir a bunch of times.
|
305
389
|
#
|
306
390
|
# MockFS.mock_file_system.fill_path '/usr/local/bin'
|
@@ -309,6 +393,10 @@ module MockFS
|
|
309
393
|
@root.fill_path( Path.new( dirname ).absolute.strip )
|
310
394
|
end
|
311
395
|
|
396
|
+
def node( dirname ) #:nodoc:
|
397
|
+
@root.node( dirname )
|
398
|
+
end
|
399
|
+
|
312
400
|
module Node #:nodoc:
|
313
401
|
attr_accessor :mtime, :name, :parent, :permissions
|
314
402
|
|
@@ -337,21 +425,6 @@ module MockFS
|
|
337
425
|
|
338
426
|
def entries; %w( . .. ).concat( keys ); end
|
339
427
|
|
340
|
-
def get_node( dirname )
|
341
|
-
if dirname.first == '..'
|
342
|
-
self.parent.get_node( dirname[1..-1] )
|
343
|
-
elsif dirname.first == '.'
|
344
|
-
self.get_node( dirname[1..-1] )
|
345
|
-
elsif dirname.size > 1
|
346
|
-
subdir = self[dirname.first]
|
347
|
-
subdir ? subdir.get_node( dirname[1..-1] ) : ( raise Errno::ENOENT )
|
348
|
-
elsif dirname == ''
|
349
|
-
self
|
350
|
-
else
|
351
|
-
self[dirname.strip] or ( raise Errno::ENOENT )
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
428
|
def fill_dir( dirname )
|
356
429
|
dir = self[dirname]
|
357
430
|
if dir.nil?
|
@@ -376,6 +449,21 @@ module MockFS
|
|
376
449
|
end
|
377
450
|
|
378
451
|
def mkdir( dirname ); fill_path( Path.new( dirname ) ); end
|
452
|
+
|
453
|
+
def node( dirname )
|
454
|
+
if dirname.first == '..'
|
455
|
+
self.parent.node( dirname[1..-1] )
|
456
|
+
elsif dirname.first == '.'
|
457
|
+
self.node( dirname[1..-1] )
|
458
|
+
elsif dirname.size > 1
|
459
|
+
subdir = self[dirname.first]
|
460
|
+
subdir ? subdir.node( dirname[1..-1] ) : ( raise Errno::ENOENT )
|
461
|
+
elsif dirname == ''
|
462
|
+
self
|
463
|
+
else
|
464
|
+
self[dirname.strip] or ( raise Errno::ENOENT )
|
465
|
+
end
|
466
|
+
end
|
379
467
|
end
|
380
468
|
|
381
469
|
class MockFile < DelegateClass( StringIO ) #:nodoc:
|
@@ -407,7 +495,7 @@ module MockFS
|
|
407
495
|
end
|
408
496
|
|
409
497
|
class MockRoot < MockDir #:nodoc:
|
410
|
-
def
|
498
|
+
def node( dirname )
|
411
499
|
begin
|
412
500
|
super( Path.new( dirname ).absolute.strip )
|
413
501
|
rescue Errno::ENOENT
|
@@ -425,7 +513,13 @@ module MockFS
|
|
425
513
|
@@getwd
|
426
514
|
end
|
427
515
|
|
428
|
-
def []( *args )
|
516
|
+
def []( *args )
|
517
|
+
if args.size == 1 and args.first.is_a? Fixnum
|
518
|
+
Path.new self.split( "/" )[*args]
|
519
|
+
else
|
520
|
+
Path.new self.split( "/" )[*args].join( "/" )
|
521
|
+
end
|
522
|
+
end
|
429
523
|
|
430
524
|
def absolute
|
431
525
|
if self =~ %r{^\w}
|
@@ -440,14 +534,16 @@ module MockFS
|
|
440
534
|
|
441
535
|
def first; self.split( "/" ).first; end
|
442
536
|
|
443
|
-
def
|
537
|
+
def last; self.split( "/" ).last; end
|
538
|
+
|
539
|
+
def node
|
444
540
|
self =~ %r{^(.*)/(.*?)$}
|
445
|
-
$
|
541
|
+
$2
|
446
542
|
end
|
447
543
|
|
448
|
-
def
|
544
|
+
def parent
|
449
545
|
self =~ %r{^(.*)/(.*?)$}
|
450
|
-
$
|
546
|
+
$1
|
451
547
|
end
|
452
548
|
|
453
549
|
def size; self.split( '/' ).size; end
|
data/lib/mockfs.rb~
CHANGED
@@ -94,7 +94,7 @@ require 'fileutils'
|
|
94
94
|
require 'singleton'
|
95
95
|
|
96
96
|
module MockFS
|
97
|
-
Version = '0.1.
|
97
|
+
Version = '0.1.2'
|
98
98
|
|
99
99
|
@@mock = false
|
100
100
|
|
@@ -141,16 +141,16 @@ module MockFS
|
|
141
141
|
module Adapter #:nodoc:
|
142
142
|
@@delegated_methods = [ :delete, :entries, :mtime ]
|
143
143
|
|
144
|
-
def get_node( nodename ); MockFileSystem.instance.get_node( nodename ); end
|
145
|
-
|
146
144
|
def method_missing( sym, *args )
|
147
145
|
if @@delegated_methods.include?( sym )
|
148
|
-
|
146
|
+
node( args.first ).send( sym )
|
149
147
|
else
|
150
148
|
super
|
151
149
|
end
|
152
150
|
end
|
153
151
|
|
152
|
+
def node( nodename ); MockFileSystem.instance.node( nodename ); end
|
153
|
+
|
154
154
|
def respond_to?( sym )
|
155
155
|
@@delegated_methods.include?( sym ) ? true : super
|
156
156
|
end
|
@@ -160,32 +160,69 @@ module MockFS
|
|
160
160
|
extend Adapter
|
161
161
|
include Adapter
|
162
162
|
|
163
|
+
def self.[]( string ); glob( string, 0 ); end
|
164
|
+
|
165
|
+
def self.glob( string, flags = 0 )
|
166
|
+
self.new( '.' ).send( :glob, string, flags )
|
167
|
+
end
|
168
|
+
|
163
169
|
def self.mkdir( dirname )
|
164
170
|
path = Path.new( dirname ).absolute
|
165
|
-
|
171
|
+
node( path.parent ).mkdir( path.node )
|
172
|
+
end
|
173
|
+
|
174
|
+
def self.rmdir( dirname )
|
175
|
+
path = Path.new( dirname ).absolute
|
176
|
+
node( path ).delete
|
166
177
|
end
|
167
178
|
|
168
179
|
def initialize( dirname )
|
169
|
-
unless
|
180
|
+
unless node( dirname ).class == MockFileSystem::MockDir
|
170
181
|
raise Errno::ENOTDIR
|
171
182
|
end
|
172
183
|
@dirname = dirname
|
173
184
|
end
|
174
185
|
|
175
186
|
def entries; self.class.entries( @dirname ); end
|
187
|
+
|
188
|
+
private
|
189
|
+
|
190
|
+
def glob( string, flags = 0 )
|
191
|
+
path = Path.new string
|
192
|
+
string = path.node
|
193
|
+
string = string.gsub( /\./, '\.' )
|
194
|
+
string = string.gsub( /\?/, '.' )
|
195
|
+
string = string.gsub( /\*/, ".*" )
|
196
|
+
string = string.gsub( /\{(.+),(.+)\}/, '(\1|\2)' )
|
197
|
+
puts " #{ string }"
|
198
|
+
re = Regexp.new string
|
199
|
+
entries( path.parent ).select { |entry|
|
200
|
+
flags & File::FNM_DOTMATCH != 0 || !%w( . .. ).include?( entry )
|
201
|
+
}.select { |entry| entry =~ re }.map { |entry|
|
202
|
+
File.join( path.parent, entry )
|
203
|
+
}
|
204
|
+
end
|
176
205
|
end
|
177
206
|
|
178
207
|
class FileAdapter #:nodoc:
|
179
208
|
extend Adapter
|
180
209
|
|
181
210
|
def self.chmod( perms, *filenames )
|
182
|
-
|
211
|
+
node( filenames.first ).permissions = perms
|
212
|
+
end
|
213
|
+
|
214
|
+
def self.directory?( file_name )
|
215
|
+
begin
|
216
|
+
node( file_name ).is_a? MockFileSystem::MockDir
|
217
|
+
rescue Errno::ENOENT
|
218
|
+
false
|
219
|
+
end
|
183
220
|
end
|
184
221
|
|
185
222
|
class << self
|
186
223
|
def exist?( filename )
|
187
224
|
begin
|
188
|
-
|
225
|
+
node( filename )
|
189
226
|
true
|
190
227
|
rescue Errno::ENOENT
|
191
228
|
false
|
@@ -194,16 +231,24 @@ module MockFS
|
|
194
231
|
|
195
232
|
alias_method :exists?, :exist?
|
196
233
|
end
|
197
|
-
|
198
|
-
def self.
|
234
|
+
|
235
|
+
def self.file?( file_name )
|
236
|
+
begin
|
237
|
+
node( file_name ).is_a? MockFileSystem::MockFile
|
238
|
+
rescue Errno::ENOENT
|
239
|
+
false
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def self.mock_file( fd, mode ) #:nodoc:
|
199
244
|
if mode.read_only?
|
200
|
-
mock_file =
|
245
|
+
mock_file = node( fd )
|
201
246
|
raise Errno::EACCES if mock_file and !mock_file.readable?
|
202
247
|
else
|
203
248
|
path = Path.new( fd ).absolute
|
204
|
-
dir =
|
249
|
+
dir = node( path.parent )
|
205
250
|
if mode.append?
|
206
|
-
mock_file =
|
251
|
+
mock_file = node( fd )
|
207
252
|
else
|
208
253
|
mock_file = MockFileSystem::MockFile.new( dir, path.node, '' )
|
209
254
|
end
|
@@ -213,7 +258,7 @@ module MockFS
|
|
213
258
|
|
214
259
|
def self.open( fd, mode_string = File::RDONLY, &action )
|
215
260
|
mode = Mode.new mode_string
|
216
|
-
mock_file =
|
261
|
+
mock_file = mock_file( fd, mode )
|
217
262
|
mock_file.pos = mock_file.size if mode.append?
|
218
263
|
if action
|
219
264
|
result = action.call( mock_file )
|
@@ -227,11 +272,21 @@ module MockFS
|
|
227
272
|
end
|
228
273
|
end
|
229
274
|
|
275
|
+
def self.read( name )
|
276
|
+
mfile = node name
|
277
|
+
raise Errno::EISDIR if mfile.is_a? MockFileSystem::MockDir
|
278
|
+
mfile.read
|
279
|
+
end
|
280
|
+
|
230
281
|
class Mode
|
231
282
|
def initialize( string_or_bitwise )
|
232
283
|
if string_or_bitwise.is_a?( String )
|
233
284
|
if string_or_bitwise == 'w'
|
234
285
|
@bitwise = File::WRONLY
|
286
|
+
elsif string_or_bitwise == 'r'
|
287
|
+
@bitwise = File::RDONLY
|
288
|
+
elsif string_or_bitwise == 'a'
|
289
|
+
@bitwise = File::APPEND
|
235
290
|
end
|
236
291
|
else
|
237
292
|
@bitwise = string_or_bitwise
|
@@ -248,15 +303,15 @@ module MockFS
|
|
248
303
|
extend Adapter
|
249
304
|
|
250
305
|
def self.cp( src, dest, options = {} )
|
251
|
-
file =
|
306
|
+
file = node( src ).clone
|
252
307
|
dest_path = Path.new( dest ).absolute
|
253
308
|
if MockFS.file.exist?( dest )
|
254
|
-
maybe_dest_dir =
|
309
|
+
maybe_dest_dir = node dest_path
|
255
310
|
if maybe_dest_dir.is_a? MockFileSystem::MockDir
|
256
311
|
dest_path << ( '/' + Path.new( src ).absolute.node )
|
257
312
|
end
|
258
313
|
end
|
259
|
-
dest_dir =
|
314
|
+
dest_dir = node dest_path.parent
|
260
315
|
dest_dir[dest_path.node] = file
|
261
316
|
file.name = dest_path.node
|
262
317
|
file.parent = dest_dir
|
@@ -273,10 +328,10 @@ module MockFS
|
|
273
328
|
|
274
329
|
def self.touch( file )
|
275
330
|
begin
|
276
|
-
|
331
|
+
node( file ).mtime = Time.now
|
277
332
|
rescue Errno::ENOENT
|
278
333
|
path = Path.new( file ).absolute
|
279
|
-
parent_dir =
|
334
|
+
parent_dir = node( path.parent )
|
280
335
|
file = MockFileSystem::MockFile.new( parent_dir, path.node, nil )
|
281
336
|
parent_dir[path.node] = file
|
282
337
|
end
|
@@ -297,10 +352,6 @@ module MockFS
|
|
297
352
|
# Flushes all file information out of the MockFileSystem.
|
298
353
|
def flush; @root = MockRoot.new( '' ); fill_path( '.' ); end
|
299
354
|
|
300
|
-
def get_node( dirname ) #:nodoc:
|
301
|
-
@root.get_node( dirname )
|
302
|
-
end
|
303
|
-
|
304
355
|
# Use this method to fill in directory paths. This is the same as calling mkdir a bunch of times.
|
305
356
|
#
|
306
357
|
# MockFS.mock_file_system.fill_path '/usr/local/bin'
|
@@ -309,6 +360,10 @@ module MockFS
|
|
309
360
|
@root.fill_path( Path.new( dirname ).absolute.strip )
|
310
361
|
end
|
311
362
|
|
363
|
+
def node( dirname ) #:nodoc:
|
364
|
+
@root.node( dirname )
|
365
|
+
end
|
366
|
+
|
312
367
|
module Node #:nodoc:
|
313
368
|
attr_accessor :mtime, :name, :parent, :permissions
|
314
369
|
|
@@ -337,21 +392,6 @@ module MockFS
|
|
337
392
|
|
338
393
|
def entries; %w( . .. ).concat( keys ); end
|
339
394
|
|
340
|
-
def get_node( dirname )
|
341
|
-
if dirname.first == '..'
|
342
|
-
self.parent.get_node( dirname[1..-1] )
|
343
|
-
elsif dirname.first == '.'
|
344
|
-
self.get_node( dirname[1..-1] )
|
345
|
-
elsif dirname.size > 1
|
346
|
-
subdir = self[dirname.first]
|
347
|
-
subdir ? subdir.get_node( dirname[1..-1] ) : ( raise Errno::ENOENT )
|
348
|
-
elsif dirname == ''
|
349
|
-
self
|
350
|
-
else
|
351
|
-
self[dirname.strip] or ( raise Errno::ENOENT )
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
395
|
def fill_dir( dirname )
|
356
396
|
dir = self[dirname]
|
357
397
|
if dir.nil?
|
@@ -376,6 +416,21 @@ module MockFS
|
|
376
416
|
end
|
377
417
|
|
378
418
|
def mkdir( dirname ); fill_path( Path.new( dirname ) ); end
|
419
|
+
|
420
|
+
def node( dirname )
|
421
|
+
if dirname.first == '..'
|
422
|
+
self.parent.node( dirname[1..-1] )
|
423
|
+
elsif dirname.first == '.'
|
424
|
+
self.node( dirname[1..-1] )
|
425
|
+
elsif dirname.size > 1
|
426
|
+
subdir = self[dirname.first]
|
427
|
+
subdir ? subdir.node( dirname[1..-1] ) : ( raise Errno::ENOENT )
|
428
|
+
elsif dirname == ''
|
429
|
+
self
|
430
|
+
else
|
431
|
+
self[dirname.strip] or ( raise Errno::ENOENT )
|
432
|
+
end
|
433
|
+
end
|
379
434
|
end
|
380
435
|
|
381
436
|
class MockFile < DelegateClass( StringIO ) #:nodoc:
|
@@ -407,7 +462,7 @@ module MockFS
|
|
407
462
|
end
|
408
463
|
|
409
464
|
class MockRoot < MockDir #:nodoc:
|
410
|
-
def
|
465
|
+
def node( dirname )
|
411
466
|
begin
|
412
467
|
super( Path.new( dirname ).absolute.strip )
|
413
468
|
rescue Errno::ENOENT
|
@@ -440,14 +495,14 @@ module MockFS
|
|
440
495
|
|
441
496
|
def first; self.split( "/" ).first; end
|
442
497
|
|
443
|
-
def
|
498
|
+
def node
|
444
499
|
self =~ %r{^(.*)/(.*?)$}
|
445
|
-
$
|
500
|
+
$2
|
446
501
|
end
|
447
502
|
|
448
|
-
def
|
503
|
+
def parent
|
449
504
|
self =~ %r{^(.*)/(.*?)$}
|
450
|
-
$
|
505
|
+
$1
|
451
506
|
end
|
452
507
|
|
453
508
|
def size; self.split( '/' ).size; end
|
metadata
CHANGED
@@ -1,53 +1,58 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.11
|
3
3
|
specification_version: 1
|
4
4
|
name: mockfs
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date:
|
6
|
+
version: 0.1.3
|
7
|
+
date: 2006-06-11 00:00:00 -04:00
|
8
8
|
summary: MockFS is a test-obsessed library for mocking out the entire file system.
|
9
9
|
require_paths:
|
10
|
-
|
10
|
+
- lib
|
11
11
|
email: sera@fhwang.net
|
12
12
|
homepage: http://mockfs.rubyforge.org/
|
13
13
|
rubyforge_project:
|
14
|
-
description:
|
15
|
-
provides mock objects that clone the functionality of File, FileUtils, Dir, and
|
16
|
-
other in-Ruby file-access libraries."
|
14
|
+
description: MockFS is a test-obsessed library for mocking out the entire file system. It provides mock objects that clone the functionality of File, FileUtils, Dir, and other in-Ruby file-access libraries.
|
17
15
|
autorequire: mockfs
|
18
16
|
default_executable:
|
19
17
|
bindir: bin
|
20
|
-
has_rdoc:
|
18
|
+
has_rdoc: true
|
21
19
|
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
22
20
|
requirements:
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
version: 0.0.0
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
27
24
|
version:
|
28
25
|
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
29
28
|
authors:
|
30
|
-
|
29
|
+
- Francis Hwang
|
31
30
|
files:
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
- lib/mockfs
|
32
|
+
- lib/mockfs.rb
|
33
|
+
- lib/mockfs.rb~
|
34
|
+
- lib/mockfs/override.rb
|
35
|
+
- lib/mockfs/override.rb~
|
37
36
|
test_files: []
|
38
|
-
|
37
|
+
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- lib/mockfs.rb
|
39
41
|
extra_rdoc_files: []
|
42
|
+
|
40
43
|
executables: []
|
44
|
+
|
41
45
|
extensions: []
|
46
|
+
|
42
47
|
requirements: []
|
48
|
+
|
43
49
|
dependencies:
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
version:
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: extensions
|
52
|
+
version_requirement:
|
53
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 0.0.0
|
58
|
+
version:
|