Skip to content

Commit

Permalink
Corrected handling of byte/unicode paths in several functions
Browse files Browse the repository at this point in the history
- added FakeOsModule.getcwdb() for Python 3
- see pytest-dev#187
  • Loading branch information
mrbean-bremen committed May 25, 2017
1 parent de0d107 commit a3cf7be
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 98 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The release versions are PyPi releases.
* `mox3` is no longer required - the relevant part has been integrated into pyfakefs ([#182](../../issues/182))

#### Fixes
* Corrected handling of byte/unicode paths in several functions ([#187](../../issues/187))
* `FakeShutilModule.rmtree` failed for directory ending with path separator ([#177](../../issues/177))
* Case incorrectly handled for added Windows drives
* `pathlib.glob()` incorrectly handled case under MacOS ([#167](../../issues/167))
Expand Down
81 changes: 62 additions & 19 deletions fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2548,14 +2548,38 @@ def setUp(self):
def tearDown(self):
time.time = self.orig_time

def testAbspath(self):
def checkAbspath(self, is_windows):
# the implementation differs in Windows and Posix, so test both
self.filesystem.is_windows_fs = is_windows
filename = u'foo'
abspath = u'!%s' % filename
self.filesystem.CreateFile(abspath)
self.assertEqual(abspath, self.path.abspath(abspath))
self.assertEqual(abspath, self.path.abspath(filename))
self.assertEqual(abspath, self.path.abspath(u'..!%s' % filename))

def testAbspathWindows(self):
self.checkAbspath(is_windows=True)

def testAbspathPosix(self):
"""abspath should return a consistent representation of a file."""
filename = 'foo'
abspath = '!%s' % filename
self.checkAbspath(is_windows=False)

def checkAbspathBytes(self, is_windows):
"""abspath should return a consistent representation of a file."""
self.filesystem.is_windows_fs = is_windows
filename = b'foo'
abspath = b'!' + filename
self.filesystem.CreateFile(abspath)
self.assertEqual(abspath, self.path.abspath(abspath))
self.assertEqual(abspath, self.path.abspath(filename))
self.assertEqual(abspath, self.path.abspath('..!%s' % filename))
self.assertEqual(abspath, self.path.abspath(b'..!' + filename))

def testAbspathBytesWindows(self):
self.checkAbspathBytes(is_windows=True)

def testAbspathBytesPosix(self):
self.checkAbspathBytes(is_windows=False)

def testAbspathDealsWithRelativeNonRootPath(self):
"""abspath should correctly handle relative paths from a non-! directory.
Expand Down Expand Up @@ -2640,9 +2664,13 @@ def testDirname(self):
dirname = 'foo!bar'
self.assertEqual(dirname, self.path.dirname('%s!baz' % dirname))

def testJoin(self):
components = ['foo', 'bar', 'baz']
self.assertEqual('foo!bar!baz', self.path.join(*components))
def testJoinStrings(self):
components = [u'foo', u'bar', u'baz']
self.assertEqual(u'foo!bar!baz', self.path.join(*components))

def testJoinBytes(self):
components = [b'foo', b'bar', b'baz']
self.assertEqual(b'foo!bar!baz', self.path.join(*components))

def testExpandUser(self):
if self.is_windows:
Expand Down Expand Up @@ -4232,14 +4260,25 @@ def testCollapsePath(self):
def testCollapseUncPath(self):
self.assertEqual('!!foo!bar!baz', self.filesystem.CollapsePath('!!foo!bar!!baz!!'))

def testNormalizePath(self):
self.assertEqual('c:!foo!bar', self.filesystem.NormalizePath('c:!foo!!bar'))
self.filesystem.cwd = 'c:!foo'
self.assertEqual('c:!foo!bar', self.filesystem.NormalizePath('bar'))
def testNormalizePathStr(self):
self.filesystem.cwd = u''
self.assertEqual(u'c:!foo!bar', self.filesystem.NormalizePath(u'c:!foo!!bar'))
self.filesystem.cwd = u'c:!foo'
self.assertEqual(u'c:!foo!bar', self.filesystem.NormalizePath(u'bar'))

def testSplitPath(self):
self.assertEqual(('c:!foo', 'bar'), self.filesystem.SplitPath('c:!foo!bar'))
self.assertEqual(('c:', 'foo'), self.filesystem.SplitPath('c:!foo'))
def testNormalizePathBytes(self):
self.filesystem.cwd = b''
self.assertEqual(b'c:!foo!bar', self.filesystem.NormalizePath(b'c:!foo!!bar'))
self.filesystem.cwd = b'c:!foo'
self.assertEqual(b'c:!foo!bar', self.filesystem.NormalizePath(b'bar'))

def testSplitPathStr(self):
self.assertEqual((u'c:!foo', u'bar'), self.filesystem.SplitPath(u'c:!foo!bar'))
self.assertEqual((u'c:', u'foo'), self.filesystem.SplitPath(u'c:!foo'))

def testSplitPathBytes(self):
self.assertEqual((b'c:!foo', b'bar'), self.filesystem.SplitPath(b'c:!foo!bar'))
self.assertEqual((b'c:', b'foo'), self.filesystem.SplitPath(b'c:!foo'))

def testCharactersBeforeRootIgnoredInJoinPaths(self):
self.assertEqual('c:d', self.filesystem.JoinPaths('b', 'c:', 'd'))
Expand All @@ -4251,11 +4290,15 @@ def testGetPathComponents(self):
self.assertEqual(['c:', 'foo', 'bar'], self.filesystem.GetPathComponents('c:!foo!bar'))
self.assertEqual(['c:'], self.filesystem.GetPathComponents('c:'))

def testSplitDrive(self):
self.assertEqual(('c:', '!foo!bar'), self.filesystem.SplitDrive('c:!foo!bar'))
self.assertEqual(('', '!foo!bar'), self.filesystem.SplitDrive('!foo!bar'))
self.assertEqual(('c:', 'foo!bar'), self.filesystem.SplitDrive('c:foo!bar'))
self.assertEqual(('', 'foo!bar'), self.filesystem.SplitDrive('foo!bar'))
def testSplitDriveStr(self):
self.assertEqual((u'c:', u'!foo!bar'), self.filesystem.SplitDrive(u'c:!foo!bar'))
self.assertEqual((u'', u'!foo!bar'), self.filesystem.SplitDrive(u'!foo!bar'))
self.assertEqual((u'c:', u'foo!bar'), self.filesystem.SplitDrive(u'c:foo!bar'))
self.assertEqual((u'', u'foo!bar'), self.filesystem.SplitDrive(u'foo!bar'))

def testSplitDriveBytes(self):
self.assertEqual((b'c:', b'!foo!bar'), self.filesystem.SplitDrive(b'c:!foo!bar'))
self.assertEqual((b'', b'!foo!bar'), self.filesystem.SplitDrive(b'!foo!bar'))

@unittest.skipIf(sys.version_info < (2, 7, 8), 'UNC path support since Python 2.7.8')
def testSplitDriveWithUncPath(self):
Expand Down
Loading

0 comments on commit a3cf7be

Please sign in to comment.