Skip to content

Commit a3cf7be

Browse files
committed
Corrected handling of byte/unicode paths in several functions
- added FakeOsModule.getcwdb() for Python 3 - see #187
1 parent de0d107 commit a3cf7be

File tree

3 files changed

+206
-98
lines changed

3 files changed

+206
-98
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The release versions are PyPi releases.
1515
* `mox3` is no longer required - the relevant part has been integrated into pyfakefs ([#182](../../issues/182))
1616

1717
#### Fixes
18+
* Corrected handling of byte/unicode paths in several functions ([#187](../../issues/187))
1819
* `FakeShutilModule.rmtree` failed for directory ending with path separator ([#177](../../issues/177))
1920
* Case incorrectly handled for added Windows drives
2021
* `pathlib.glob()` incorrectly handled case under MacOS ([#167](../../issues/167))

fake_filesystem_test.py

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,14 +2548,38 @@ def setUp(self):
25482548
def tearDown(self):
25492549
time.time = self.orig_time
25502550

2551-
def testAbspath(self):
2551+
def checkAbspath(self, is_windows):
2552+
# the implementation differs in Windows and Posix, so test both
2553+
self.filesystem.is_windows_fs = is_windows
2554+
filename = u'foo'
2555+
abspath = u'!%s' % filename
2556+
self.filesystem.CreateFile(abspath)
2557+
self.assertEqual(abspath, self.path.abspath(abspath))
2558+
self.assertEqual(abspath, self.path.abspath(filename))
2559+
self.assertEqual(abspath, self.path.abspath(u'..!%s' % filename))
2560+
2561+
def testAbspathWindows(self):
2562+
self.checkAbspath(is_windows=True)
2563+
2564+
def testAbspathPosix(self):
25522565
"""abspath should return a consistent representation of a file."""
2553-
filename = 'foo'
2554-
abspath = '!%s' % filename
2566+
self.checkAbspath(is_windows=False)
2567+
2568+
def checkAbspathBytes(self, is_windows):
2569+
"""abspath should return a consistent representation of a file."""
2570+
self.filesystem.is_windows_fs = is_windows
2571+
filename = b'foo'
2572+
abspath = b'!' + filename
25552573
self.filesystem.CreateFile(abspath)
25562574
self.assertEqual(abspath, self.path.abspath(abspath))
25572575
self.assertEqual(abspath, self.path.abspath(filename))
2558-
self.assertEqual(abspath, self.path.abspath('..!%s' % filename))
2576+
self.assertEqual(abspath, self.path.abspath(b'..!' + filename))
2577+
2578+
def testAbspathBytesWindows(self):
2579+
self.checkAbspathBytes(is_windows=True)
2580+
2581+
def testAbspathBytesPosix(self):
2582+
self.checkAbspathBytes(is_windows=False)
25592583

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

2643-
def testJoin(self):
2644-
components = ['foo', 'bar', 'baz']
2645-
self.assertEqual('foo!bar!baz', self.path.join(*components))
2667+
def testJoinStrings(self):
2668+
components = [u'foo', u'bar', u'baz']
2669+
self.assertEqual(u'foo!bar!baz', self.path.join(*components))
2670+
2671+
def testJoinBytes(self):
2672+
components = [b'foo', b'bar', b'baz']
2673+
self.assertEqual(b'foo!bar!baz', self.path.join(*components))
26462674

26472675
def testExpandUser(self):
26482676
if self.is_windows:
@@ -4232,14 +4260,25 @@ def testCollapsePath(self):
42324260
def testCollapseUncPath(self):
42334261
self.assertEqual('!!foo!bar!baz', self.filesystem.CollapsePath('!!foo!bar!!baz!!'))
42344262

4235-
def testNormalizePath(self):
4236-
self.assertEqual('c:!foo!bar', self.filesystem.NormalizePath('c:!foo!!bar'))
4237-
self.filesystem.cwd = 'c:!foo'
4238-
self.assertEqual('c:!foo!bar', self.filesystem.NormalizePath('bar'))
4263+
def testNormalizePathStr(self):
4264+
self.filesystem.cwd = u''
4265+
self.assertEqual(u'c:!foo!bar', self.filesystem.NormalizePath(u'c:!foo!!bar'))
4266+
self.filesystem.cwd = u'c:!foo'
4267+
self.assertEqual(u'c:!foo!bar', self.filesystem.NormalizePath(u'bar'))
42394268

4240-
def testSplitPath(self):
4241-
self.assertEqual(('c:!foo', 'bar'), self.filesystem.SplitPath('c:!foo!bar'))
4242-
self.assertEqual(('c:', 'foo'), self.filesystem.SplitPath('c:!foo'))
4269+
def testNormalizePathBytes(self):
4270+
self.filesystem.cwd = b''
4271+
self.assertEqual(b'c:!foo!bar', self.filesystem.NormalizePath(b'c:!foo!!bar'))
4272+
self.filesystem.cwd = b'c:!foo'
4273+
self.assertEqual(b'c:!foo!bar', self.filesystem.NormalizePath(b'bar'))
4274+
4275+
def testSplitPathStr(self):
4276+
self.assertEqual((u'c:!foo', u'bar'), self.filesystem.SplitPath(u'c:!foo!bar'))
4277+
self.assertEqual((u'c:', u'foo'), self.filesystem.SplitPath(u'c:!foo'))
4278+
4279+
def testSplitPathBytes(self):
4280+
self.assertEqual((b'c:!foo', b'bar'), self.filesystem.SplitPath(b'c:!foo!bar'))
4281+
self.assertEqual((b'c:', b'foo'), self.filesystem.SplitPath(b'c:!foo'))
42434282

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

4254-
def testSplitDrive(self):
4255-
self.assertEqual(('c:', '!foo!bar'), self.filesystem.SplitDrive('c:!foo!bar'))
4256-
self.assertEqual(('', '!foo!bar'), self.filesystem.SplitDrive('!foo!bar'))
4257-
self.assertEqual(('c:', 'foo!bar'), self.filesystem.SplitDrive('c:foo!bar'))
4258-
self.assertEqual(('', 'foo!bar'), self.filesystem.SplitDrive('foo!bar'))
4293+
def testSplitDriveStr(self):
4294+
self.assertEqual((u'c:', u'!foo!bar'), self.filesystem.SplitDrive(u'c:!foo!bar'))
4295+
self.assertEqual((u'', u'!foo!bar'), self.filesystem.SplitDrive(u'!foo!bar'))
4296+
self.assertEqual((u'c:', u'foo!bar'), self.filesystem.SplitDrive(u'c:foo!bar'))
4297+
self.assertEqual((u'', u'foo!bar'), self.filesystem.SplitDrive(u'foo!bar'))
4298+
4299+
def testSplitDriveBytes(self):
4300+
self.assertEqual((b'c:', b'!foo!bar'), self.filesystem.SplitDrive(b'c:!foo!bar'))
4301+
self.assertEqual((b'', b'!foo!bar'), self.filesystem.SplitDrive(b'!foo!bar'))
42594302

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

0 commit comments

Comments
 (0)