summaryrefslogtreecommitdiff
path: root/setuptools/_distutils/tests/test_archive_util.py
diff options
context:
space:
mode:
Diffstat (limited to 'setuptools/_distutils/tests/test_archive_util.py')
-rw-r--r--setuptools/_distutils/tests/test_archive_util.py265
1 files changed, 129 insertions, 136 deletions
diff --git a/setuptools/_distutils/tests/test_archive_util.py b/setuptools/_distutils/tests/test_archive_util.py
index 800b9018..7778c3ad 100644
--- a/setuptools/_distutils/tests/test_archive_util.py
+++ b/setuptools/_distutils/tests/test_archive_util.py
@@ -1,47 +1,32 @@
-# -*- coding: utf-8 -*-
"""Tests for distutils.archive_util."""
-import unittest
import os
import sys
import tarfile
from os.path import splitdrive
import warnings
+import functools
+import operator
+import pathlib
+
+import pytest
+import path
from distutils import archive_util
-from distutils.archive_util import (check_archive_formats, make_tarball,
- make_zipfile, make_archive,
- ARCHIVE_FORMATS)
-from distutils.spawn import find_executable, spawn
+from distutils.archive_util import (
+ check_archive_formats,
+ make_tarball,
+ make_zipfile,
+ make_archive,
+ ARCHIVE_FORMATS,
+)
+from distutils.spawn import spawn
from distutils.tests import support
-from test.support import run_unittest, patch
+from test.support import patch
from .unix_compat import require_unix_id, require_uid_0, grp, pwd, UID_0_SUPPORT
-from .py38compat import change_cwd
from .py38compat import check_warnings
-try:
- import zipfile
- ZIP_SUPPORT = True
-except ImportError:
- ZIP_SUPPORT = find_executable('zip')
-
-try:
- import zlib
- ZLIB_SUPPORT = True
-except ImportError:
- ZLIB_SUPPORT = False
-
-try:
- import bz2
-except ImportError:
- bz2 = None
-
-try:
- import lzma
-except ImportError:
- lzma = None
-
def can_fs_encode(filename):
"""
Return True if the filename can be saved in the file system.
@@ -55,11 +40,16 @@ def can_fs_encode(filename):
return True
-class ArchiveUtilTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+def all_equal(values):
+ return functools.reduce(operator.eq, values)
+
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+def same_drive(*paths):
+ return all_equal(pathlib.Path(path).drive for path in paths)
+
+
+class ArchiveUtilTestCase(support.TempdirManager):
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_tarball(self, name='archive'):
# creating something to tar
tmpdir = self._create_files()
@@ -67,53 +57,51 @@ class ArchiveUtilTestCase(support.TempdirManager,
# trying an uncompressed one
self._make_tarball(tmpdir, name, '.tar', compress=None)
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_tarball_gzip(self):
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')
- @unittest.skipUnless(bz2, 'Need bz2 support to run')
def test_make_tarball_bzip2(self):
+ pytest.importorskip('bz2')
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2')
- @unittest.skipUnless(lzma, 'Need lzma support to run')
def test_make_tarball_xz(self):
+ pytest.importorskip('lzma')
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz')
- @unittest.skipUnless(can_fs_encode('årchiv'),
- 'File system cannot handle this filename')
+ @pytest.mark.skipif("not can_fs_encode('årchiv')")
def test_make_tarball_latin1(self):
"""
Mirror test_make_tarball, except filename contains latin characters.
"""
- self.test_make_tarball('årchiv') # note this isn't a real word
+ self.test_make_tarball('årchiv') # note this isn't a real word
- @unittest.skipUnless(can_fs_encode('のアーカイブ'),
- 'File system cannot handle this filename')
+ @pytest.mark.skipif("not can_fs_encode('のアーカイブ')")
def test_make_tarball_extended(self):
"""
Mirror test_make_tarball, except filename contains extended
characters outside the latin charset.
"""
- self.test_make_tarball('のアーカイブ') # japanese for archive
+ self.test_make_tarball('のアーカイブ') # japanese for archive
def _make_tarball(self, tmpdir, target_name, suffix, **kwargs):
tmpdir2 = self.mkdtemp()
- unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
- "source and target should be on same drive")
+ if same_drive(tmpdir, tmpdir2):
+ pytest.skip("source and target should be on same drive")
base_name = os.path.join(tmpdir2, target_name)
# working with relative paths to avoid tar warnings
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_tarball(splitdrive(base_name)[1], 'dist', **kwargs)
# check if the compressed tarball was created
tarball = base_name + suffix
- self.assertTrue(os.path.exists(tarball))
- self.assertEqual(self._tarinfo(tarball), self._created_files)
+ assert os.path.exists(tarball)
+ assert self._tarinfo(tarball) == self._created_files
def _tarinfo(self, path):
tar = tarfile.open(path)
@@ -124,8 +112,14 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
tar.close()
- _zip_created_files = ['dist/', 'dist/file1', 'dist/file2',
- 'dist/sub/', 'dist/sub/file3', 'dist/sub2/']
+ _zip_created_files = [
+ 'dist/',
+ 'dist/file1',
+ 'dist/file2',
+ 'dist/sub/',
+ 'dist/sub/file3',
+ 'dist/sub2/',
+ ]
_created_files = [p.rstrip('/') for p in _zip_created_files]
def _create_files(self):
@@ -140,11 +134,10 @@ class ArchiveUtilTestCase(support.TempdirManager,
os.mkdir(os.path.join(dist, 'sub2'))
return tmpdir
- @unittest.skipUnless(find_executable('tar') and find_executable('gzip')
- and ZLIB_SUPPORT,
- 'Need the tar, gzip and zlib command to run')
+ @pytest.mark.usefixtures('needs_zlib')
+ @pytest.mark.skipif("not (find_executable('tar') and find_executable('gzip'))")
def test_tarfile_vs_tar(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
tmpdir2 = self.mkdtemp()
base_name = os.path.join(tmpdir2, 'archive')
old_dir = os.getcwd()
@@ -156,7 +149,7 @@ class ArchiveUtilTestCase(support.TempdirManager,
# check if the compressed tarball was created
tarball = base_name + '.tar.gz'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
# now create another tarball using `tar`
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
@@ -170,10 +163,10 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
- self.assertTrue(os.path.exists(tarball2))
+ assert os.path.exists(tarball2)
# let's compare both tarballs
- self.assertEqual(self._tarinfo(tarball), self._created_files)
- self.assertEqual(self._tarinfo(tarball2), self._created_files)
+ assert self._tarinfo(tarball) == self._created_files
+ assert self._tarinfo(tarball2) == self._created_files
# trying an uncompressed one
base_name = os.path.join(tmpdir2, 'archive')
@@ -184,7 +177,7 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
# now for a dry_run
base_name = os.path.join(tmpdir2, 'archive')
@@ -195,15 +188,14 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
- @unittest.skipUnless(find_executable('compress'),
- 'The compress program is required')
+ @pytest.mark.skipif("not find_executable('compress')")
def test_compress_deprecated(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- # using compress and testing the PendingDeprecationWarning
+ # using compress and testing the DeprecationWarning
old_dir = os.getcwd()
os.chdir(tmpdir)
try:
@@ -213,8 +205,8 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar.Z'
- self.assertTrue(os.path.exists(tarball))
- self.assertEqual(len(w.warnings), 1)
+ assert os.path.exists(tarball)
+ assert len(w.warnings) == 1
# same test with dry_run
os.remove(tarball)
@@ -223,34 +215,34 @@ class ArchiveUtilTestCase(support.TempdirManager,
try:
with check_warnings() as w:
warnings.simplefilter("always")
- make_tarball(base_name, 'dist', compress='compress',
- dry_run=True)
+ make_tarball(base_name, 'dist', compress='compress', dry_run=True)
finally:
os.chdir(old_dir)
- self.assertFalse(os.path.exists(tarball))
- self.assertEqual(len(w.warnings), 1)
+ assert not os.path.exists(tarball)
+ assert len(w.warnings) == 1
- @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
- 'Need zip and zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_zipfile(self):
+ zipfile = pytest.importorskip('zipfile')
# creating something to tar
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_zipfile(base_name, 'dist')
# check if the compressed tarball was created
tarball = base_name + '.zip'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
with zipfile.ZipFile(tarball) as zf:
- self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
+ assert sorted(zf.namelist()) == self._zip_created_files
- @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
def test_make_zipfile_no_zlib(self):
+ zipfile = pytest.importorskip('zipfile')
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
called = []
zipfile_class = zipfile.ZipFile
+
def fake_zipfile(*a, **kw):
if kw.get('compression', None) == zipfile.ZIP_STORED:
called.append((a, kw))
@@ -261,75 +253,78 @@ class ArchiveUtilTestCase(support.TempdirManager,
# create something to tar and compress
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_zipfile(base_name, 'dist')
tarball = base_name + '.zip'
- self.assertEqual(called,
- [((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
- self.assertTrue(os.path.exists(tarball))
+ assert called == [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]
+ assert os.path.exists(tarball)
with zipfile.ZipFile(tarball) as zf:
- self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
+ assert sorted(zf.namelist()) == self._zip_created_files
def test_check_archive_formats(self):
- self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
- 'xxx')
- self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar',
- 'ztar', 'tar', 'zip']))
+ assert check_archive_formats(['gztar', 'xxx', 'zip']) == 'xxx'
+ assert (
+ check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])
+ is None
+ )
def test_make_archive(self):
tmpdir = self.mkdtemp()
base_name = os.path.join(tmpdir, 'archive')
- self.assertRaises(ValueError, make_archive, base_name, 'xxx')
+ with pytest.raises(ValueError):
+ make_archive(base_name, 'xxx')
def test_make_archive_cwd(self):
current_dir = os.getcwd()
+
def _breaks(*args, **kw):
raise RuntimeError()
+
ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
try:
try:
make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
- except:
+ except Exception:
pass
- self.assertEqual(os.getcwd(), current_dir)
+ assert os.getcwd() == current_dir
finally:
del ARCHIVE_FORMATS['xxx']
def test_make_archive_tar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'tar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_archive_gztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'gztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.gz')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.gz'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(bz2, 'Need bz2 support to run')
def test_make_archive_bztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ pytest.importorskip('bz2')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'bztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.bz2')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.bz2'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(lzma, 'Need xz support to run')
def test_make_archive_xztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ pytest.importorskip('lzma')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'xztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.xz')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.xz'
+ assert self._tarinfo(res) == self._created_files
def test_make_archive_owner_group(self):
# testing make_archive with owner and group, with various combinations
@@ -340,54 +335,52 @@ class ArchiveUtilTestCase(support.TempdirManager,
else:
group = owner = 'root'
- base_dir = self._create_files()
+ base_dir = self._create_files()
root_dir = self.mkdtemp()
- base_name = os.path.join(self.mkdtemp() , 'archive')
- res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
- group=group)
- self.assertTrue(os.path.exists(res))
+ base_name = os.path.join(self.mkdtemp(), 'archive')
+ res = make_archive(
+ base_name, 'zip', root_dir, base_dir, owner=owner, group=group
+ )
+ assert os.path.exists(res)
res = make_archive(base_name, 'zip', root_dir, base_dir)
- self.assertTrue(os.path.exists(res))
+ assert os.path.exists(res)
- res = make_archive(base_name, 'tar', root_dir, base_dir,
- owner=owner, group=group)
- self.assertTrue(os.path.exists(res))
+ res = make_archive(
+ base_name, 'tar', root_dir, base_dir, owner=owner, group=group
+ )
+ assert os.path.exists(res)
- res = make_archive(base_name, 'tar', root_dir, base_dir,
- owner='kjhkjhkjg', group='oihohoh')
- self.assertTrue(os.path.exists(res))
+ res = make_archive(
+ base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh'
+ )
+ assert os.path.exists(res)
- @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
+ @pytest.mark.usefixtures('needs_zlib')
@require_unix_id
@require_uid_0
def test_tarfile_root_owner(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
try:
- archive_name = make_tarball(base_name, 'dist', compress=None,
- owner=owner, group=group)
+ archive_name = make_tarball(
+ base_name, 'dist', compress=None, owner=owner, group=group
+ )
finally:
os.chdir(old_dir)
# check if the compressed tarball was created
- self.assertTrue(os.path.exists(archive_name))
+ assert os.path.exists(archive_name)
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
- self.assertEqual(member.uid, 0)
- self.assertEqual(member.gid, 0)
+ assert member.uid == 0
+ assert member.gid == 0
finally:
archive.close()
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(ArchiveUtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())