fakefs 1.5.0 → 1.7.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 825da63678bcf85ba7a0e7fdcdf067da5477187ffb2e5d982bcdae72084b8ea6
4
- data.tar.gz: 7d860f9926de5b203041b652ff4692731dfd5c34846908c48bc02e6b853e0dd1
3
+ metadata.gz: ce09ec8d126543da5199d5a49c32ac6e7122895c2a07985a0216a9b45105f6bc
4
+ data.tar.gz: 0bbcbe864737bc302f7c8f079f448f8f3a89128fb5bcdcd35c42c062b8d7c2aa
5
5
  SHA512:
6
- metadata.gz: 649a2c7d8c39458f3c81cdaf7eade2a5203fa5b1c33ac7e243e94afbb4da4dad6029772ee708d7034b6ad70ca15549e6bbb028967a92183b4bb26780c5374b6f
7
- data.tar.gz: cded1748b5a782e70923bf9cf3111f91cd2f06cb749db05768b2a619c884041a508e49221b2ddf775ac340bc76dd47bb26da1c397a6cd7f9b9dc99eedd22005b
6
+ metadata.gz: 22575e664d52f74dc4c14d84d8981c751d33a43e94b52891e51e9b1cc5000838f25fd8f8709de48908f4d01ad489c51d4aa0865fc6b1c82b77c63e0814d93d01
7
+ data.tar.gz: e570516fa4e06a352b1b90bf643331b296f60684920a51a65a897913fa7c1a91a2a2ea092bcb363d8727442281851fa3f3fe497d09100bbf5e1b2f4bbe3e8274
data/README.md CHANGED
@@ -71,6 +71,23 @@ FakeFS do
71
71
  end
