Skip to content

Commit 0d7e6a6

Browse files
authored
bpo-38087: Fix case sensitivity in test_pathlib and test_ntpath (GH-15850)
1 parent d94b762 commit 0d7e6a6

File tree

3 files changed

+64
-45
lines changed

3 files changed

+64
-45
lines changed

Lib/test/test_ntpath.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@
1414
# but for those that require it we import here.
1515
nt = None
1616

17+
18+
def _norm(path):
19+
if isinstance(path, (bytes, str, os.PathLike)):
20+
return ntpath.normcase(os.fsdecode(path))
21+
elif hasattr(path, "__iter__"):
22+
return tuple(ntpath.normcase(os.fsdecode(p)) for p in path)
23+
return path
24+
25+
1726
def tester(fn, wantResult):
1827
fn = fn.replace("\\", "\\\\")
1928
gotResult = eval(fn)
20-
if wantResult != gotResult:
29+
if wantResult != gotResult and _norm(wantResult) != _norm(gotResult):
2130
raise TestFailed("%s should return: %s but returned: %s" \
2231
%(str(fn), str(wantResult), str(gotResult)))
2332

@@ -33,16 +42,22 @@ def tester(fn, wantResult):
3342
with warnings.catch_warnings():
3443
warnings.simplefilter("ignore", DeprecationWarning)
3544
gotResult = eval(fn)
36-
if isinstance(wantResult, str):
37-
wantResult = os.fsencode(wantResult)
38-
elif isinstance(wantResult, tuple):
39-
wantResult = tuple(os.fsencode(r) for r in wantResult)
40-
if wantResult != gotResult:
45+
if _norm(wantResult) != _norm(gotResult):
4146
raise TestFailed("%s should return: %s but returned: %s" \
4247
%(str(fn), str(wantResult), repr(gotResult)))
4348

4449

45-
class TestNtpath(unittest.TestCase):
50+
class NtpathTestCase(unittest.TestCase):
51+
def assertPathEqual(self, path1, path2):
52+
if path1 == path2 or _norm(path1) == _norm(path2):
53+
return
54+
self.assertEqual(path1, path2)
55+
56+
def assertPathIn(self, path, pathset):
57+
self.assertIn(_norm(path), _norm(pathset))
58+
59+
60+
class TestNtpath(NtpathTestCase):
4661
def test_splitext(self):
4762
tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
4863
tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
@@ -459,7 +474,7 @@ class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
459474
attributes = ['relpath']
460475

461476

462-
class PathLikeTests(unittest.TestCase):
477+
class PathLikeTests(NtpathTestCase):
463478

464479
path = ntpath
465480

@@ -470,67 +485,67 @@ def setUp(self):
470485
with open(self.file_name, 'xb', 0) as file:
471486
file.write(b"test_ntpath.PathLikeTests")
472487

473-
def assertPathEqual(self, func):
474-
self.assertEqual(func(self.file_path), func(self.file_name))
488+
def _check_function(self, func):
489+
self.assertPathEqual(func(self.file_path), func(self.file_name))
475490

476491
def test_path_normcase(self):
477-
self.assertPathEqual(self.path.normcase)
492+
self._check_function(self.path.normcase)
478493

479494
def test_path_isabs(self):
480-
self.assertPathEqual(self.path.isabs)
495+
self._check_function(self.path.isabs)
481496

482497
def test_path_join(self):
483498
self.assertEqual(self.path.join('a', FakePath('b'), 'c'),
484499
self.path.join('a', 'b', 'c'))
485500

486501
def test_path_split(self):
487-
self.assertPathEqual(self.path.split)
502+
self._check_function(self.path.split)
488503

489504
def test_path_splitext(self):
490-
self.assertPathEqual(self.path.splitext)
505+
self._check_function(self.path.splitext)
491506

492507
def test_path_splitdrive(self):
493-
self.assertPathEqual(self.path.splitdrive)
508+
self._check_function(self.path.splitdrive)
494509

495510
def test_path_basename(self):
496-
self.assertPathEqual(self.path.basename)
511+
self._check_function(self.path.basename)
497512

498513
def test_path_dirname(self):
499-
self.assertPathEqual(self.path.dirname)
514+
self._check_function(self.path.dirname)
500515

501516
def test_path_islink(self):
502-
self.assertPathEqual(self.path.islink)
517+
self._check_function(self.path.islink)
503518

504519
def test_path_lexists(self):
505-
self.assertPathEqual(self.path.lexists)
520+
self._check_function(self.path.lexists)
506521

507522
def test_path_ismount(self):
508-
self.assertPathEqual(self.path.ismount)
523+
self._check_function(self.path.ismount)
509524

510525
def test_path_expanduser(self):
511-
self.assertPathEqual(self.path.expanduser)
526+
self._check_function(self.path.expanduser)
512527

513528
def test_path_expandvars(self):
514-
self.assertPathEqual(self.path.expandvars)
529+
self._check_function(self.path.expandvars)
515530

516531
def test_path_normpath(self):
517-
self.assertPathEqual(self.path.normpath)
532+
self._check_function(self.path.normpath)
518533

519534
def test_path_abspath(self):
520-
self.assertPathEqual(self.path.abspath)
535+
self._check_function(self.path.abspath)
521536

522537
def test_path_realpath(self):
523-
self.assertPathEqual(self.path.realpath)
538+
self._check_function(self.path.realpath)
524539

