From 192444786bba02969bac58b724953de3253b67c2 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Sat, 17 Jun 2017 17:37:15 +0200 Subject: [PATCH] Make sure adding object to a symlinked directory works - see #215 --- CHANGES.md | 7 ++++--- fake_filesystem_test.py | 9 +++++++++ pyfakefs/fake_filesystem.py | 5 ++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d12b8eac..c69b9ecc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,15 +5,16 @@ The release versions are PyPi releases. #### New Features * Added support for basic modes in fake `os.open()` ([#204](../../issues/204)). - * Added fake `os.path.samefile` implementation ([#193](../../issues/193)) + * Added fake `os.path.samefile` implementation ([#193](../../issues/193)). * Added support for `ns` argument in `os.utime()` (Python >= 3.3) ([#192](../../issues/192)). * Added nanosecond time members in `os.stat_result` (Python >= 3.3) ([#196](../../issues/196)). #### Infrastructure #### Fixes - * Incorrect error handling during directory creation ([#209](../../issues/209)) - * Creating files in read-only directory was possible ([#203](../../issues/203)) + * Failed to create directory with symlink as parent ([#215](../../issues/215)). + * Incorrect error handling during directory creation ([#209](../../issues/209)). + * Creating files in read-only directory was possible ([#203](../../issues/203)). ## [Version 3.2](https://pypi.python.org/pypi/pyfakefs/3.2) diff --git a/fake_filesystem_test.py b/fake_filesystem_test.py index b1e86372..8e921842 100755 --- a/fake_filesystem_test.py +++ b/fake_filesystem_test.py @@ -1457,6 +1457,15 @@ def testMkdirRaisesIfParentIsReadOnly(self): directory = '/a/b' self.assertRaises(Exception, self.os.mkdir, directory) + def testMkdirWithWithSymlinkParent(self): + dir_path = '/foo/bar' + self.filesystem.CreateDirectory(dir_path) + link_path = '/foo/link' + self.os.symlink(dir_path, link_path) + new_dir = link_path + '/new_dir' + self.os.mkdir(new_dir) + self.assertTrue(self.filesystem.Exists(new_dir)) + def testMakedirs(self): """makedirs can create a directory even if parent does not exist.""" parent = 'xyzzy' diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index 5d3ff7e0..2ba02cfb 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -1773,7 +1773,10 @@ def AddObject(self, file_path, file_object): IOError: if file_path does not correspond to a directory. """ try: - target_directory = self.GetObject(file_path) + if not file_path: + target_directory = self.root + else: + target_directory = self.ResolveObject(file_path) target_directory.AddEntry(file_object) except AttributeError: raise IOError(errno.ENOTDIR,