summaryrefslogtreecommitdiff
path: root/tests/utils_tests/test_archive.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /tests/utils_tests/test_archive.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/utils_tests/test_archive.py')
-rw-r--r--tests/utils_tests/test_archive.py52
1 files changed, 29 insertions, 23 deletions
diff --git a/tests/utils_tests/test_archive.py b/tests/utils_tests/test_archive.py
index 10f8e56198..8cd107063f 100644
--- a/tests/utils_tests/test_archive.py
+++ b/tests/utils_tests/test_archive.py
@@ -10,21 +10,22 @@ from django.utils import archive
try:
import bz2 # NOQA
+
HAS_BZ2 = True
except ImportError:
HAS_BZ2 = False
try:
import lzma # NOQA
+
HAS_LZMA = True
except ImportError:
HAS_LZMA = False
class TestArchive(unittest.TestCase):
-
def setUp(self):
- self.testdir = os.path.join(os.path.dirname(__file__), 'archives')
+ self.testdir = os.path.join(os.path.dirname(__file__), "archives")
self.old_cwd = os.getcwd()
os.chdir(self.testdir)
@@ -35,20 +36,25 @@ class TestArchive(unittest.TestCase):
with os.scandir(self.testdir) as entries:
for entry in entries:
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
- if (
- (entry.name.endswith('.bz2') and not HAS_BZ2) or
- (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA)
+ if (entry.name.endswith(".bz2") and not HAS_BZ2) or (
+ entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA
):
continue
archive.extract(entry.path, tmpdir)
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, '1')))
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, '2')))
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '1')))
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', '2')))
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '1')))
- self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'foo', 'bar', '2')))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, "1")))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, "2")))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "1")))
+ self.assertTrue(os.path.isfile(os.path.join(tmpdir, "foo", "2")))
+ self.assertTrue(
+ os.path.isfile(os.path.join(tmpdir, "foo", "bar", "1"))
+ )
+ self.assertTrue(
+ os.path.isfile(os.path.join(tmpdir, "foo", "bar", "2"))
+ )
- @unittest.skipIf(sys.platform == 'win32', 'Python on Windows has a limited os.chmod().')
+ @unittest.skipIf(
+ sys.platform == "win32", "Python on Windows has a limited os.chmod()."
+ )
def test_extract_file_permissions(self):
"""archive.extract() preserves file permissions."""
mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
@@ -57,33 +63,33 @@ class TestArchive(unittest.TestCase):
with os.scandir(self.testdir) as entries:
for entry in entries:
if (
- entry.name.startswith('leadpath_') or
- (entry.name.endswith('.bz2') and not HAS_BZ2) or
- (entry.name.endswith(('.lzma', '.xz')) and not HAS_LZMA)
+ entry.name.startswith("leadpath_")
+ or (entry.name.endswith(".bz2") and not HAS_BZ2)
+ or (entry.name.endswith((".lzma", ".xz")) and not HAS_LZMA)
):
continue
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
archive.extract(entry.path, tmpdir)
# An executable file in the archive has executable
# permissions.
- filepath = os.path.join(tmpdir, 'executable')
+ filepath = os.path.join(tmpdir, "executable")
self.assertEqual(os.stat(filepath).st_mode & mask, 0o775)
# A file is readable even if permission data is missing.
- filepath = os.path.join(tmpdir, 'no_permissions')
+ filepath = os.path.join(tmpdir, "no_permissions")
self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask)
class TestArchiveInvalid(SimpleTestCase):
def test_extract_function_traversal(self):
- archives_dir = os.path.join(os.path.dirname(__file__), 'traversal_archives')
+ archives_dir = os.path.join(os.path.dirname(__file__), "traversal_archives")
tests = [
- ('traversal.tar', '..'),
- ('traversal_absolute.tar', '/tmp/evil.py'),
+ ("traversal.tar", ".."),
+ ("traversal_absolute.tar", "/tmp/evil.py"),
]
- if sys.platform == 'win32':
+ if sys.platform == "win32":
tests += [
- ('traversal_disk_win.tar', 'd:evil.py'),
- ('traversal_disk_win.zip', 'd:evil.py'),
+ ("traversal_disk_win.tar", "d:evil.py"),
+ ("traversal_disk_win.zip", "d:evil.py"),
]
msg = "Archive contains invalid path: '%s'"
for entry, invalid_path in tests: