Skip to content

Commit 8f74918

Browse files
committed
Test coverage for filecmp: use exact filepaths in tests
1 parent f9f7b06 commit 8f74918

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

‎Lib/test/test_filecmp.py‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ def test_cache_overflow_and_hit(self):
8484
filecmp.clear_cache()
8585
s1 = filecmp._sig(os.stat(self.name))
8686
s2 = filecmp._sig(os.stat(self.name_same))
87+
base_dir = os.path.dirname(self.name)
8788
for i in range(105):
88-
fake_path2 = f"fake_path_{i}"
89+
fake_path2 = os.path.join(base_dir, f"fake_path_{i}")
8990
filecmp._cache[self.name, fake_path2, s1, s2] = True
9091

9192
self.assertGreater(len(filecmp._cache), 100)
@@ -434,16 +435,20 @@ def test_dircmp_os_errors_in_processing(self):
434435
for d in (self.dir, self.dir_same):
435436
Path(d, trigger_file).write_text('trigger', encoding='utf-8')
436437

438+
expected_path1 = os.path.normcase(os.path.abspath(os.path.join(self.dir, trigger_file)))
439+
expected_path2 = os.path.normcase(os.path.abspath(os.path.join(self.dir_same, trigger_file)))
440+
437441
original_stat = os.stat
438442

439443
def fake_stat(path, *args, **kwargs):
440-
if trigger_file in str(path):
444+
current_path = os.path.normcase(os.path.abspath(os.fspath(path)))
445+
if current_path == expected_path1 or current_path == expected_path2:
441446
raise OSError
442447
return original_stat(path, *args, **kwargs)
443448

444449
with patch("os.stat", side_effect=fake_stat):
445450
dc = filecmp.dircmp(self.dir, self.dir_same)
446-
self.assertIn(trigger_file, dc.common_funny)
451+
self.assertIn(trigger_file, dc.common_funny)
447452

448453
# Test common_funny layout with regular files, directories, and sockets.
449454
# 'diff_type.txt' is a directory in one path and a regular file in another.
@@ -456,19 +461,24 @@ def test_dircmp_different_types_and_funny_types(self):
456461
for f in (file_diff_type, file_funny_type):
457462
Path(d, f).write_text("data")
458463

464+
diff_path1 = os.path.normcase(os.path.abspath(os.path.join(self.dir, file_diff_type)))
465+
diff_path2 = os.path.normcase(os.path.abspath(os.path.join(self.dir_same, file_diff_type)))
466+
funny_path1 = os.path.normcase(os.path.abspath(os.path.join(self.dir, file_funny_type)))
467+
funny_path2 = os.path.normcase(os.path.abspath(os.path.join(self.dir_same, file_funny_type)))
468+
459469
original_stat = os.stat
460470

461471
def fake_stat_types(path, *args, **kwargs):
462472
st = original_stat(path, *args, **kwargs)
463-
path_str = str(path)
473+
current_path = os.path.normcase(os.path.abspath(os.fspath(path)))
464474

465-
if file_diff_type in path_str:
466-
is_dir_same = "dir-same" in path_str
475+
if current_path == diff_path1 or current_path == diff_path2:
476+
is_dir_same = (current_path == diff_path2)
467477
target_type = stat.S_IFREG if is_dir_same else stat.S_IFDIR
468478
new_mode = (st.st_mode & ~stat.S_IFREG) | target_type
469479
return os.stat_result((new_mode,) + st[1:])
470480

471-
if file_funny_type in path_str:
481+
if current_path == funny_path1 or current_path == funny_path2:
472482
new_mode = (st.st_mode & ~stat.S_IFREG) | stat.S_IFSOCK
473483
return os.stat_result((new_mode,) + st[1:])
474484

@@ -495,18 +505,26 @@ def test_dircmp_closure_and_report_funnies(self):
495505
for f in (funny_trigger, funny_file_trigger):
496506
Path(d, f).write_text('data', encoding='utf-8')
497507

508+
trigger_path1 = os.path.normcase(os.path.abspath(os.path.join(self.dir, funny_trigger)))
509+
trigger_path2 = os.path.normcase(os.path.abspath(os.path.join(self.dir_same, funny_trigger)))
510+
cmp_path1 = os.path.normcase(os.path.abspath(os.path.join(self.dir, funny_file_trigger)))
511+
cmp_path2 = os.path.normcase(os.path.abspath(os.path.join(self.dir_same, funny_file_trigger)))
512+
498513
original_stat = os.stat
499514
original_cmp = filecmp._cmp
500515

501516
def fake_stat_for_report(path, *args, **kwargs):
502-
if funny_trigger in str(path):
517+
current_path = os.path.normcase(os.path.abspath(os.fspath(path)))
518+
if current_path == trigger_path1 or current_path == trigger_path2:
503519
st = original_stat(path, *args, **kwargs)
504520
new_mode = (st.st_mode & ~stat.S_IFREG) | stat.S_IFSOCK
505521
return os.stat_result((new_mode,) + st[1:])
506522
return original_stat(path, *args, **kwargs)
507523

508524
def fake_private_cmp(a, b, sh):
509-
if funny_file_trigger in str(a) or funny_file_trigger in str(b):
525+
norm_a = os.path.normcase(os.path.abspath(os.fspath(a)))
526+
norm_b = os.path.normcase(os.path.abspath(os.fspath(b)))
527+
if norm_a in (cmp_path1, cmp_path2) or norm_b in (cmp_path1, cmp_path2):
510528
return 2
511529
return original_cmp(a, b, sh)
512530

0 commit comments

Comments
 (0)