Skip to content

Commit c700c04

Browse files
committed
gh-143668: Add format-independent tests for make_archive path-like support
1 parent 6b86cac commit c700c04

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Lib/shutil.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
12161216

12171217
if base_dir is None:
12181218
base_dir = os.curdir
1219+
else:
1220+
base_dir = os.fspath(base_dir)
12191221

12201222
supports_root_dir = getattr(func, 'supports_root_dir', False)
12211223
save_cwd = None

Lib/test/test_shutil.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,23 +2042,31 @@ def test_make_zipfile_in_curdir(self):
20422042
self.assertEqual(make_archive('test', 'zip'), 'test.zip')
20432043
self.assertTrue(os.path.isfile('test.zip'))
20442044

2045-
@support.requires_zlib()
2046-
def test_make_archive_accepts_pathlike(self):
2047-
tmpdir = self.mkdtemp()
2048-
with os_helper.change_cwd(tmpdir), no_chdir:
2049-
# Test path-like base_name without root_dir and base_dir
2050-
base_name = FakePath(os.path.join(tmpdir, 'archive'))
2051-
res = make_archive(base_name, 'zip')
2052-
self.assertEqual(res, os.path.join(tmpdir, 'archive.zip'))
2053-
self.assertTrue(os.path.isfile(res))
2054-
2055-
# Test with path-like base_name, root_dir, and base_dir
2056-
root_dir, base_dir = self._create_files()
2057-
base_name = FakePath(os.path.join(tmpdir, 'archive2'))
2058-
res = make_archive(
2059-
base_name, 'zip', FakePath(root_dir), FakePath(base_dir))
2060-
self.assertEqual(res, os.path.join(tmpdir, 'archive2.zip'))
2061-
self.assertTrue(os.path.isfile(res))
2045+
def test_make_archive_pathlike_cwd_default(self):
2046+
called_args = []
2047+
def archiver(base_name, base_dir, **kw):
2048+
called_args.append((base_name, kw.get('root_dir')))
2049+
2050+
register_archive_format('xxx', archiver, [], 'xxx file')
2051+
self.addCleanup(unregister_archive_format, 'xxx')
2052+
with no_chdir:
2053+
make_archive(FakePath('basename'), 'xxx')
2054+
self.assertEqual(called_args, [('basename', None)])
2055+
2056+
def test_make_archive_pathlike_cwd_supports_root_dir(self):
2057+
root_dir = self.mkdtemp()
2058+
called_args = []
2059+
def archiver(base_name, base_dir, **kw):
2060+
called_args.append((base_name, base_dir, kw.get('root_dir')))
2061+
archiver.supports_root_dir = True
2062+
2063+
register_archive_format('xxx', archiver, [], 'xxx file')
2064+
self.addCleanup(unregister_archive_format, 'xxx')
2065+
with no_chdir:
2066+
make_archive(FakePath('basename'), 'xxx',
2067+
root_dir=FakePath(root_dir),
2068+
base_dir=FakePath('basedir'))
2069+
self.assertEqual(called_args, [('basename', 'basedir', root_dir)])
20622070

20632071
def test_register_archive_format(self):
20642072

0 commit comments

Comments
 (0)