fakefs 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,31 @@
1
1
  module FakeFS
2
+ # Kernel Module
2
3
  module Kernel
4
+ @captives = { original: {}, hijacked: {} }
3
5
 
4
- @captives = { :original => {}, :hijacked => {}}
5
6
  class << self
6
7
  attr_accessor :captives
7
8
  end
8
9
 
9
10
  def self.hijack!
10
- captives[:hijacked].each do |name,prc|
11
+ captives[:hijacked].each do |name, prc|
11
12
  ::Kernel.send(:define_method, name.to_sym, &prc)
12
13
  end
13
14
  end
14
15
 
15
16
  def self.unhijack!
16
- captives[:original].each do |name,prc|
17
- ::Kernel.send(:define_method, name.to_sym, Proc.new do |*args, &block|
17
+ captives[:original].each do |name, _prc|
18
+ ::Kernel.send(:define_method, name.to_sym, proc do |*args, &block|
18
19
  ::FakeFS::Kernel.captives[:original][name].call(*args, &block)
19
20
  end)
20
21
  end
21
22
  end
22
23
 
23
24
  private
24
- def self.hijack name, &block
25
+
26
+ def self.hijack(name, &block)
25
27
  captives[:original][name] = ::Kernel.method(name.to_sym)
26
- captives[:hijacked][name] = block || Proc.new { |args| }
28
+ captives[:hijacked][name] = block || proc { |_args| }
27
29
  end
28
30
 
29
31
  hijack :open do |*args, &block|
@@ -35,6 +37,5 @@ module FakeFS
35
37
  FakeFS::File.open(name, *args, &block)
36
38
  end
37
39
  end
38
-
39
40
  end
40
41
  end
@@ -1,6 +1,7 @@
1
+ # FakeFS module
1
2
  module FakeFS
2
- if RUBY_VERSION >= "1.9.3"
3
-
3
+ if RUBY_VERSION >= '1.9.3'
4
+
4
5
  #
5
6
  # = pathname.rb - From MRI 1.9.2
6
7
  #
@@ -12,52 +13,68 @@ module FakeFS
12
13
  # For documentation, see class Pathname.
13
14
  #
14
15
  class Pathname
15
-
16
- # to_path is implemented so Pathname objects are usable with File.open, etc.
16
+ # to_path is implemented so Pathname objects are
17
+ # usable with File.open, etc.
17
18
  TO_PATH = :to_path
18
19
 
19
20
  SAME_PATHS = if File::FNM_SYSCASE.nonzero?
20
- proc {|a, b| a.casecmp(b).zero?}
21
- else
22
- proc {|a, b| a == b}
23
- end
21
+ proc { |a, b| a.casecmp(b).zero? }
22
+ else
23
+ proc { |a, b| a == b }
24
+ end
24
25
 
25
26
  # :startdoc:
26
27
 
27
28
  #
28
29
  # Create a Pathname object from the given String (or String-like object).
29
- # If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
30
+ # If +path+ contains a NUL character (<tt>\0</tt>),
31
+ # an ArgumentError is raised.
30
32
  #
31
33
  def initialize(path)
32
34
  path = path.__send__(TO_PATH) if path.respond_to? TO_PATH
33
35
  @path = path.dup
34
36
 
35
37
  if /\0/ =~ @path
36
- raise ArgumentError, "pathname contains \\0: #{@path.inspect}"
38
+ fail ArgumentError, "pathname contains \\0: #{@path.inspect}"
37
39
  end
38
40
 
39
- self.taint if @path.tainted?
41
+ taint if @path.tainted?
42
+ end
43
+
44
+ def freeze
45
+ super
46
+ @path.freeze
47
+ self
40
48
  end
41
49
 
42
- def freeze() super; @path.freeze; self end
43
- def taint() super; @path.taint; self end
44
- def untaint() super; @path.untaint; self end
50
+ def taint
51
+ super
52
+ @path.taint
53
+ self
54
+ end
55
+
56
+ def untaint
57
+ super
58
+ @path.untaint
59
+ self
60
+ end
45
61
 
46
62
  #
47
63
  # Compare this pathname with +other+. The comparison is string-based.
48
- # Be aware that two different paths (<tt>foo.txt</tt> and <tt>./foo.txt</tt>)
49
- # can refer to the same file.
64
+ # Be aware that two different paths
65
+ # (<tt>foo.txt</tt> and <tt>./foo.txt</tt>) can refer to the same file.
50
66
  #
51
67
  def ==(other)
52
- return false unless Pathname === other
68
+ return false unless other.is_a?(Pathname)
53
69
  other.to_s == @path
54
70
  end
55
- alias === ==
56
- alias eql? ==
71
+
72
+ alias_method :===, :==
73
+ alias_method :eql?, :==
57
74
 
58
75
  # Provides for comparing pathnames, case-sensitively.
59
76
  def <=>(other)
60
- return nil unless Pathname === other
77
+ return nil unless other.is_a?(Pathname)
61
78
  @path.tr('/', "\0") <=> other.to_s.tr('/', "\0")
62
79
  end
63
80
 
@@ -70,7 +87,8 @@ module FakeFS
70
87
  @path.dup
71
88
  end
72
89
 
73
- # to_path is implemented so Pathname objects are usable with File.open, etc.
90
+ # to_path is implemented so Pathname objects are usable
91
+ # with File.open, etc.
74
92
  alias_method TO_PATH, :to_s
75
93
 
76
94
  def inspect # :nodoc:
@@ -80,16 +98,17 @@ module FakeFS
80
98
  # Return a pathname which is substituted by String#sub.
81
99
  def sub(pattern, *rest, &block)
82
100
  if block
83
- path = @path.sub(pattern, *rest) {|*args|
101
+ path = @path.sub(pattern, *rest) do |*args|
84
102
  begin
85
103
  old = Thread.current[:pathname_sub_matchdata]
86
- Thread.current[:pathname_sub_matchdata] = $~
87
- eval("$~ = Thread.current[:pathname_sub_matchdata]", block.binding)
104
+ Thread.current[:pathname_sub_matchdata] = $LAST_MATCH_INFO
105
+ eval('$~ = Thread.current[:pathname_sub_matchdata]',
106
+ block.binding)
88
107
  ensure
89
108
  Thread.current[:pathname_sub_matchdata] = old
90
109
  end
91
110
  yield(*args)
92
- }
111
+ end
93
112
  else
94
113
  path = @path.sub(pattern, *rest)
95
114
  end
@@ -97,7 +116,8 @@ module FakeFS
97
116
  end
98
117
 
99
118
  if File::ALT_SEPARATOR
100
- SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
119
+ SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}" \
120
+ "#{Regexp.quote File::SEPARATOR}"
101
121
  SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
