summaryrefslogtreecommitdiff
path: root/tests/utils_tests
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2021-06-05 23:56:34 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-06-07 06:52:42 +0200
commit7272e1963ffdf39c1d4fe225d5425a45dd095d11 (patch)
tree89b3ee03deeeee46f049e2720b2a3254aa93761a /tests/utils_tests
parent7dd502b0e1e7d3e7fa2c5a2d54bbc926dbcf5f92 (diff)
Fixed #32821 -- Updated os.scandir() uses to use a context manager.
Diffstat (limited to 'tests/utils_tests')
-rw-r--r--tests/utils_tests/test_archive.py61
1 files changed, 32 insertions, 29 deletions
diff --git a/tests/utils_tests/test_archive.py b/tests/utils_tests/test_archive.py
index 4eb47d2fad..10f8e56198 100644
--- a/tests/utils_tests/test_archive.py
+++ b/tests/utils_tests/test_archive.py
@@ -32,20 +32,21 @@ class TestArchive(unittest.TestCase):
os.chdir(self.old_cwd)
def test_extract_function(self):
- for entry in os.scandir(self.testdir):
- 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)
- ):
- 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')))
+ 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)
+ ):
+ 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')))
@unittest.skipIf(sys.platform == 'win32', 'Python on Windows has a limited os.chmod().')
def test_extract_file_permissions(self):
@@ -53,21 +54,23 @@ class TestArchive(unittest.TestCase):
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_') 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')
- 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')
- self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask)
+ 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)
+ ):
+ 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')
+ self.assertEqual(os.stat(filepath).st_mode & mask, 0o666 & ~umask)
class TestArchiveInvalid(SimpleTestCase):