From 1a158caddf25b7ea615702388d660e3c5dccb29a Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Sat, 9 Dec 2023 21:28:05 +0100 Subject: [PATCH 1/2] gh-109980: Fix test_tarfile_vs_tar on macOS On recentish macOS versions the system tar command includes system metadata (ACLs, extended attributes and resource forks) in the tar archive, which shutil.make_archive will not do. This can cause spurious test failures. --- Lib/test/test_shutil.py | 14 ++++++++++++++ .../2023-12-09-21-27-46.gh-issue-109980.y--500.rst | 2 ++ 2 files changed, 16 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2023-12-09-21-27-46.gh-issue-109980.y--500.rst diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index d7061b2f9d8724..1314f7f33f7a91 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1660,6 +1660,7 @@ def _create_files(self, base_dir='dist'): def test_tarfile_vs_tar(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') + with no_chdir: tarball = make_archive(base_name, 'gztar', root_dir, base_dir) @@ -1670,9 +1671,22 @@ def test_tarfile_vs_tar(self): # now create another tarball using `tar` tarball2 = os.path.join(root_dir, 'archive2.tar') tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir] + if sys.platform == 'darwin': + # macOS tar can include extended attributes, + # ACLs and other mac specific metadata into the + # archive (an recentish version of the OS). + # + # This feature can be disabled with the + # '--no-mac-metadata' option on macOS 11 or + # later. + import platform + if int(platform.mac_ver()[0].split('.')[0]) >= 11: + tar_cmd.insert(1, '--no-mac-metadata') subprocess.check_call(tar_cmd, cwd=root_dir, stdout=subprocess.DEVNULL) + + self.assertTrue(os.path.isfile(tarball2)) # let's compare both tarballs self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) diff --git a/Misc/NEWS.d/next/Tests/2023-12-09-21-27-46.gh-issue-109980.y--500.rst b/Misc/NEWS.d/next/Tests/2023-12-09-21-27-46.gh-issue-109980.y--500.rst new file mode 100644 index 00000000000000..c475a33919db98 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-12-09-21-27-46.gh-issue-109980.y--500.rst @@ -0,0 +1,2 @@ +Fix ``test_tarfile_vs_tar`` in ``test_shutil`` for macOS, where system tar +can include more information in the archive than :mod:`shutil.make_archive`. From c8a421eea7c3fb1a78c122daae07ea4ac8db93d7 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Sat, 9 Dec 2023 21:31:36 +0100 Subject: [PATCH 2/2] revert accidental changes --- Lib/test/test_shutil.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 1314f7f33f7a91..b29d316352f219 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -1660,7 +1660,6 @@ def _create_files(self, base_dir='dist'): def test_tarfile_vs_tar(self): root_dir, base_dir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') - with no_chdir: tarball = make_archive(base_name, 'gztar', root_dir, base_dir) @@ -1685,8 +1684,6 @@ def test_tarfile_vs_tar(self): subprocess.check_call(tar_cmd, cwd=root_dir, stdout=subprocess.DEVNULL) - - self.assertTrue(os.path.isfile(tarball2)) # let's compare both tarballs self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))