102
122
  else
103
123
  SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
@@ -127,12 +147,14 @@ module FakeFS
127
147
  # split_names(path) -> prefix, [name, ...]
128
148
  def split_names(path)
129
149
  names = []
130
- while r = chop_basename(path)
150
+ while (r = chop_basename(path))
131
151
  path, basename = r
132
152
  names.unshift basename
133
153
  end
134
- return path, names
154
+
155
+ [path, names]
135
156
  end
157
+
136
158
  private :split_names
137
159
 
138
160
  def prepend_prefix(prefix, relpath)
@@ -140,7 +162,7 @@ module FakeFS
140
162
  File.dirname(prefix)
141
163
  elsif /#{SEPARATOR_PAT}/o =~ prefix
142
164
  prefix = File.dirname(prefix)
143
- prefix = File.join(prefix, "") if File.basename(prefix + 'a') != 'a'
165
+ prefix = File.join(prefix, '') if File.basename(prefix + 'a') != 'a'
144
166
  prefix + relpath
145
167
  else
146
168
  prefix + relpath
@@ -148,15 +170,16 @@ module FakeFS
148
170
  end
149
171
  private :prepend_prefix
150
172
 
151
- # Returns clean pathname of +self+ with consecutive slashes and useless dots
152
- # removed. The filesystem is not accessed.
173
+ # Returns clean pathname of +self+ with consecutive slashes and
174
+ # useless dots removed. The filesystem is not accessed.
153
175
  #
154
- # If +consider_symlink+ is +true+, then a more conservative algorithm is used
155
- # to avoid breaking symbolic linkages. This may retain more <tt>..</tt>
156
- # entries than absolutely necessary, but without accessing the filesystem,
157
- # this can't be avoided. See #realpath.
176
+ # If +consider_symlink+ is +true+, then a more conservative algorithm
177
+ # is used to avoid breaking symbolic linkages.
178
+ # This may retain more <tt>..</tt> entries than absolutely necessary,
179
+ # but without accessing the filesystem, this can't be avoided.
180
+ # See #realpath.
158
181
  #
159
- def cleanpath(consider_symlink=false)
182
+ def cleanpath(consider_symlink = false)
160
183
  if consider_symlink
161
184
  cleanpath_conservative
162
185
  else
@@ -165,14 +188,15 @@ module FakeFS
165
188
  end
166
189
 
167
190
  #
168
- # Clean the path simply by resolving and removing excess "." and ".." entries.
191
+ # Clean the path simply by resolving and removing excess
192
+ # "." and ".." entries.
169
193
  # Nothing more, nothing less.
170
194
  #
171
195
  def cleanpath_aggressive
172
196
  path = @path
173
197
  names = []
174
198
  pre = path
175
- while r = chop_basename(pre)
199
+ while (r = chop_basename(pre))
176
200
  pre, base = r
177
201
  case base
178
202
  when '.'
@@ -193,29 +217,31 @@ module FakeFS
193
217
  end
194
218
  private :cleanpath_aggressive
195
219
 
196
- # has_trailing_separator?(path) -> bool
197
- def has_trailing_separator?(path)
198
- if r = chop_basename(path)
220
+ # trailing_separator?(path) -> bool
221
+ def trailing_separator?(path)
222
+ if (r = chop_basename(path))
199
223
  pre, basename = r
200
224
  pre.length + basename.length < path.length
201
225
  else
202
226
  false
203
227
  end
204
228
  end
205
- private :has_trailing_separator?
229
+
230
+ private :trailing_separator?
206
231
 
207
232
  # add_trailing_separator(path) -> path
208
233
  def add_trailing_separator(path)
209
234
  if File.basename(path + 'a') == 'a'
210
235
  path
211
236
  else
212
- File.join(path, "") # xxx: Is File.join is appropriate to add separator?
237
+ # xxx: Is File.join is appropriate to add separator?
238
+ File.join(path, '')
213
239
  end
214
240
  end
215
241
  private :add_trailing_separator
216
242
 
217
243
  def del_trailing_separator(path)
218
- if r = chop_basename(path)
244
+ if (r = chop_basename(path))
219
245
  pre, basename = r
220
246
  pre + basename
221
247
  elsif /#{SEPARATOR_PAT}+\z/o =~ path
@@ -230,7 +256,7 @@ module FakeFS
230
256
  path = @path
231
257
  names = []
232
258
  pre = path
233
- while r = chop_basename(pre)
259
+ while (r = chop_basename(pre))
234
260
  pre, base = r
235
261
  names.unshift base if base != '.'
236
262
  end
