Bug 910660 - Add a test for the unpacker code. r=gps, a=sledru
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 13 Mar 2015 18:21:53 +0900
changeset 258294 06cd1b5deb25
parent 258293 a141c675b405
child 258295 90925932c88c
push id4638
push userryanvm@gmail.com
push date2015-04-06 20:41 +0000
treeherdermozilla-beta@499e1c563939 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps, sledru
bugs910660
milestone38.0
Bug 910660 - Add a test for the unpacker code. r=gps, a=sledru
python/moz.build
python/mozbuild/mozpack/packager/unpack.py
python/mozbuild/mozpack/test/test_packager_formats.py
python/mozbuild/mozpack/test/test_packager_unpack.py
--- a/python/moz.build
+++ b/python/moz.build
@@ -63,11 +63,12 @@ PYTHON_UNIT_TESTS += [
     'mozbuild/mozpack/test/test_copier.py',
     'mozbuild/mozpack/test/test_errors.py',
     'mozbuild/mozpack/test/test_files.py',
     'mozbuild/mozpack/test/test_manifests.py',
     'mozbuild/mozpack/test/test_mozjar.py',
     'mozbuild/mozpack/test/test_packager.py',
     'mozbuild/mozpack/test/test_packager_formats.py',
     'mozbuild/mozpack/test/test_packager_l10n.py',
+    'mozbuild/mozpack/test/test_packager_unpack.py',
     'mozbuild/mozpack/test/test_path.py',
     'mozbuild/mozpack/test/test_unify.py',
 ]
--- a/python/mozbuild/mozpack/packager/unpack.py
+++ b/python/mozbuild/mozpack/packager/unpack.py
@@ -155,20 +155,29 @@ class UnpackFinder(FileFinder):
         base = entry.base
         jar, relpath = urlparse(relpath).path.split('!', 1)
         entry = entry.rebase(mozpack.path.join(base, 'jar:%s!' % jar)) \
             .move(mozpack.path.join(base, mozpack.path.splitext(jar)[0])) \
             .rebase(base)
         return mozpack.path.join(base, jar), entry
 
 
+def unpack_to_registry(source, registry):
+    '''
+    Transform a jar chrome or omnijar packaged directory into a flat package.
+
+    The given registry is filled with the flat package.
+    '''
+    finder = UnpackFinder(source)
+    packager = SimplePackager(FlatFormatter(registry))
+    for p, f in finder.find('*'):
+        if mozpack.path.split(p)[0] not in STARTUP_CACHE_PATHS:
+            packager.add(p, f)
+    packager.close()
+
+
 def unpack(source):
     '''
     Transform a jar chrome or omnijar packaged directory into a flat package.
     '''
     copier = FileCopier()
-    finder = UnpackFinder(source)
-    packager = SimplePackager(FlatFormatter(copier))
-    for p, f in finder.find('*'):
-        if mozpack.path.split(p)[0] not in STARTUP_CACHE_PATHS:
-            packager.add(p, f)
-    packager.close()
+    unpack_to_registry(source, copier)
     copier.copy(source, skip_if_older=False)
--- a/python/mozbuild/mozpack/test/test_packager_formats.py
+++ b/python/mozbuild/mozpack/test/test_packager_formats.py
@@ -68,26 +68,26 @@ def fill_formatter(formatter, contents):
         if k in ('bases', 'manifests'):
             continue
         if k.endswith('.xpt'):
             formatter.add_interfaces(k, v)
         else:
             formatter.add(k, v)
 
 
-def get_contents(registry):
+def get_contents(registry, read_all=False):
     result = {}
     for k, v in registry:
         if k.endswith('.xpt'):
             tmpfile = MockDest()
             registry[k].copy(tmpfile)
             result[k] = read_interfaces(tmpfile)
         elif isinstance(v, FileRegistry):
             result[k] = get_contents(v)
-        elif isinstance(v, ManifestFile):
+        elif isinstance(v, ManifestFile) or read_all:
             result[k] = v.open().read().splitlines()
         else:
             result[k] = v
     return result
 
 
 class TestFormatters(unittest.TestCase):
     maxDiff = None
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozpack/test/test_packager_unpack.py
@@ -0,0 +1,65 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import mozunit
+from mozpack.packager.formats import (
+    FlatFormatter,
+    JarFormatter,
+    OmniJarFormatter,
+)
+from mozpack.packager.unpack import unpack_to_registry
+from mozpack.copier import (
+    FileCopier,
+    FileRegistry,
+)
+from mozpack.test.test_packager_formats import (
+    CONTENTS,
+    fill_formatter,
+    get_contents,
+)
+from mozpack.test.test_files import TestWithTmpDir
+
+
+class TestUnpack(TestWithTmpDir):
+    maxDiff = None
+
+    @staticmethod
+    def _get_copier(cls):
+        copier = FileCopier()
+        formatter = cls(copier)
+        fill_formatter(formatter, CONTENTS)
+        return copier
+
+    @classmethod
+    def setUpClass(cls):
+        cls.contents = get_contents(cls._get_copier(FlatFormatter),
+                                    read_all=True)
+
+    def _unpack_test(self, cls):
+        # Format a package with the given formatter class
+        copier = self._get_copier(cls)
+        copier.copy(self.tmpdir)
+
+        # Unpack that package. Its content is expected to match that of a Flat
+        # formatted package.
+        registry = FileRegistry()
+        unpack_to_registry(self.tmpdir, registry)
+        self.assertEqual(get_contents(registry, read_all=True), self.contents)
+
+    def test_flat_unpack(self):
+        self._unpack_test(FlatFormatter)
+
+    def test_jar_unpack(self):
+        self._unpack_test(JarFormatter)
+
+    def test_omnijar_unpack(self):
+        class OmniFooFormatter(OmniJarFormatter):
+            def __init__(self, registry):
+                super(OmniFooFormatter, self).__init__(registry, 'omni.foo')
+
+        self._unpack_test(OmniFooFormatter)
+
+
+if __name__ == '__main__':
+    mozunit.main()