Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review
  • Loading branch information
6t8k committed Jan 30, 2023
commit e9a0118d842e72ca48b20cf7783eeb608011311a
2 changes: 1 addition & 1 deletion Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
if root_dir is not None:
stmd = os.stat(root_dir).st_mode
if not stat.S_ISDIR(stmd):
raise NotADirectoryError(root_dir)
raise NotADirectoryError(errno.ENOTDIR, 'Not a directory', root_dir)

if supports_root_dir:
# Support path-like base_name here for backwards-compatibility.
Expand Down
34 changes: 14 additions & 20 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,47 +1665,41 @@ def test_register_archive_format(self):
formats = [name for name, params in get_archive_formats()]
self.assertNotIn('xxx', formats)

def _unlink_existing_file(self, path):
try:
os.unlink(path)
except FileNotFoundError:
pass

def test_make_tarfile_rootdir_nodir(self):
# GH-99203
self.addCleanup(self._unlink_existing_file, f'{TESTFN}.tar')
for dry_run in (0, True):
self.addCleanup(os_helper.unlink, f'{TESTFN}.tar')
for dry_run in (False, True):
with self.subTest(dry_run=dry_run):
tmp_fd, tmp_file = tempfile.mkstemp(dir=self.mkdtemp())
Comment thread
6t8k marked this conversation as resolved.
Outdated
os.close(tmp_fd)
with self.assertRaises(NotADirectoryError):
with self.assertRaises(NotADirectoryError) as cm:
make_archive(TESTFN, 'tar', tmp_file, dry_run=dry_run)
self.assertEqual(cm.exception.errno, errno.ENOTDIR)
self.assertEqual(cm.exception.filename, tmp_file)
self.assertFalse(os.path.exists(f'{TESTFN}.tar'))

tmp_fd, tmp_file = tempfile.mkstemp(dir=self.mkdtemp())
os.close(tmp_fd)
os.unlink(tmp_file)
nonexisting_file = os.path.join(self.mkdtemp(), 'nonexisting')
with self.assertRaises(FileNotFoundError):
make_archive(TESTFN, 'tar', tmp_file, dry_run=dry_run)
make_archive(TESTFN, 'tar', nonexisting_file, dry_run=dry_run)
self.assertFalse(os.path.exists(f'{TESTFN}.tar'))

@support.requires_zlib()
def test_make_zipfile_rootdir_nodir(self):
# GH-99203
self.addCleanup(self._unlink_existing_file, f'{TESTFN}.zip')
for dry_run in (0, True):
self.addCleanup(os_helper.unlink, f'{TESTFN}.zip')
for dry_run in (False, True):
with self.subTest(dry_run=dry_run):
tmp_fd, tmp_file = tempfile.mkstemp(dir=self.mkdtemp())
os.close(tmp_fd)
with self.assertRaises(NotADirectoryError):
with self.assertRaises(NotADirectoryError) as cm:
make_archive(TESTFN, 'zip', tmp_file, dry_run=dry_run)
self.assertEqual(cm.exception.errno, errno.ENOTDIR)
self.assertEqual(cm.exception.filename, tmp_file)
self.assertFalse(os.path.exists(f'{TESTFN}.zip'))

tmp_fd, tmp_file = tempfile.mkstemp(dir=self.mkdtemp())
os.close(tmp_fd)
os.unlink(tmp_file)
nonexisting_file = os.path.join(self.mkdtemp(), 'nonexisting')
with self.assertRaises(FileNotFoundError):
make_archive(TESTFN, 'zip', tmp_file, dry_run=dry_run)
make_archive(TESTFN, 'zip', nonexisting_file, dry_run=dry_run)
self.assertFalse(os.path.exists(f'{TESTFN}.zip'))

### shutil.unpack_archive
Expand Down