@@ -240,11 +266,10 @@ module FakeFS
240
266
  if names.empty?
241
267
  self.class.new(File.dirname(pre))
242
268
  else
243
- if names.last != '..' && File.basename(path) == '.'
244
- names << '.'
245
- end
269
+ names << '.' if names.last != '..' && File.basename(path) == '.'
270
+
246
271
  result = prepend_prefix(pre, File.join(*names))
247
- if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
272
+ if /\A(?:\.|\.\.)\z/ !~ names.last && trailing_separator?(path)
248
273
  self.class.new(add_trailing_separator(result))
249
274
  else
250
275
  self.class.new(result)
@@ -260,7 +285,7 @@ module FakeFS
260
285
  # All components of the pathname must exist when this method is
261
286
  # called.
262
287
  #
263
- def realpath(basedir=nil)
288
+ def realpath(basedir = nil)
264
289
  self.class.new(File.realpath(@path, basedir))
265
290
  end
266
291
 
@@ -270,7 +295,7 @@ module FakeFS
270
295
  #
271
296
  # The last component of the real pathname can be nonexistent.
272
297
  #
273
- def realdirpath(basedir=nil)
298
+ def realdirpath(basedir = nil)
274
299
  self.class.new(File.realdirpath(@path, basedir))
275
300
  end
276
301
 
@@ -283,9 +308,9 @@ module FakeFS
283
308
 
284
309
  # #mountpoint? returns +true+ if <tt>self</tt> points to a mountpoint.
285
310
  def mountpoint?
311
+ stat1 = lstat
286
312
  begin
287
- stat1 = self.lstat
288
- stat2 = self.parent.lstat
313
+ stat2 = parent.lstat
289
314
  stat1.dev == stat2.dev && stat1.ino == stat2.ino ||
290
315
  stat1.dev != stat2.dev
291
316
  rescue Errno::ENOENT
@@ -294,14 +319,15 @@ module FakeFS
294
319
  end
295
320
 
296
321
  #
297
- # #root? is a predicate for root directories. I.e. it returns +true+ if the
322
+ # #root? is a predicate for root directories.
323
+ # I.e. it returns +true+ if the
298
324
  # pathname consists of consecutive slashes.
299
325
  #
300
326
  # It doesn't access actual filesystem. So it may return +false+ for some
301
327
  # pathnames which points to roots such as <tt>/usr/..</tt>.
302
328
  #
303
329
  def root?
