Skip to content

Commit

Permalink
Handle contents=None in create_file() as empty contents if size not set
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Aug 31, 2018
1 parent 8990455 commit 5ad3dbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2259,8 +2259,11 @@ def create_file(self, file_path, st_mode=S_IFREG | PERM_DEF_FILE,
Args:
file_path: The path to the file to create.
st_mode: The stat constant representing the file type.
contents: The contents of the file.
st_size: The file size; only valid if contents not given.
contents: the contents of the file. If not given and st_size is
None, an empty file is assumed.
st_size: file size; only valid if contents not given. If given,
the file is considered to be in "large file mode" and trying
to read from or write to the file will result in an exception.
create_missing_dirs: If `True`, auto create missing directories.
apply_umask: `True` if the current umask must be applied
on `st_mode`.
Expand Down Expand Up @@ -2413,8 +2416,11 @@ def create_file_internally(self, file_path,
Args:
file_path: path to the file to create.
st_mode: the stat.S_IF constant representing the file type.
contents: the contents of the file.
st_size: file size; only valid if contents not given.
contents: the contents of the file. If not given and st_size is
None, an empty file is assumed.
st_size: file size; only valid if contents not given. If given,
the file is considered to be in "large file mode" and trying
to read from or write to the file will result in an exception.
create_missing_dirs: if True, auto create missing directories.
apply_umask: whether or not the current umask must be applied
on st_mode.
Expand Down Expand Up @@ -2456,6 +2462,8 @@ def create_file_internally(self, file_path,
file_object.st_ino = self._last_ino
self.add_object(parent_directory, file_object, error_fct)

if st_size is None and contents is None:
contents = ''
if (not read_from_real_fs and
(contents is not None or st_size is not None)):
try:
Expand Down
7 changes: 7 additions & 0 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ def test_create_file(self):
self.assertTrue(stat.S_IFREG & new_file.st_mode)
self.assertEqual(new_file, retval)

def test_empty_file_created_for_none_contents(self):
fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
path = 'foo/bar/baz'
self.filesystem.create_file(path, contents=None)
with fake_open(path) as f:
self.assertEqual('', f.read())

def test_create_file_with_incorrect_mode_type(self):
self.assertRaises(TypeError, self.filesystem.create_file, 'foo', 'bar')

Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4693,7 +4693,7 @@ def test_path_links_not_resolved(self):
link_path = self.make_path('A', 'C')
self.os.symlink(dir_path, link_path)
self.assertEqual([self.os.path.join(link_path, 'D')],
[f.path for f in self.os.scandir(link_path)])
[f.path for f in self.scandir(link_path)])

def test_inode(self):
if has_scandir and self.is_windows and self.use_real_fs():
Expand Down

0 comments on commit 5ad3dbb

Please sign in to comment.