Skip to content

Commit acaacc4

Browse files
authored
Add test for shutil.copy() in test_shutil.py to verify str return type with Path arguments
Adds a unit test to test_shutil.py that ensures shutil.copy() returns a str even when given pathlib.Path objects as arguments. This addresses the inconsistent behavior where shutil.copy() could return a Path when the destination was a Path object. The test fails on the original implementation and passes with the fix introduced in this PR. This regression test helps enforce consistent return types for shutil functions.
1 parent afbf1fa commit acaacc4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Lib/test/test_shutil.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from test import support
3232
from test.support import os_helper
3333
from test.support.os_helper import TESTFN, FakePath
34+
from pathlib import Path
3435

3536
TESTFN2 = TESTFN + "2"
3637
TESTFN_SRC = TESTFN + "_SRC"
@@ -3470,6 +3471,25 @@ def test_fallback(self):
34703471
self.assertEqual(size.columns, 30)
34713472
self.assertEqual(size.lines, 40)
34723473

3474+
class TestCopyReturnType(unittest.TestCase):
3475+
def setUp(self):
3476+
self.tempdir = tempfile.TemporaryDirectory()
3477+
self.src = Path(self.tempdir.name) / "src.txt"
3478+
self.dst_file = Path(self.tempdir.name) / "dst.txt"
3479+
self.dst_dir = Path(self.tempdir.name) / "dst_dir"
3480+
self.src.write_text("hello")
3481+
self.dst_dir.mkdir()
3482+
3483+
def tearDown(self):
3484+
self.tempdir.cleanup()
3485+
3486+
def test_copy_returns_str_to_file(self):
3487+
result = shutil.copy(self.src, self.dst_file)
3488+
self.assertIsInstance(result, str)
3489+
3490+
def test_copy_returns_str_to_dir(self):
3491+
result = shutil.copy(self.src, self.dst_dir)
3492+
self.assertIsInstance(result,str)
34733493

34743494
class PublicAPITests(unittest.TestCase):
34753495
"""Ensures that the correct values are exposed in the public API."""

0 commit comments

Comments
 (0)