304
- !!(chop_basename(@path) == nil && /#{SEPARATOR_PAT}/o =~ @path)
330
+ !(chop_basename(@path).nil? && /#{SEPARATOR_PAT}/o =~ @path).nil?
305
331
  end
306
332
 
307
333
  # Predicate method for testing whether a path is absolute.
@@ -313,8 +339,8 @@ module FakeFS
313
339
  # The opposite of #absolute?
314
340
  def relative?
315
341
  path = @path
316
- while r = chop_basename(path)
317
- path, basename = r
342
+ while (r = chop_basename(path))
343
+ path, _basename = r
318
344
  end
319
345
  path == ''
320
346
  end
@@ -322,27 +348,27 @@ module FakeFS
322
348
  #
323
349
  # Iterates over each component of the path.
324
350
  #
325
- # Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
351
+ # Pathname.new("/usr/bin/ruby").each_filename { |filename| ... }
326
352
  # # yields "usr", "bin", and "ruby".
327
353
  #
328
354
  def each_filename # :yield: filename
329
355
  return to_enum(__method__) unless block_given?
330
- prefix, names = split_names(@path)
331
- names.each {|filename| yield filename }
356
+ _prefix, names = split_names(@path)
357
+ names.each { |filename| yield filename }
332
358
  nil
333
359
  end
334
360
 
335
361
  # Iterates over and yields a new Pathname object
336
362
  # for each element in the given path in descending order.
337
363
  #
338
- # Pathname.new('/path/to/some/file.rb').descend {|v| p v}
364
+ # Pathname.new('/path/to/some/file.rb').descend { |v| p v}
339
365
  # #<Pathname:/>
340
366
  # #<Pathname:/path>
341
367
  # #<Pathname:/path/to>
342
368
  # #<Pathname:/path/to/some>
343
369
  # #<Pathname:/path/to/some/file.rb>
344
370
  #
345
- # Pathname.new('path/to/some/file.rb').descend {|v| p v}
371
+ # Pathname.new('path/to/some/file.rb').descend { |v| p v}
346
372
  # #<Pathname:path>
347
373
  # #<Pathname:path/to>
348
374
  # #<Pathname:path/to/some>
@@ -354,22 +380,22 @@ module FakeFS
354
380
  #
355
381
  def descend
356
382
  vs = []
357
- ascend {|v| vs << v }
358
- vs.reverse_each {|v| yield v }
383
+ ascend { |v| vs << v }
384
+ vs.reverse_each { |v| yield v }
359
385
  nil
360
386
  end
361
387
 
362
388
  # Iterates over and yields a new Pathname object
363
389
  # for each element in the given path in ascending order.
364
390
  #
365
- # Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
391
+ # Pathname.new('/path/to/some/file.rb').ascend { |v| p v}
366
392
  # #<Pathname:/path/to/some/file.rb>
367
393
  # #<Pathname:/path/to/some>
368
394
  # #<Pathname:/path/to>
369
395
  # #<Pathname:/path>
370
396
  # #<Pathname:/>
371
397
  #
372
- # Pathname.new('path/to/some/file.rb').ascend {|v| p v}
398
+ # Pathname.new('path/to/some/file.rb').ascend { |v| p v}
373
399
  # #<Pathname:path/to/some/file.rb>
374
400
  # #<Pathname:path/to/some>
375
401
  # #<Pathname:path/to>
@@ -382,25 +408,27 @@ module FakeFS
382
408
  def ascend
383
409
  path = @path
384
410
  yield self
385
- while r = chop_basename(path)
386
- path, name = r
411
+ while (r = chop_basename(path))
412
+ path, _name = r
387
413
  break if path.empty?
388
414
  yield self.class.new(del_trailing_separator(path))
389
415
  end
390
416
  end
391
417
 
392
418
  #
393
- # Pathname#+ appends a pathname fragment to this one to produce a new Pathname
419
+ # Pathname#+ appends a pathname fragment to this one to produce a new
420
+ # Pathname
394
421
  # object.
395
422
  #
396
423
  # p1 = Pathname.new("/usr") # Pathname:/usr
397
424
  # p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
398
425
  # p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
399
426
  #
400
- # This method doesn't access the file system; it is pure string manipulation.
427
+ # This method doesn't access the file system; it is pure string
428
+ # manipulation.
401
429
  #
402
430
  def +(other)
403
- other = Pathname.new(other) unless Pathname === other
431
+ other = Pathname.new(other) unless other.is_a?(Pathname)
404
432
  Pathname.new(plus(@path, other.to_s))
405
433
  end
406
434
 
@@ -408,28 +436,33 @@ module FakeFS
408
436
  prefix2 = path2
409
437
  index_list2 = []
410
438
  basename_list2 = []
411
- while r2 = chop_basename(prefix2)
439
+ while (r2 = chop_basename(prefix2))
412
440
  prefix2, basename2 = r2
413
441
  index_list2.unshift prefix2.length
414
442
  basename_list2.unshift basename2
415
443
  end
444
+
416
445
  return path2 if prefix2 != ''
446
+
417
447
  prefix1 = path1
418
- while true
448
+ while (r1 = chop_basename(prefix1))
419
449
  while !basename_list2.empty? && basename_list2.first == '.'
420
450
  index_list2.shift
421
451
  basename_list2.shift
422
452
  end
423
- break unless r1 = chop_basename(prefix1)
453
+
424
454
  prefix1, basename1 = r1
425
455
  next if basename1 == '.'
426
- if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
427
- prefix1 = prefix1 + basename1
456
+ if basename1 == '..' ||
457
+ basename_list2.empty? ||
458
+ basename_list2.first != '..'
459
+ prefix1 += basename1
428
460
  break
429
461
  end
430
462
  index_list2.shift
431
463
  basename_list2.shift
432
464
  end
465
+
433
466
  r1 = chop_basename(prefix1)
434
467
  if !r1 && /#{SEPARATOR_PAT}/o =~ File.basename(prefix1)
435
468
  while !basename_list2.empty? && basename_list2.first == '..'
@@ -437,6 +470,7 @@ module FakeFS
437
470
  basename_list2.shift
438
471
  end
439
472
  end
473
+
440
474
  if !basename_list2.empty?
441
475
  suffix2 = path2[index_list2.first..-1]
442
476
  r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
@@ -455,13 +489,13 @@ module FakeFS
455
489
  def join(*args)
456
490
  args.unshift self
457
491
  result = args.pop
458
- result = Pathname.new(result) unless Pathname === result
492
+ result = Pathname.new(result) unless result.is_a?(Pathname)
459
493
  return result if result.absolute?
460
- args.reverse_each {|arg|
461
- arg = Pathname.new(arg) unless Pathname === arg
494
+ args.reverse_each do |arg|
495
+ arg = Pathname.new(arg) unless arg.is_a?(Pathname)
462
496
  result = arg + result
463
497
  return result if result.absolute?
464
- }
498
+ end
465
499
  result
466
500
  end
467
501
 
@@ -469,44 +503,50 @@ module FakeFS
469
503
  # Returns the children of the directory (files and subdirectories, not
470
504
  # recursive) as an array of Pathname objects. By default, the returned
471
505
  # pathnames will have enough information to access the files. If you set
472
- # +with_directory+ to +false+, then the returned pathnames will contain the
506
+ # +with_directory+ to +false+, then the returned
507
+ # pathnames will contain the
473
508
  # filename only.
474
509
  #
475
510
  # For example:
476
511
  # pn = Pathname("/usr/lib/ruby/1.8")
477
512
  # pn.children
478
- # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
479
- # Pathname:/usr/lib/ruby/1.8/Env.rb,
480
- # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
513
+ # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
514
+ # Pathname:/usr/lib/ruby/1.8/Env.rb,
515
+ # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
481
516
  # pn.children(false)
482
- # # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
517
+ # # -> [ Pathname:English.rb,
518
+ # Pathname:Env.rb,
519
+ # Pathname:abbrev.rb, ... ]
483
520
  #
484
- # Note that the result never contain the entries <tt>.</tt> and <tt>..</tt> in
521
+ # Note that the result never contain the entries
522
+ # <tt>.</tt> and <tt>..</tt> in
485
523
  # the directory because they are not children.
486
524
  #
487
525
  # This method has existed since 1.8.1.
488
526
  #
489
- def children(with_directory=true)
527
+ def children(with_directory = true)
490
528
  with_directory = false if @path == '.'
491
529
  result = []
492
- Dir.foreach(@path) {|e|
530
+ Dir.foreach(@path) do |e|
493
531
  next if e == '.' || e == '..'
494
532
  if with_directory
495
533
  result << self.class.new(File.join(@path, e))
496
534
  else
497
535
  result << self.class.new(e)
498
536
  end
499
- }
537
+ end
500
538
  result
501
539
  end
502
540
 
503
541
  # Iterates over the children of the directory
504
542
  # (files and subdirectories, not recursive).
505
543
  # It yields Pathname object for each child.
506
- # By default, the yielded pathnames will have enough information to access the files.
507
- # If you set +with_directory+ to +false+, then the returned pathnames will contain the filename only.
544
+ # By default, the yielded pathnames will have enough information to access
545
+ # the files.
546
+ # If you set +with_directory+ to +false+,
547
+ # then the returned pathnames will contain the filename only.
508
548
  #
509
- # Pathname("/usr/local").each_child {|f| p f }
549
+ # Pathname("/usr/local").each_child { |f| p f }
510
550
  # #=> #<Pathname:/usr/local/share>
511
551
  # # #<Pathname:/usr/local/bin>
512
552
  # # #<Pathname:/usr/local/games>
@@ -516,7 +556,7 @@ module FakeFS
516
556
  # # #<Pathname:/usr/local/src>
517
557
  # # #<Pathname:/usr/local/man>
518
558
  #
519
- # Pathname("/usr/local").each_child(false) {|f| p f }
559
+ # Pathname("/usr/local").each_child(false) { |f| p f }
520
560
  # #=> #<Pathname:share>
521
561
  # # #<Pathname:bin>
522
562
  # # #<Pathname:games>
@@ -526,38 +566,40 @@ module FakeFS
526
566
  # # #<Pathname:src>
527
567
  # # #<Pathname:man>
528
568
  #
529
- def each_child(with_directory=true, &b)
569
+ def each_child(with_directory = true, &b)
530
570
  children(with_directory).each(&b)
531
571
  end
532
572
 
533
573
  #
534
574
  # #relative_path_from returns a relative path from the argument to the
535
- # receiver. If +self+ is absolute, the argument must be absolute too. If
575
+ # receiver. If +self+ is absolute, the argument must be absolute too. If
536
576
  # +self+ is relative, the argument must be relative too.
537
577
  #
538
- # #relative_path_from doesn't access the filesystem. It assumes no symlinks.
578
+ # #relative_path_from doesn't access the filesystem.
579
+ # It assumes no symlinks.
539
580
  #
540
581
  # ArgumentError is raised when it cannot find a relative path.
541
582
  #
542
583
  # This method has existed since 1.8.1.
543
584
  #
544
585
  def relative_path_from(base_directory)
545
- dest_directory = self.cleanpath.to_s
586
+ dest_directory = cleanpath.to_s
546
587
  base_directory = base_directory.cleanpath.to_s
547
588
  dest_prefix = dest_directory
548
589
  dest_names = []
549
- while r = chop_basename(dest_prefix)
590
+ while (r = chop_basename(dest_prefix)) == true
550
591
  dest_prefix, basename = r
551
592
  dest_names.unshift basename if basename != '.'
552
593
  end
553
594
  base_prefix = base_directory
554
595
  base_names = []
555
- while r = chop_basename(base_prefix)
596
+ while (r = chop_basename(base_prefix)) == true
556
597
  base_prefix, basename = r
557
598
  base_names.unshift basename if basename != '.'
558
599
  end
559
600
  unless SAME_PATHS[dest_prefix, base_prefix]
560
- raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
601
+ fail ArgumentError, "different prefix: #{dest_prefix.inspect} " \
602
+ "and #{base_directory.inspect}"
561
603
  end
562
604
  while !dest_names.empty? &&
563
605
  !base_names.empty? &&
@@ -566,7 +608,7 @@ module FakeFS
566
608
  base_names.shift
567
609
  end
568
610
  if base_names.include? '..'
569
- raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
611
+ fail ArgumentError, "base_directory has ..: #{base_directory.inspect}"
570
612
  end
571
613
  base_names.fill('..')
572
614
  relpath_names = base_names + dest_names
@@ -578,10 +620,11 @@ module FakeFS
578
620
  end
579
621
  end
580
622
 
623
+ # Pathname class
581
624
  class Pathname # * IO *
582
625
  #
583
- # #each_line iterates over the line in the file. It yields a String object
584
- # for each line.
626
+ # #each_line iterates over the line in the file.
627
+ # It yields a String object for each line.
585
628
  #
586
629
  # This method has existed since 1.8.1.
587
630
  #
@@ -589,58 +632,88 @@ module FakeFS
589
632
  IO.foreach(@path, *args, &block)
590
633
  end
591
634
 
592
- # See <tt>IO.read</tt>. Returns all data from the file, or the first +N+ bytes
593
- # if specified.
594
- def read(*args) IO.read(@path, *args) end
635
+ # See <tt>IO.read</tt>. Returns all data from the file,
636
+ # or the first +N+ bytes if specified.
637
+ def read(*args)
638
+ IO.read(@path, *args)
639
+ end
595
640
 
596
- # See <tt>IO.binread</tt>. Returns all the bytes from the file, or the first +N+
597
- # if specified.
598
- def binread(*args) IO.binread(@path, *args) end
641
+ # See <tt>IO.binread</tt>. Returns all the bytes from the file,
642
+ # or the first +N+ if specified.
643
+ def binread(*args)
644
+ IO.binread(@path, *args)
645
+ end
599
646
 
600
647
  # See <tt>IO.readlines</tt>. Returns all the lines from the file.
601
- def readlines(*args) IO.readlines(@path, *args) end
648
+ def readlines(*args)
649
+ IO.readlines(@path, *args)
650
+ end
602
651
 
603
652
  # See <tt>IO.sysopen</tt>.
604
- def sysopen(*args) IO.sysopen(@path, *args) end
653
+ def sysopen(*args)
654
+ IO.sysopen(@path, *args)
655
+ end
605
656
  end
606
657
 
607
-
658
+ # Pathnam class
608
659
  class Pathname # * File *
609
-
610
660
  # See <tt>File.atime</tt>. Returns last access time.
611
- def atime() File.atime(@path) end
661
+ def atime
662
+ File.atime(@path)
663
+ end
612
664
 
613
- # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
614
- def ctime() File.ctime(@path) end
665
+ # See <tt>File.ctime</tt>.
666
+ # Returns last (directory entry, not file) change time.
667
+ def ctime
668
+ File.ctime(@path)
669
+ end
615
670
 
616
671
  # See <tt>File.mtime</tt>. Returns last modification time.
617
- def mtime() File.mtime(@path) end
672
+ def mtime
673
+ File.mtime(@path)
674
+ end
618
675
 
619
676
  # See <tt>File.chmod</tt>. Changes permissions.
620
- def chmod(mode) File.chmod(mode, @path) end
677
+ def chmod(mode)
678
+ File.chmod(mode, @path)
679
+ end
621
680
 
622
681
  # See <tt>File.lchmod</tt>.
623
- def lchmod(mode) File.lchmod(mode, @path) end
682
+ def lchmod(mode)
683
+ File.lchmod(mode, @path)
684
+ end
624
685
 
625
686
  # See <tt>File.chown</tt>. Change owner and group of file.
626
- def chown(owner, group) File.chown(owner, group, @path) end
687
+ def chown(owner, group)
688
+ File.chown(owner, group, @path)
689
+ end
627
690
 
628
691
  # See <tt>File.lchown</tt>.
629
- def lchown(owner, group) File.lchown(owner, group, @path) end
692
+ def lchown(owner, group)
693
+ File.lchown(owner, group, @path)
694
+ end
630
695
 
631
- # See <tt>File.fnmatch</tt>. Return +true+ if the receiver matches the given
632
- # pattern.
633
- def fnmatch(pattern, *args) File.fnmatch(pattern, @path, *args) end
696
+ # See <tt>File.fnmatch</tt>. Return +true+
697
+ # if the receiver matches the given pattern
698
+ def fnmatch(pattern, *args)
699
+ File.fnmatch(pattern, @path, *args)
700
+ end
634
701
 
635
702
  # See <tt>File.fnmatch?</tt> (same as #fnmatch).
636
- def fnmatch?(pattern, *args) File.fnmatch?(pattern, @path, *args) end
703
+ def fnmatch?(pattern, *args)
704
+ File.fnmatch?(pattern, @path, *args)
705
+ end
637
706
 
638
707
  # See <tt>File.ftype</tt>. Returns "type" of file ("file", "directory",
639
708
  # etc).
640
- def ftype() File.ftype(@path) end
709
+ def ftype
710
+ File.ftype(@path)
711
+ end
641
712
 
642
713
  # See <tt>File.link</tt>. Creates a hard link.
643
- def make_link(old) File.link(old, @path) end
714
+ def make_link(old)
715
+ File.link(old, @path)
716
+ end
644
717
 
645
718
  # See <tt>File.open</tt>. Opens the file for reading or writing.
646
719
  def open(*args, &block) # :yield: file
@@ -648,151 +721,233 @@ module FakeFS
648
721
  end
649
722
 
650
723
  # See <tt>File.readlink</tt>. Read symbolic link.
651
- def readlink() self.class.new(File.readlink(@path)) end
724
+ def readlink
725
+ self.class.new(File.readlink(@path))
726
+ end
652
727
 
653
728
  # See <tt>File.rename</tt>. Rename the file.
654
- def rename(to) File.rename(@path, to) end
729
+ def rename(to)
730
+ File.rename(@path, to)
731
+ end
655
732
 
656
733
  # See <tt>File.stat</tt>. Returns a <tt>File::Stat</tt> object.
657
- def stat() File.stat(@path) end
734
+ def stat
735
+ File.stat(@path)
736
+ end
658
737
 
659
738
  # See <tt>File.lstat</tt>.
660
- def lstat() File.lstat(@path) end
739
+ def lstat
740
+ File.lstat(@path)
741
+ end
661
742
 
662
743
  # See <tt>File.symlink</tt>. Creates a symbolic link.
663
- def make_symlink(old) File.symlink(old, @path) end
744
+ def make_symlink(old)
745
+ File.symlink(old, @path)
746
+ end
664
747
 
665
748
  # See <tt>File.truncate</tt>. Truncate the file to +length+ bytes.
666
- def truncate(length) File.truncate(@path, length) end
749
+ def truncate(length)
750
+ File.truncate(@path, length)
751
+ end
667
752
 
668
753
  # See <tt>File.utime</tt>. Update the access and modification times.
669
- def utime(atime, mtime) File.utime(atime, mtime, @path) end
754
+ def utime(atime, mtime)
755
+ File.utime(atime, mtime, @path)
756
+ end
670
757
 
671
758
  # See <tt>File.basename</tt>. Returns the last component of the path.
672
- def basename(*args) self.class.new(File.basename(@path, *args)) end
759
+ def basename(*args)
760
+ self.class.new(File.basename(@path, *args))
761
+ end
673
762
 
674
- # See <tt>File.dirname</tt>. Returns all but the last component of the path.
675
- def dirname() self.class.new(File.dirname(@path)) end
763
+ # See <tt>File.dirname</tt>. Returns all but the last
764
+ # component of the path.
765
+ def dirname
766
+ self.class.new(File.dirname(@path))
767
+ end
676
768
 
677
769
  # See <tt>File.extname</tt>. Returns the file's extension.
678
- def extname() File.extname(@path) end
770
+ def extname
771
+ File.extname(@path)
772
+ end
679
773
 
680
774
  # See <tt>File.expand_path</tt>.
681
- def expand_path(*args) self.class.new(File.expand_path(@path, *args)) end
775
+ def expand_path(*args)
776
+ self.class.new(File.expand_path(@path, *args))
777
+ end
682
778
 
683
779
  # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
684
780
  # Array.
685
- def split() File.split(@path).map {|f| self.class.new(f) } end
781
+ def split
782
+ File.split(@path).map { |f| self.class.new(f) }
783
+ end
686
784
  end
687
785
 
688
-
786
+ # Pathname class
689
787
  class Pathname # * FileTest *
690
-
691
788
  # See <tt>FileTest.blockdev?</tt>.
692
- def blockdev?() FileTest.blockdev?(@path) end
789
+ def blockdev?
790
+ FileTest.blockdev?(@path)
791
+ end
693
792
 
694
793
  # See <tt>FileTest.chardev?</tt>.
695
- def chardev?() FileTest.chardev?(@path) end
794
+ def chardev?
795
+ FileTest.chardev?(@path)
796
+ end
696
797
 
697
798
  # See <tt>FileTest.executable?</tt>.
698
- def executable?() FileTest.executable?(@path) end
799
+ def executable?
800
+ FileTest.executable?(@path)
801
+ end
699
802
 
700
803
  # See <tt>FileTest.executable_real?</tt>.
701
- def executable_real?() FileTest.executable_real?(@path) end
804
+ def executable_real?
805
+ FileTest.executable_real?(@path)
806
+ end
702
807
 
703
808
  # See <tt>FileTest.exist?</tt>.
704
- def exist?() FileTest.exist?(@path) end
809
+ def exist?
810
+ FileTest.exist?(@path)
811
+ end
705
812
 
706
813
  # See <tt>FileTest.grpowned?</tt>.
707
- def grpowned?() FileTest.grpowned?(@path) end
814
+ def grpowned?
815
+ FileTest.grpowned?(@path)
816
+ end
708
817
 
709
818
  # See <tt>FileTest.directory?</tt>.
710
- def directory?() FileTest.directory?(@path) end
819
+ def directory?
820
+ FileTest.directory?(@path)
821
+ end
711
822
 
712
823
  # See <tt>FileTest.file?</tt>.
713
- def file?() FileTest.file?(@path) end
824
+ def file?
825
+ FileTest.file?(@path)
826
+ end
714
827
 
715
828
  # See <tt>FileTest.pipe?</tt>.
716
- def pipe?() FileTest.pipe?(@path) end
829
+ def pipe?
830
+ FileTest.pipe?(@path)
831
+ end
717
832
 
718
833
  # See <tt>FileTest.socket?</tt>.
719
- def socket?() FileTest.socket?(@path) end
834
+ def socket?
835
+ FileTest.socket?(@path)
836
+ end
720
837
 
721
838
  # See <tt>FileTest.owned?</tt>.
722
- def owned?() FileTest.owned?(@path) end
839
+ def owned?
840
+ FileTest.owned?(@path)
841
+ end
723
842
 
724
843
  # See <tt>FileTest.readable?</tt>.
725
- def readable?() FileTest.readable?(@path) end
844
+ def readable?
845
+ FileTest.readable?(@path)
846
+ end
726
847
 
727
848
  # See <tt>FileTest.world_readable?</tt>.
728
- def world_readable?() FileTest.world_readable?(@path) end
849
+ def world_readable?
850
+ FileTest.world_readable?(@path)
851
+ end
729
852
 
730
853
  # See <tt>FileTest.readable_real?</tt>.
731
- def readable_real?() FileTest.readable_real?(@path) end
854
+ def readable_real?
855
+ FileTest.readable_real?(@path)
856
+ end
732
857
 
733
858
  # See <tt>FileTest.setuid?</tt>.
734
- def setuid?() FileTest.setuid?(@path) end
859
+ def setuid?
860
+ FileTest.setuid?(@path)
861
+ end
735
862
 
736
863
  # See <tt>FileTest.setgid?</tt>.
737
- def setgid?() FileTest.setgid?(@path) end
864
+ def setgid?
865
+ FileTest.setgid?(@path)
866
+ end
738
867
 
739
868
  # See <tt>FileTest.size</tt>.
740
- def size() FileTest.size(@path) end
869
+ def size
870
+ FileTest.size(@path)
871
+ end
741
872
 
742
873
  # See <tt>FileTest.size?</tt>.
743
- def size?() FileTest.size?(@path) end
874
+ def size?
875
+ FileTest.size?(@path)
876
+ end
744
877
 
745
878
  # See <tt>FileTest.sticky?</tt>.
746
- def sticky?() FileTest.sticky?(@path) end
879
+ def sticky?
880
+ FileTest.sticky?(@path)
881
+ end
747
882
 
748
883
  # See <tt>FileTest.symlink?</tt>.
749
- def symlink?() FileTest.symlink?(@path) end
884
+ def symlink?
885
+ FileTest.symlink?(@path)
886
+ end
750
887
 
751
888
  # See <tt>FileTest.writable?</tt>.
752
- def writable?() FileTest.writable?(@path) end
889
+ def writable?
890
+ FileTest.writable?(@path)
891
+ end
753
892
 
754
893
  # See <tt>FileTest.world_writable?</tt>.
755
- def world_writable?() FileTest.world_writable?(@path) end
894
+ def world_writable?
895
+ FileTest.world_writable?(@path)
896
+ end
756
897
 
757
898
  # See <tt>FileTest.writable_real?</tt>.
758
- def writable_real?() FileTest.writable_real?(@path) end
899
+ def writable_real?
900
+ FileTest.writable_real?(@path)
901
+ end
759
902
 
760
903
  # See <tt>FileTest.zero?</tt>.
761
- def zero?() FileTest.zero?(@path) end
904
+ def zero?
905
+ FileTest.zero?(@path)
906
+ end
762
907
  end
763
908
 
764
-
909
+ # Pathname class
765
910
  class Pathname # * Dir *
766
911
  # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
767
- def Pathname.glob(*args) # :yield: pathname
912
+ def self.glob(*args) # :yield: pathname
768
913
  if block_given?
769
- Dir.glob(*args) {|f| yield self.new(f) }
914
+ Dir.glob(*args) { |f| yield new(f) }
770
915
  else
771
- Dir.glob(*args).map {|f| self.new(f) }
916
+ Dir.glob(*args).map { |f| new(f) }
772
917
  end
773
918
  end
774
919
 
775
- # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
776
- def Pathname.getwd() self.new(Dir.getwd) end
777
- class << self; alias pwd getwd end
920
+ # See <tt>Dir.getwd</tt>. Returns the current working directory
921
+ # as a Pathname.
922
+ def self.getwd
923
+ new(Dir.getwd)
924
+ end
778
925
 
779
- # Return the entries (files and subdirectories) in the directory, each as a
780
- # Pathname object.
781
- def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
926
+ class << self; alias_method :pwd, :getwd end
782
927
 
783
- # Iterates over the entries (files and subdirectories) in the directory. It
784
- # yields a Pathname object for each entry.
928
+ # Return the entries (files and subdirectories) in the directory, each as
929
+ # a Pathname object.
930
+ def entries
931
+ Dir.entries(@path).map { |f| self.class.new(f) }
932
+ end
933
+
934
+ # Iterates over the entries (files and subdirectories) in the directory.
935
+ # It yields a Pathname object for each entry.
785
936
  #
786
937
  # This method has existed since 1.8.1.
787
- def each_entry(&block) # :yield: pathname
788
- Dir.foreach(@path) {|f| yield self.class.new(f) }
938
+ def each_entry(*) # :yield: pathname
939
+ Dir.foreach(@path) { |f| yield self.class.new(f) }
789
940
  end
790
941
 
791
942
  # See <tt>Dir.mkdir</tt>. Create the referenced directory.
792
- def mkdir(*args) Dir.mkdir(@path, *args) end
943
+ def mkdir(*args)
944
+ Dir.mkdir(@path, *args)
945
+ end
793
946
 
794
947
  # See <tt>Dir.rmdir</tt>. Remove the referenced directory.
795
- def rmdir() Dir.rmdir(@path) end
948
+ def rmdir
949
+ Dir.rmdir(@path)
950
+ end
796
951
 
797
952
  # See <tt>Dir.open</tt>.
798
953
  def opendir(&block) # :yield: dir
@@ -800,29 +955,30 @@ module FakeFS
800
955
  end
801
956
  end
802
957
 
803
-
958
+ # Pathname class
804
959
  class Pathname # * Find *
805
960
  #
806
- # Pathname#find is an iterator to traverse a directory tree in a depth first
807
- # manner. It yields a Pathname for each file under "this" directory.
961
+ # Pathname#find is an iterator to traverse a directory tree
962
+ # in a depth first manner.
963
+ # It yields a Pathname for each file under "this" directory.
808
964
  #
809
- # Since it is implemented by <tt>find.rb</tt>, <tt>Find.prune</tt> can be used
810
- # to control the traverse.
965
+ # Since it is implemented by <tt>find.rb</tt>, <tt>Find.prune</tt>
966
+ # can be used to control the traverse.
811
967
  #
812
- # If +self+ is <tt>.</tt>, yielded pathnames begin with a filename in the
813
- # current directory, not <tt>./</tt>.
968
+ # If +self+ is <tt>.</tt>, yielded pathnames begin with
969
+ # a filename in the current directory, not <tt>./</tt>.
814
970
  #
815
- def find(&block) # :yield: pathname
971
+ def find(*) # :yield: pathname
816
972
  require 'find'
817
973
  if @path == '.'
818
- Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
974
+ Find.find(@path) { |f| yield self.class.new(f.sub(%r{/\A\./}, '')) }
819
975
  else
820
- Find.find(@path) {|f| yield self.class.new(f) }
976
+ Find.find(@path) { |f| yield self.class.new(f) }
821
977
  end
822
978
  end
823
979
  end
824
980
 
825
-
981
+ # Pathname class
826
982
  class Pathname # * FileUtils *
827
983
  # See <tt>FileUtils.mkpath</tt>. Creates a full path, including any
828
984
  # intermediate directories that don't yet exist.
@@ -842,23 +998,21 @@ module FakeFS
842
998
  end
843
999
  end
844
1000
 
845
-
1001
+ # Pathname class
846
1002
  class Pathname # * mixed *
847
1003
  # Removes a file or directory, using <tt>File.unlink</tt> or
848
1004
  # <tt>Dir.unlink</tt> as necessary.
849
- def unlink()
850
- begin
851
- Dir.unlink @path
852
- rescue Errno::ENOTDIR
853
- File.unlink @path
854
- end
1005
+ def unlink
1006
+ Dir.unlink @path if File.directory? @path
1007
+ File.unlink @path unless File.directory? @path
855
1008
  end
856
- alias delete unlink
1009
+
1010
+ alias_method :delete, :unlink
857
1011
  end
858
1012
 
1013
+ # Pathname class
859
1014
  class Pathname
860
1015
  undef =~
861
1016
  end
862
1017
  end # RUBY_VERSION >= 1.9.3
863
1018
  end
864
-