Skip to content

Commit 7f6a868

Browse files
committed
Minor changes to copyRealFile
- do not test the whole content of string contents as this will not work with other line endings (e.g. on Windows) - moved setUpClass() code into class as setUpClass() is not available under Python 2.6
1 parent aa239a0 commit 7f6a868

File tree

3 files changed

+48
-46
lines changed

3 files changed

+48
-46
lines changed

CHANGES.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ The release versions are PyPi releases.
33

44
## Version 3.1 (as yet unreleased)
55

6-
* Added helper method `FakeFile.CopyRealFile()` to copy a file from \
7-
the real file system to the fake file system. This makes it easy to use \
6+
#### New Features
7+
* Added helper method `TestCase.copyRealFile()` to copy a file from
8+
the real file system to the fake file system. This makes it easy to use
89
files from the real file system in your tests.
10+
* A pytest plugin is now installed with pyfakefs that exports `fs`
11+
as a pytest fixture.
912

1013
## Version 3.0
1114

fake_filesystem_unittest_test.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,11 @@ def test_own_path_module(self):
171171
class TestCopyRealFile(TestPyfakefsUnittestBase):
172172
"""Tests the `fake_filesystem_unittest.TestCase.copyRealFile()` method."""
173173

174-
@classmethod
175-
def setUpClass(cls):
176-
cls.real_stat = os.stat(__file__)
177-
with open(__file__) as f:
178-
cls.real_string_contents = f.read()
179-
with open(__file__, 'rb') as f:
180-
cls.real_byte_contents = f.read()
174+
real_stat = os.stat(__file__)
175+
with open(__file__) as f:
176+
real_string_contents = f.read()
177+
with open(__file__, 'rb') as f:
178+
real_byte_contents = f.read()
181179

182180
def testCopyRealFile(self):
183181
'''Copy a real file to the fake file system'''
@@ -190,7 +188,8 @@ def testCopyRealFile(self):
190188
'Verify real file string contents')
191189
self.assertTrue(b'class TestCopyRealFile(TestPyfakefsUnittestBase)' in self.real_byte_contents,
192190
'Verify real file byte contents')
193-
self.assertEqual(fake_file.contents, self.real_string_contents)
191+
192+
# note that real_string_contents may differ to fake_file.contents due to newline conversions in open()
194193
self.assertEqual(fake_file.byte_contents, self.real_byte_contents)
195194

196195
self.assertEqual(fake_file.st_mode, self.real_stat.st_mode)

pyfakefs/fake_filesystem_unittest.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from pyfakefs import fake_filesystem
5050
from pyfakefs import fake_filesystem_shutil
5151
from pyfakefs import fake_tempfile
52+
5253
if sys.version_info >= (3, 4):
5354
from pyfakefs import fake_pathlib
5455

@@ -119,41 +120,40 @@ def fs(self):
119120
def patches(self):
120121
return self._stubber.patches
121122

122-
if sys.version_info >= (2, 7):
123-
def copyRealFile(self, real_file_path, fake_file_path=None,
124-
create_missing_dirs=True):
125-
"""Copy the file `real_file_path` from the real file system to the fake
126-
file system file `fake_file_path`. The permissions, gid, uid, ctime,
127-
mtime and atime of the real file are copied to the fake file.
128-
129-
This is a helper method you can use to set up your test more easily.
130-
131-
This method is available in Python 2.7 and above.
132-
133-
Args:
134-
real_file_path: Path to the source file in the real file system.
135-
fake_file_path: path to the destination file in the fake file system.
136-
create_missing_dirs: if True, auto create missing directories.
137-
138-
Returns:
139-
the newly created FakeFile object.
140-
141-
Raises:
142-
IOError: if the file already exists.
143-
IOError: if the containing directory is required and missing.
144-
"""
145-
real_stat = REAL_OS.stat(real_file_path)
146-
with REAL_OPEN(real_file_path, 'rb') as real_file:
147-
real_contents = real_file.read()
148-
fake_file = self.fs.CreateFile(fake_file_path, st_mode=real_stat.st_mode,
149-
contents=real_contents,
150-
create_missing_dirs=create_missing_dirs)
151-
fake_file.st_ctime = real_stat.st_ctime
152-
fake_file.st_atime = real_stat.st_atime
153-
fake_file.st_mtime = real_stat.st_mtime
154-
fake_file.st_gid = real_stat.st_gid
155-
fake_file.st_uid = real_stat.st_uid
156-
return fake_file
123+
def copyRealFile(self, real_file_path, fake_file_path=None,
124+
create_missing_dirs=True):
125+
"""Copy the file `real_file_path` from the real file system to the fake
126+
file system file `fake_file_path`. The permissions, gid, uid, ctime,
127+
mtime and atime of the real file are copied to the fake file.
128+
129+
This is a helper method you can use to set up your test more easily.
130+
131+
This method is available in Python 2.7 and above.
132+
133+
Args:
134+
real_file_path: Path to the source file in the real file system.
135+
fake_file_path: path to the destination file in the fake file system.
136+
create_missing_dirs: if True, auto create missing directories.
137+
138+
Returns:
139+
the newly created FakeFile object.
140+
141+
Raises:
142+
IOError: if the file already exists.
143+
IOError: if the containing directory is required and missing.
144+
"""
145+
real_stat = REAL_OS.stat(real_file_path)
146+
with REAL_OPEN(real_file_path, 'rb') as real_file:
147+
real_contents = real_file.read()
148+
fake_file = self.fs.CreateFile(fake_file_path, st_mode=real_stat.st_mode,
149+
contents=real_contents,
150+
create_missing_dirs=create_missing_dirs)
151+
fake_file.st_ctime = real_stat.st_ctime
152+
fake_file.st_atime = real_stat.st_atime
153+
fake_file.st_mtime = real_stat.st_mtime
154+
fake_file.st_gid = real_stat.st_gid
155+
fake_file.st_uid = real_stat.st_uid
156+
return fake_file
157157

158158
def setUpPyfakefs(self):
159159
"""Bind the file-related modules to the :py:class:`pyfakefs` fake file
@@ -262,7 +262,7 @@ def _findModules(self):
262262
for name, module in set(sys.modules.items()):
263263
if (module in self.SKIPMODULES or
264264
(not inspect.ismodule(module)) or
265-
name.split('.')[0] in self._skipNames):
265+
name.split('.')[0] in self._skipNames):
266266
continue
267267
if 'os' in module.__dict__:
268268
self._os_modules.add(module)

0 commit comments

Comments
 (0)