Bug 1646427 - [mozfile] Convert 'test_extract.py' to the pytest format, r=gbrown
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 18 Jun 2020 19:25:53 +0000
changeset 536606 1bafbdf97b9b7f1cb58a4940546437a8e8d031c1
parent 536605 b323411b08ffef747cf9ef011f85b30021257c88
child 536607 97e16bc7c6041592170c4a45252593ecb866d3d5
push id37531
push usernbeleuzu@mozilla.com
push dateTue, 23 Jun 2020 03:44:39 +0000
treeherdermozilla-central@b1146cce5053 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs1646427
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1646427 - [mozfile] Convert 'test_extract.py' to the pytest format, r=gbrown Differential Revision: https://phabricator.services.mozilla.com/D80207
testing/mozbase/mozfile/tests/stubs.py
testing/mozbase/mozfile/tests/test_extract.py
--- a/testing/mozbase/mozfile/tests/stubs.py
+++ b/testing/mozbase/mozfile/tests/stubs.py
@@ -16,20 +16,20 @@ files = [('foo.txt',),
          ]
 
 
 def create_empty_stub():
     tempdir = tempfile.mkdtemp()
     return tempdir
 
 
-def create_stub():
+def create_stub(tempdir=None):
     """create a stub directory"""
 
-    tempdir = tempfile.mkdtemp()
+    tempdir = tempdir or tempfile.mkdtemp()
     try:
         for path in files:
             fullpath = os.path.join(tempdir, *path)
             dirname = os.path.dirname(fullpath)
             if not os.path.exists(dirname):
                 os.makedirs(dirname)
             contents = path[-1]
             f = open(fullpath, 'w')
--- a/testing/mozbase/mozfile/tests/test_extract.py
+++ b/testing/mozbase/mozfile/tests/test_extract.py
@@ -1,164 +1,123 @@
 #!/usr/bin/env python
 
 from __future__ import absolute_import, print_function
 
 import os
-import shutil
 import tarfile
 import tempfile
-import unittest
 import zipfile
 
 import mozunit
+import pytest
 
 import mozfile
 
 import stubs
 
 
-class TestExtract(unittest.TestCase):
-    """test extracting archives"""
+@pytest.fixture
+def ensure_directory_contents():
+    """ensure the directory contents match"""
 
-    def ensure_directory_contents(self, directory):
-        """ensure the directory contents match"""
+    def inner(directory):
         for f in stubs.files:
             path = os.path.join(directory, *f)
             exists = os.path.exists(path)
             if not exists:
                 print("%s does not exist" % (os.path.join(f)))
-            self.assertTrue(exists)
+            assert exists
             if exists:
                 contents = open(path).read().strip()
-                self.assertTrue(contents == f[-1])
+                assert contents == f[-1]
+
+    return inner
+
 
-    def test_extract_zipfile(self):
-        """test extracting a zipfile"""
-        _zipfile = self.create_zip()
-        self.assertTrue(os.path.exists(_zipfile))
-        try:
-            dest = tempfile.mkdtemp()
-            try:
-                mozfile.extract_zip(_zipfile, dest)
-                self.ensure_directory_contents(dest)
-            finally:
-                shutil.rmtree(dest)
-        finally:
-            os.remove(_zipfile)
+@pytest.fixture(scope="module")
+def tarpath(tmpdir_factory):
+    """create a stub tarball for testing"""
+    tmpdir = tmpdir_factory.mktemp("test_extract")
 
-    def test_extract_zipfile_missing_file_attributes(self):
-        """if files do not have attributes set the default permissions have to be inherited."""
-        _zipfile = os.path.join(os.path.dirname(__file__), 'files', 'missing_file_attributes.zip')
-        self.assertTrue(os.path.exists(_zipfile))
-        dest = tempfile.mkdtemp()
-        try:
-            # Get the default file permissions for the user
-            fname = os.path.join(dest, 'foo')
-            with open(fname, 'w'):
-                pass
-            default_stmode = os.stat(fname).st_mode
+    tempdir = tmpdir.join("stubs").strpath
+    stubs.create_stub(tempdir)
+    filename = tmpdir.join('bundle.tar').strpath
+    archive = tarfile.TarFile(filename, mode='w')
+    for path in stubs.files:
+        archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path))
+    archive.close()
 
-            files = mozfile.extract_zip(_zipfile, dest)
-            for filename in files:
-                self.assertEqual(os.stat(os.path.join(dest, filename)).st_mode,
-                                 default_stmode)
-        finally:
-            shutil.rmtree(dest)
+    assert os.path.exists(filename)
+    return filename
+
+
+@pytest.fixture(scope="module")
+def zippath(tmpdir_factory):
+    """create a stub zipfile for testing"""
+    tmpdir = tmpdir_factory.mktemp("test_extract")
 
