gh-74696: Pass root_dir to custom archivers which support it (GH-94251)

Co-authored-by: Éric <merwok@netwok.org>
This commit is contained in:
Serhiy Storchaka 2022-10-05 12:48:59 +03:00 committed by GitHub
parent 4b83cd0b22
commit e3ef400be7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 17 deletions

View file

@ -1568,28 +1568,65 @@ class TestArchives(BaseTest, unittest.TestCase):
finally:
archive.close()
def test_make_archive_cwd_default(self):
current_dir = os.getcwd()
def archiver(base_name, base_dir, **kw):
self.assertNotIn('root_dir', kw)
self.assertEqual(base_name, 'basename')
self.assertEqual(os.getcwd(), current_dir)
raise RuntimeError()
register_archive_format('xxx', archiver, [], 'xxx file')
try:
with no_chdir:
with self.assertRaises(RuntimeError):
make_archive('basename', 'xxx')
self.assertEqual(os.getcwd(), current_dir)
finally:
unregister_archive_format('xxx')
def test_make_archive_cwd(self):
current_dir = os.getcwd()
root_dir = self.mkdtemp()
def _breaks(*args, **kw):
def archiver(base_name, base_dir, **kw):
self.assertNotIn('root_dir', kw)
self.assertEqual(base_name, os.path.join(current_dir, 'basename'))
self.assertEqual(os.getcwd(), root_dir)
raise RuntimeError()
dirs = []
def _chdir(path):
dirs.append(path)
orig_chdir(path)
register_archive_format('xxx', _breaks, [], 'xxx file')
register_archive_format('xxx', archiver, [], 'xxx file')
try:
with support.swap_attr(os, 'chdir', _chdir) as orig_chdir:
try:
make_archive('xxx', 'xxx', root_dir=root_dir)
except Exception:
pass
with self.assertRaises(RuntimeError):
make_archive('basename', 'xxx', root_dir=root_dir)
self.assertEqual(os.getcwd(), current_dir)
self.assertEqual(dirs, [root_dir, current_dir])
finally:
unregister_archive_format('xxx')
def test_make_archive_cwd_supports_root_dir(self):
current_dir = os.getcwd()
root_dir = self.mkdtemp()
def archiver(base_name, base_dir, **kw):
self.assertEqual(base_name, 'basename')
self.assertEqual(kw['root_dir'], root_dir)
self.assertEqual(os.getcwd(), current_dir)
raise RuntimeError()
archiver.supports_root_dir = True
register_archive_format('xxx', archiver, [], 'xxx file')
try:
with no_chdir:
with self.assertRaises(RuntimeError):
make_archive('basename', 'xxx', root_dir=root_dir)
self.assertEqual(os.getcwd(), current_dir)
finally:
unregister_archive_format('xxx')
def test_make_tarfile_in_curdir(self):
# Issue #21280
root_dir = self.mkdtemp()