525540
def test_path_relpath(self):
526-
self.assertPathEqual(self.path.relpath)
541+
self._check_function(self.path.relpath)
527542

528543
def test_path_commonpath(self):
529544
common_path = self.path.commonpath([self.file_path, self.file_name])
530-
self.assertEqual(common_path, self.file_name)
545+
self.assertPathEqual(common_path, self.file_name)
531546

532547
def test_path_isdir(self):
533-
self.assertPathEqual(self.path.isdir)
548+
self._check_function(self.path.isdir)
534549

535550

536551
if __name__ == "__main__":

Lib/test/test_pathlib.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,10 +1271,13 @@ def assertFileNotFound(self, func, *args, **kwargs):
12711271
func(*args, **kwargs)
12721272
self.assertEqual(cm.exception.errno, errno.ENOENT)
12731273

1274+
def assertEqualNormCase(self, path_a, path_b):
1275+
self.assertEqual(os.path.normcase(path_a), os.path.normcase(path_b))
1276+
12741277
def _test_cwd(self, p):
12751278
q = self.cls(os.getcwd())
12761279
self.assertEqual(p, q)
1277-
self.assertEqual(str(p), str(q))
1280+
self.assertEqualNormCase(str(p), str(q))
12781281
self.assertIs(type(p), type(q))
12791282
self.assertTrue(p.is_absolute())
12801283

@@ -1285,7 +1288,7 @@ def test_cwd(self):
12851288
def _test_home(self, p):
12861289
q = self.cls(os.path.expanduser('~'))
12871290
self.assertEqual(p, q)
1288-
self.assertEqual(str(p), str(q))
1291+
self.assertEqualNormCase(str(p), str(q))
12891292
self.assertIs(type(p), type(q))
12901293
self.assertTrue(p.is_absolute())
12911294

@@ -1491,15 +1494,15 @@ def test_resolve_common(self):
14911494
p.resolve(strict=True)
14921495
self.assertEqual(cm.exception.errno, errno.ENOENT)
14931496
# Non-strict
1494-
self.assertEqual(str(p.resolve(strict=False)),
1495-
os.path.join(BASE, 'foo'))
1497+
self.assertEqualNormCase(str(p.resolve(strict=False)),
1498+
os.path.join(BASE, 'foo'))
14961499
p = P(BASE, 'foo', 'in', 'spam')
1497-
self.assertEqual(str(p.resolve(strict=False)),
1498-
os.path.join(BASE, 'foo', 'in', 'spam'))
1500+
self.assertEqualNormCase(str(p.resolve(strict=False)),
1501+
os.path.join(BASE, 'foo', 'in', 'spam'))
14991502
p = P(BASE, '..', 'foo', 'in', 'spam')
1500-
self.assertEqual(str(p.resolve(strict=False)),
1501-
os.path.abspath(os.path.join('foo', 'in', 'spam')))
1502-
# These are all relative symlinks
1503+
self.assertEqualNormCase(str(p.resolve(strict=False)),
1504+
os.path.abspath(os.path.join('foo', 'in', 'spam')))
1505+
# These are all relative symlinks.
15031506
p = P(BASE, 'dirB', 'fileB')
15041507
self._check_resolve_relative(p, p)
15051508
p = P(BASE, 'linkA')
@@ -1996,33 +1999,33 @@ def _check_complex_symlinks(self, link0_target):
19961999
# Resolve absolute paths
19972000
p = (P / 'link0').resolve()
19982001
self.assertEqual(p, P)
1999-
self.assertEqual(str(p), BASE)
2002+
self.assertEqualNormCase(str(p), BASE)
20002003
p = (P / 'link1').resolve()
20012004
self.assertEqual(p, P)
2002-
self.assertEqual(str(p), BASE)
2005+
self.assertEqualNormCase(str(p), BASE)
20032006
p = (P / 'link2').resolve()
20042007
self.assertEqual(p, P)
2005-
self.assertEqual(str(p), BASE)
2008+
self.assertEqualNormCase(str(p), BASE)
20062009
p = (P / 'link3').resolve()
20072010
self.assertEqual(p, P)
2008-
self.assertEqual(str(p), BASE)
2011+
self.assertEqualNormCase(str(p), BASE)
20092012

20102013
# Resolve relative paths
20112014
old_path = os.getcwd()
20122015
os.chdir(BASE)
20132016
try:
20142017
p = self.cls('link0').resolve()
20152018
self.assertEqual(p, P)
2016-
self.assertEqual(str(p), BASE)
2019+
self.assertEqualNormCase(str(p), BASE)
20172020
p = self.cls('link1').resolve()
20182021
self.assertEqual(p, P)
2019-
self.assertEqual(str(p), BASE)
2022+
self.assertEqualNormCase(str(p), BASE)
20202023
p = self.cls('link2').resolve()
20212024
self.assertEqual(p, P)
2022-
self.assertEqual(str(p), BASE)
2025+
self.assertEqualNormCase(str(p), BASE)
20232026
p = self.cls('link3').resolve()
20242027
self.assertEqual(p, P)
2025-
self.assertEqual(str(p), BASE)
2028+
self.assertEqualNormCase(str(p), BASE)
20262029
finally:
20272030
os.chdir(old_path)
20282031

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix case sensitivity in test_pathlib and test_ntpath.

0 commit comments

Comments
 (0)