2011-12-23 06:38:02 +08:00
|
|
|
import os
|
2016-12-02 00:05:08 +08:00
|
|
|
import stat
|
|
|
|
import sys
|
2011-12-23 06:38:02 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2019-02-16 07:59:51 +08:00
|
|
|
from django.utils import archive
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
|
2019-02-16 07:59:51 +08:00
|
|
|
class TestArchive(unittest.TestCase):
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2019-02-16 07:59:51 +08:00
|
|
|
self.testdir = os.path.join(os.path.dirname(__file__), 'archives')
|
2011-12-23 06:38:02 +08:00
|
|
|
self.old_cwd = os.getcwd()
|
2019-02-16 07:59:51 +08:00
|
|
|
os.chdir(self.testdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.old_cwd)
|
|
|
|
|
|
|
|
def test_extract_function(self):
|
2019-02-16 07:59:51 +08:00
|
|
|
for entry in os.scandir(self.testdir):
|
|
|
|
with self.subTest(entry.name), tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
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')))
|
2014-05-22 20:12:22 +08:00
|
|
|
|
2016-12-02 00:05:08 +08:00
|
|
|
@unittest.skipIf(sys.platform == 'win32', 'Python on Windows has a limited os.chmod().')
|
|
|
|
def test_extract_file_permissions(self):
|
2019-02-16 07:59:51 +08:00
|
|
|
"""archive.extract() preserves file permissions."""
|
|
|
|
mask = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
|
|
|
|
umask = os.umask(0)
|
|
|
|
os.umask(umask) # Restore the original umask.
|
|
|
|
for entry in os.scandir(self.testdir):
|
|
|
|
if entry.name.startswith('leadpath_'):
|
|
|
|
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')
|
|
|
|
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')
|
2020-06-29 13:51:43 +08:00
|
|
|
self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask)
|