-    def test_extract_tarball(self):
-        """test extracting a tarball"""
-        tarball = self.create_tarball()
-        self.assertTrue(os.path.exists(tarball))
-        try:
-            dest = tempfile.mkdtemp()
-            try:
-                mozfile.extract_tarball(tarball, dest)
-                self.ensure_directory_contents(dest)
-            finally:
-                shutil.rmtree(dest)
-        finally:
-            os.remove(tarball)
+    tempdir = tmpdir.join("stubs").strpath
+    stubs.create_stub(tempdir)
+    filename = tmpdir.join('bundle.zip').strpath
+    archive = zipfile.ZipFile(filename, mode='w')
+    for path in stubs.files:
+        archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path))
+    archive.close()
 
-    def test_extract(self):
-        """test the generalized extract function"""
+    assert os.path.exists(filename)
+    return filename
 
-        # test extracting a tarball
-        tarball = self.create_tarball()
-        self.assertTrue(os.path.exists(tarball))
-        try:
-            dest = tempfile.mkdtemp()
-            try:
-                mozfile.extract(tarball, dest)
-                self.ensure_directory_contents(dest)
-            finally:
-                shutil.rmtree(dest)
-        finally:
-            os.remove(tarball)
 
-        # test extracting a zipfile
-        _zipfile = self.create_zip()
-        self.assertTrue(os.path.exists(_zipfile))
-        try:
-            dest = tempfile.mkdtemp()
-            try:
-                mozfile.extract_zip(_zipfile, dest)
-                self.ensure_directory_contents(dest)
-            finally:
-                shutil.rmtree(dest)
-        finally:
-            os.remove(_zipfile)
+@pytest.fixture(scope="module", params=["tar", "zip"])
+def bundlepath(request, tarpath, zippath):
+    if request.param == "tar":
+        return tarpath
+    else:
+        return zippath
+
 
-        # test extracting some non-archive; this should fail
-        fd, filename = tempfile.mkstemp()
-        os.write(fd, b'This is not a zipfile or tarball')
-        os.close(fd)
-        exception = None
+def test_extract(tmpdir, bundlepath, ensure_directory_contents):
+    """test extracting a zipfile"""
+    dest = tmpdir.mkdir('dest').strpath
+    mozfile.extract(bundlepath, dest)
+    ensure_directory_contents(dest)
 
-        try:
-            dest = tempfile.mkdtemp()
-            mozfile.extract(filename, dest)
-        except Exception as exc:
-            exception = exc
-        finally:
-            os.remove(filename)
-            os.rmdir(dest)
 
-        self.assertTrue(isinstance(exception, Exception))
-
-    # utility functions
+def test_extract_zipfile_missing_file_attributes(tmpdir):
+    """if files do not have attributes set the default permissions have to be inherited."""
+    _zipfile = os.path.join(os.path.dirname(__file__), 'files', 'missing_file_attributes.zip')
+    assert os.path.exists(_zipfile)
+    dest = tmpdir.mkdir('dest').strpath
 
-    def create_tarball(self):
-        """create a stub tarball for testing"""
-        tempdir = stubs.create_stub()
-        filename = tempfile.mktemp(suffix='.tar')
-        archive = tarfile.TarFile(filename, mode='w')
-        try:
-            for path in stubs.files:
-                archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path))
-        except BaseException:
-            os.remove(archive)
-            raise
-        finally:
-            shutil.rmtree(tempdir)
-        archive.close()
-        return filename
+    # Get the default file permissions for the user
+    fname = os.path.join(dest, 'foo')
+    with open(fname, 'w'):
+        pass
+    default_stmode = os.stat(fname).st_mode
+
+    files = mozfile.extract_zip(_zipfile, dest)
+    for filename in files:
+        assert os.stat(os.path.join(dest, filename)).st_mode == default_stmode
+
 
-    def create_zip(self):
-        """create a stub zipfile for testing"""
+def test_extract_non_archive(tarpath, zippath):
+    """test the generalized extract function"""
+    # test extracting some non-archive; this should fail
+    fd, filename = tempfile.mkstemp()
+    os.write(fd, b'This is not a zipfile or tarball')
+    os.close(fd)
+    exception = None
 
-        tempdir = stubs.create_stub()
-        filename = tempfile.mktemp(suffix='.zip')
-        archive = zipfile.ZipFile(filename, mode='w')
-        try:
-            for path in stubs.files:
-                archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path))
-        except BaseException:
-            os.remove(filename)
-            raise
-        finally:
-            shutil.rmtree(tempdir)
-        archive.close()
-        return filename
+    try:
+        dest = tempfile.mkdtemp()
+        mozfile.extract(filename, dest)
+    except Exception as exc:
+        exception = exc
+    finally:
+        os.remove(filename)
+        os.rmdir(dest)
+
+    assert isinstance(exception, Exception)
 
 
 if __name__ == '__main__':
     mozunit.main()