72
72
  ```
73
73
 
74
+ Mocking IO methods
75
+ ------------------
76
+
77
+ The IO class is the very basis for all Input and Output in Ruby, not only simple File reading/writting operations.
78
+ To avoid breaking critical components, the fakefs gem is not mocking IO methods by default.
79
+
80
+ However you can enable some very simple mocks using an explicit optin keyword:
81
+
82
+ ``` ruby
83
+
84
+ require 'fakefs/safe'
85
+
86
+ FakeFS.activate!(io_mocks: true)
87
+ # your code
88
+ FakeFS.deactivate!
89
+ ```
90
+
74
91
  Rails
75
92
  -----
76
93
 
data/lib/fakefs/base.rb CHANGED
@@ -2,6 +2,7 @@ RealFile = File
2
2
  RealFileTest = FileTest
3
3
  RealFileUtils = FileUtils
4
4
  RealDir = Dir
5
+ RealIO = IO
5
6
  RealPathname = Pathname
6
7
 
7
8
  def RealPathname(*args)
@@ -20,7 +21,7 @@ module FakeFS
20
21
  end
21
22
 
22
23
  # unconditionally activate
23
- def activate!
24
+ def activate!(io_mocks: false)
24
25
  Object.class_eval do
25
26
  remove_const(:Dir)
26
27
  remove_const(:File)
@@ -33,6 +34,12 @@ module FakeFS
33
34
  const_set(:FileUtils, FakeFS::FileUtils)
34
35
  const_set(:FileTest, FakeFS::FileTest)
35
36
  const_set(:Pathname, FakeFS::Pathname)
37
+
38
+ if io_mocks
39
+ remove_const(:IO)
40
+ const_set(:IO, ::FakeFS::IO)
41
+ end
42
+
36
43
  ::FakeFS::Kernel.hijack!
37
44
  end
38
45
 
@@ -48,12 +55,14 @@ module FakeFS
48
55
  remove_const(:File)
49
56
  remove_const(:FileTest)
50
57
  remove_const(:FileUtils)
58
+ remove_const(:IO)
51
59
  remove_const(:Pathname)
52
60
 
53
61
  const_set(:Dir, RealDir)
54
62
  const_set(:File, RealFile)
55
63
  const_set(:FileTest, RealFileTest)
56
64
  const_set(:FileUtils, RealFileUtils)
65
+ const_set(:IO, RealIO)
57
66
  const_set(:Pathname, RealPathname)
58
67
  ::FakeFS::Kernel.unhijack!
59
68
  end
data/lib/fakefs/file.rb CHANGED
@@ -174,8 +174,8 @@ module FakeFS
174
174
  RealFile.basename(*args)
175
175
  end
176
176
 
177
- def self.dirname(path)
178
- RealFile.dirname(path)
177
+ def self.dirname(*args)
178
+ RealFile.dirname(*args)
179
179
  end
180
180
 
181
181
  def self.readlink(path)
@@ -624,7 +624,10 @@ module FakeFS
624
624
  end
625
625
 
626
626
  def binmode?
627
- raise NotImplementedError
627
+ @mode.is_a?(String) && (
628
+ @mode.include?('b') ||
629
+ @mode.include?('binary')
630
+ ) && !@mode.include?('bom')
628
631
  end
629
632
 
630
633
  def close_on_exec=(_bool)
@@ -702,7 +705,7 @@ module FakeFS
702
705
 
703
706
  def read(length = nil, buf = '')
704
707
  read_buf = super(length, buf)
705
- if binary_mode?
708
+ if binmode?
706
709
  read_buf&.force_encoding('ASCII-8BIT')
707
710
  else
708
711
  read_buf&.force_encoding(Encoding.default_external)
@@ -869,13 +872,6 @@ module FakeFS
869
872
  StringIO.new('', @mode)
870
873
  end
871
874
 
872
- def binary_mode?
873
- @mode.is_a?(String) && (
874
- @mode.include?('b') ||
875
- @mode.include?('binary')
876
- ) && !@mode.include?('bom')
877
- end
878
-
879
875
  def check_file_existence!
880
876
  raise Errno::ENOENT, @path.to_s unless @file
881
877
  end
data/lib/fakefs/io.rb ADDED
@@ -0,0 +1,20 @@
1
+ module FakeFS
2
+ # FakeFS IO class inherit root IO
3
+ # Only minimal mocks are provided as IO may be used by ruby's internals
4
+ class IO < ::IO
5
+ # Redirects ::IO.binread to ::FakeFS::File.read
6
+ def self.binread(*args, **keywords)
7
+ ::FakeFS::File.read(*args, **keywords)
8
+ end
9
+
10
+ # Redirects ::IO.read to ::FakeFS::File.read
11
+ def self.read(*args, **keywords)
12
+ ::FakeFS::File.read(*args, **keywords)
13
+ end
14
+
15
+ # Redirects ::IO.write to ::FakeFS::File.write
16
+ def self.write(*args, **keywords)
17
+ ::FakeFS::File.write(*args, **keywords)
18
+ end
19
+ end
20
+ end
data/lib/fakefs/safe.rb CHANGED
@@ -11,5 +11,6 @@ require 'fakefs/file'
11
11
  require 'fakefs/file_test'
12
12
  require 'fakefs/dir'
13
13
  require 'fakefs/globber'
14
+ require 'fakefs/io'
14
15
  require 'fakefs/pathname'
15
16
  require 'fakefs/kernel'
@@ -1,7 +1,7 @@
1
1
  module FakeFS
2
2
  # Version module
3
3
  module Version
4
- VERSION = '1.5.0'.freeze
4
+ VERSION = '1.7.0'.freeze
5
5
 
6
6
  def self.to_s
7
7
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakefs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2022-05-23 00:00:00.000000000 Z
15
+ date: 2022-06-19 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: bump
@@ -105,6 +105,7 @@ files:
105
105
  - lib/fakefs/file_test.rb
106
106
  - lib/fakefs/fileutils.rb
107
107
  - lib/fakefs/globber.rb
108
+ - lib/fakefs/io.rb
108
109
  - lib/fakefs/kernel.rb
109
110
  - lib/fakefs/pathname.rb
110
111
  - lib/fakefs/safe.rb