Skip to content

Commit

Permalink
Merge pull request #109 from mrbean-bremen/fake_io_open
Browse files Browse the repository at this point in the history
Fake io open
  • Loading branch information
jmcgeheeiv authored Jun 14, 2016
2 parents e110e29 + 50a1969 commit 5ede6d5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
13 changes: 12 additions & 1 deletion fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""
Test the :py:class`pyfakefs.fake_filesystem_unittest.TestCase` base class.
"""

import io
import os
import glob
import shutil
Expand Down Expand Up @@ -65,6 +65,17 @@ def test_open(self):
self.assertEqual(content,
'This test file was created using the open() function.\n')

def test_io_open(self):
'''Fake io module is bound'''
self.assertFalse(os.path.exists('/fake_file.txt'))
with io.open('/fake_file.txt', 'w') as f:
f.write("This test file was created using the io.open() function.\n")
self.assertTrue(self.fs.Exists('/fake_file.txt'))
with open('/fake_file.txt') as f:
content = f.read()
self.assertEqual(content,
'This test file was created using the io.open() function.\n')

def test_os(self):
'''Fake os module is bound'''
self.assertFalse(self.fs.Exists('/test/dir1/dir2'))
Expand Down
27 changes: 27 additions & 0 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,33 @@ def __getattr__(self, name):
"""Forwards any unfaked calls to the standard os module."""
return getattr(self._os_module, name)

class FakeIoModule(object):
"""Uses FakeFilesystem to provide a fake io module replacement.
Currently only used to wrap io.open() which is an alias to open()
# You need a fake_filesystem to use this:
filesystem = fake_filesystem.FakeFilesystem()
my_io_module = fake_filesystem.FakeIoModule(filesystem)
"""

def __init__(self, filesystem):
"""
Args:
filesystem: FakeFilesystem used to provide file system information
"""
self.filesystem = filesystem

def open(self, file_path, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True, opener=None):
"""Redirect the call to FakeFileOpen.
See FakeFileOpen.Call() for description.
"""
if opener is not None and sys.version_info < (3, 3):
raise TypeError("open() got an unexpected keyword argument 'opener'")
return FakeFileOpen(self.filesystem).Call(
file_path, mode, buffering, encoding, errors, newline, closefd, opener)


class FakeFileWrapper(object):
"""Wrapper for a StringIO object for use by a FakeFile object.
Expand Down
12 changes: 11 additions & 1 deletion pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Patcher(object):

# To add py.test support per issue https://github.com/jmcgeheeiv/pyfakefs/issues/43,
# it appears that adding 'py', 'pytest', '_pytest' to SKIPNAMES will help
SKIPNAMES = set(['os', 'glob', 'path', 'shutil', 'tempfile'])
SKIPNAMES = set(['os', 'glob', 'path', 'shutil', 'tempfile', 'io'])

def __init__(self):
# Attributes set by _findModules()
Expand All @@ -124,6 +124,7 @@ def __init__(self):
self._pathModules = None
self._shutilModules = None
self._tempfileModules = None
self._ioModules = None
self._findModules()
assert None not in vars(self).values(), \
"_findModules() missed the initialization of an instance variable"
Expand All @@ -137,6 +138,7 @@ def __init__(self):
self.fake_shutil = None
self.fake_tempfile_ = None
self.fake_open = None
self.fake_io = None
# _isStale is set by tearDown(), reset by _refresh()
self._isStale = True
self._refresh()
Expand All @@ -154,6 +156,7 @@ def _findModules(self):
self._pathModules = set()
self._shutilModules = set()
self._tempfileModules = set()
self._ioModules = set()
for name, module in set(sys.modules.items()):
if (module in self.SKIPMODULES or
(not inspect.ismodule(module)) or
Expand All @@ -169,6 +172,8 @@ def _findModules(self):
self._shutilModules.add(module)
if 'tempfile' in module.__dict__:
self._tempfileModules.add(module)
if 'io' in module.__dict__:
self._ioModules.add(module)

def _refresh(self):
'''Renew the fake file system and set the _isStale flag to `False`.'''
Expand All @@ -183,6 +188,7 @@ def _refresh(self):
self.fake_shutil = fake_filesystem_shutil.FakeShutilModule(self.fs)
self.fake_tempfile_ = fake_tempfile.FakeTempfileModule(self.fs)
self.fake_open = fake_filesystem.FakeFileOpen(self.fs)
self.fake_io = fake_filesystem.FakeIoModule(self.fs)

self._isStale = False

Expand Down Expand Up @@ -211,6 +217,8 @@ def setUp(self, doctester=None):
self._stubs.SmartSet(module, 'shutil', self.fake_shutil)
for module in self._tempfileModules:
self._stubs.SmartSet(module, 'tempfile', self.fake_tempfile_)
for module in self._ioModules:
self._stubs.SmartSet(module, 'io', self.fake_io)

def replaceGlobs(self, globs_):
globs = globs_.copy()
Expand All @@ -228,6 +236,8 @@ def replaceGlobs(self, globs_):
globs['shutil'] = fake_filesystem_shutil.FakeShutilModule(self.fs)
if 'tempfile' in globs:
globs['tempfile'] = fake_tempfile.FakeTempfileModule(self.fs)
if 'io' in globs:
globs['io'] = fake_filesystem.FakeIoModule(self.fs)
return globs

def tearDown(self, doctester=None):
Expand Down

0 comments on commit 5ede6d5

Please sign in to comment.