63
63
64
64
65
65
def load_doctests (loader , tests , ignore , module ,
66
- additional_skip_names = None , patch_path = True ): # pylint: disable=unused-argument
66
+ additional_skip_names = None ,
67
+ patch_path = True , special_names = None ): # pylint: disable=unused-argument
67
68
"""Load the doctest tests for the specified module into unittest.
68
69
Args:
69
70
loader, tests, ignore : arguments passed in from `load_tests()`
@@ -74,7 +75,7 @@ def load_doctests(loader, tests, ignore, module,
74
75
File `example_test.py` in the pyfakefs release provides a usage example.
75
76
"""
76
77
_patcher = Patcher (additional_skip_names = additional_skip_names ,
77
- patch_path = patch_path )
78
+ patch_path = patch_path , special_names = special_names )
78
79
globs = _patcher .replaceGlobs (vars (module ))
79
80
tests .addTests (doctest .DocTestSuite (module ,
80
81
globs = globs ,
@@ -88,7 +89,8 @@ class TestCase(unittest.TestCase):
88
89
modules by fake implementations.
89
90
"""
90
91
91
- def __init__ (self , methodName = 'runTest' , additional_skip_names = None , patch_path = True ):
92
+ def __init__ (self , methodName = 'runTest' , additional_skip_names = None ,
93
+ patch_path = True , special_names = None ):
92
94
"""Creates the test class instance and the stubber used to stub out
93
95
file system related modules.
94
96
@@ -103,21 +105,34 @@ def __init__(self, methodName='runTest', additional_skip_names=None, patch_path=
103
105
from my_module import path
104
106
Irrespective of patch_path, module 'os.path' is still correctly faked
105
107
if imported the usual way using `import os` or `import os.path`.
108
+ special_names: A dictionary with module names as key and a dictionary as
109
+ value, where the key is the original name of the module to be patched,
110
+ and the value is the name as it is imported.
111
+ This allows to patch modules where some of the file system modules are
112
+ imported as another name (e.g. `import os as _os`).
106
113
107
114
If you specify arguments `additional_skip_names` or `patch_path` here
108
115
and you have DocTests, consider also specifying the same arguments to
109
116
:py:func:`load_doctests`.
110
117
111
- Example usage in a derived test class ::
118
+ Example usage in derived test classes ::
112
119
113
- class MyTestCase(fake_filesystem_unittest.TestCase):
114
- def __init__(self, methodName='runTest'):
115
- super(MyTestCase, self).__init__(
116
- methodName=methodName, additional_skip_names=['posixpath'])
120
+ class MyTestCase(fake_filesystem_unittest.TestCase):
121
+ def __init__(self, methodName='runTest'):
122
+ super(MyTestCase, self).__init__(
123
+ methodName=methodName, additional_skip_names=['posixpath'])
124
+
125
+
126
+ class AnotherTestCase(fake_filesystem_unittest.TestCase):
127
+ def __init__(self, methodName='runTest'):
128
+ # allow patching a module that imports `os` as `my_os`
129
+ special_names = {'amodule': {'os': 'my_os'}}
130
+ super(MyTestCase, self).__init__(
131
+ methodName=methodName, special_names=special_names)
117
132
"""
118
133
super (TestCase , self ).__init__ (methodName )
119
134
self ._stubber = Patcher (additional_skip_names = additional_skip_names ,
120
- patch_path = patch_path )
135
+ patch_path = patch_path , special_names = special_names )
121
136
122
137
@property
123
138
def fs (self ):
@@ -210,10 +225,11 @@ class Patcher(object):
210
225
if HAS_PATHLIB :
211
226
SKIPNAMES .add ('pathlib' )
212
227
213
- def __init__ (self , additional_skip_names = None , patch_path = True ):
228
+ def __init__ (self , additional_skip_names = None , patch_path = True , special_names = None ):
214
229
"""For a description of the arguments, see TestCase.__init__"""
215
230
216
231
self ._skipNames = self .SKIPNAMES .copy ()
232
+ self ._special_names = special_names or {}
217
233
if additional_skip_names is not None :
218
234
self ._skipNames .update (additional_skip_names )
219
235
self ._patchPath = patch_path
@@ -282,17 +298,28 @@ def _findModules(self):
282
298
# and a test in fake_filesystem_unittest_test.py, class
283
299
# TestAttributesWithFakeModuleNames.
284
300
if inspect .ismodule (module .__dict__ .get ('os' )):
285
- self ._os_modules .add (module )
301
+ self ._os_modules .add (( module , 'os' ) )
286
302
if self ._patchPath and inspect .ismodule (module .__dict__ .get ('path' )):
287
- self ._path_modules .add (module )
303
+ self ._path_modules .add (( module , 'path' ) )
288
304
if self .HAS_PATHLIB and inspect .ismodule (module .__dict__ .get ('pathlib' )):
289
- self ._pathlib_modules .add (module )
305
+ self ._pathlib_modules .add (( module , 'pathlib' ) )
290
306
if inspect .ismodule (module .__dict__ .get ('shutil' )):
291
- self ._shutil_modules .add (module )
307
+ self ._shutil_modules .add (( module , 'shutil' ) )
292
308
if inspect .ismodule (module .__dict__ .get ('tempfile' )):
293
- self ._tempfile_modules .add (module )
309
+ self ._tempfile_modules .add (( module , 'tempfile' ) )
294
310
if inspect .ismodule (module .__dict__ .get ('io' )):
295
- self ._io_modules .add (module )
311
+ self ._io_modules .add ((module , 'io' ))
312
+ print (module , type (module ))
313
+ if '__name__' in module .__dict__ and module .__name__ in self ._special_names :
314
+ module_names = self ._special_names [module .__name__ ]
315
+ if 'os' in module_names :
316
+ self ._os_modules .add ((module , module_names ['os' ]))
317
+ if self ._patchPath and 'path' in module_names :
318
+ self ._path_modules .add ((module , module_names ['path' ]))
319
+ if self .HAS_PATHLIB and 'pathlib' in module_names :
320
+ self ._pathlib_modules .add ((module , module_names ['pathlib' ]))
321
+ if 'io' in module_names :
322
+ self ._io_modules .add ((module , module_names ['io' ]))
296
323
297
324
def _refresh (self ):
298
325
"""Renew the fake file system and set the _isStale flag to `False`."""
@@ -326,19 +353,19 @@ def setUp(self, doctester=None):
326
353
self ._stubs .SmartSet (builtins , 'file' , self .fake_open )
327
354
self ._stubs .SmartSet (builtins , 'open' , self .fake_open )
328
355
329
- for module in self ._os_modules :
330
- self ._stubs .SmartSet (module , 'os' , self .fake_os )
331
- for module in self ._path_modules :
332
- self ._stubs .SmartSet (module , 'path' , self .fake_path )
356
+ for module , attr in self ._os_modules :
357
+ self ._stubs .SmartSet (module , attr , self .fake_os )
358
+ for module , attr in self ._path_modules :
359
+ self ._stubs .SmartSet (module , attr , self .fake_path )
333
360
if self .HAS_PATHLIB :
334
- for module in self ._pathlib_modules :
335
- self ._stubs .SmartSet (module , 'pathlib' , self .fake_pathlib )
336
- for module in self ._shutil_modules :
337
- self ._stubs .SmartSet (module , 'shutil' , self .fake_shutil )
338
- for module in self ._tempfile_modules :
339
- self ._stubs .SmartSet (module , 'tempfile' , self .fake_tempfile_ )
340
- for module in self ._io_modules :
341
- self ._stubs .SmartSet (module , 'io' , self .fake_io )
361
+ for module , attr in self ._pathlib_modules :
362
+ self ._stubs .SmartSet (module , attr , self .fake_pathlib )
363
+ for module , attr in self ._shutil_modules :
364
+ self ._stubs .SmartSet (module , attr , self .fake_shutil )
365
+ for module , attr in self ._tempfile_modules :
366
+ self ._stubs .SmartSet (module , attr , self .fake_tempfile_ )
367
+ for module , attr in self ._io_modules :
368
+ self ._stubs .SmartSet (module , attr , self .fake_io )
342
369
343
370
def replaceGlobs (self , globs_ ):
344
371
globs = globs_ .copy ()
0 commit comments