Bug 910660 - Refactor test_packager_formats.py so that it's easier to follow. r=gps, a=sledru
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 13 Mar 2015 11:42:09 +0900
changeset 258293 a141c675b405
parent 258292 882dd82e8af0
child 258294 06cd1b5deb25
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 - Refactor test_packager_formats.py so that it's easier to follow. r=gps, a=sledru
python/mozbuild/mozpack/test/test_packager_formats.py
--- a/python/mozbuild/mozpack/test/test_packager_formats.py
+++ b/python/mozbuild/mozpack/test/test_packager_formats.py
@@ -1,242 +1,268 @@
 # 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
+import unittest
 from mozpack.packager.formats import (
     FlatFormatter,
     JarFormatter,
     OmniJarFormatter,
 )
 from mozpack.copier import FileRegistry
-from mozpack.files import GeneratedFile
+from mozpack.files import (
+    GeneratedFile,
+    ManifestFile,
+)
 from mozpack.chrome.manifest import (
     ManifestContent,
+    ManifestComponent,
     ManifestResource,
     ManifestBinaryComponent,
 )
 from mozpack.test.test_files import (
-    TestWithTmpDir,
+    MockDest,
     foo_xpt,
     bar_xpt,
     read_interfaces,
 )
 
 
-class TestFlatFormatter(TestWithTmpDir):
-    def test_flat_formatter(self):
-        registry = FileRegistry()
-        formatter = FlatFormatter(registry)
-        formatter.add_base('app')
-        formatter.add('f/oo/bar', GeneratedFile('foobar'))
-        formatter.add('f/oo/baz', GeneratedFile('foobaz'))
-        formatter.add('f/oo/qux', GeneratedFile('fooqux'))
-        formatter.add_manifest(ManifestContent('f/oo', 'bar', 'bar'))
-        formatter.add_manifest(ManifestContent('f/oo', 'qux', 'qux'))
-        self.assertEqual(registry.paths(),
-                         ['f/oo/bar', 'f/oo/baz', 'f/oo/qux',
-                          'chrome.manifest', 'f/f.manifest',
-                          'f/oo/oo.manifest'])
-        self.assertEqual(registry['chrome.manifest'].open().read(),
-                         'manifest f/f.manifest\n')
-        self.assertEqual(registry['f/f.manifest'].open().read(),
-                         'manifest oo/oo.manifest\n')
-        self.assertEqual(registry['f/oo/oo.manifest'].open().read(), ''.join([
-            'content bar bar\n',
-            'content qux qux\n',
-        ]))
+CONTENTS = {
+    'bases': [
+        'app',
+    ],
+    'manifests': [
+        ManifestContent('chrome/f', 'oo', 'oo/'),
+        ManifestContent('chrome/f', 'bar', 'oo/bar/'),
+        ManifestResource('chrome/f', 'foo', 'resource://bar/'),
+        ManifestBinaryComponent('components', 'foo.so'),
+        ManifestContent('app/chrome', 'content', 'foo/'),
+        ManifestComponent('app/components', '{foo-id}', 'foo.js'),
+    ],
+    'chrome/f/oo/bar/baz': GeneratedFile('foobarbaz'),
+    'chrome/f/oo/baz': GeneratedFile('foobaz'),
+    'chrome/f/oo/qux': GeneratedFile('fooqux'),
+    'components/foo.so': GeneratedFile('foo.so'),
+    'components/foo.xpt': foo_xpt,
+    'components/bar.xpt': bar_xpt,
+    'foo': GeneratedFile('foo'),
+    'app/chrome/foo/foo': GeneratedFile('appfoo'),
+    'app/components/foo.js': GeneratedFile('foo.js'),
+}
+
+
+class MockDest(MockDest):
+    def exists(self):
+        return False
+
 
-        formatter.add_interfaces('components/foo.xpt', foo_xpt)
-        formatter.add_interfaces('components/bar.xpt', bar_xpt)
-        self.assertEqual(registry.paths(),
-                         ['f/oo/bar', 'f/oo/baz', 'f/oo/qux',
-                          'chrome.manifest', 'f/f.manifest',
-                          'f/oo/oo.manifest', 'components/components.manifest',
-                          'components/interfaces.xpt'])
-        self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([
-            'manifest f/f.manifest\n',
-            'manifest components/components.manifest\n',
-        ]))
-        self.assertEqual(
-            registry['components/components.manifest'].open().read(),
-            'interfaces interfaces.xpt\n'
-        )
+def fill_formatter(formatter, contents):
+    for base in contents['bases']:
+        formatter.add_base(base)
+
+    for manifest in contents['manifests']:
+        formatter.add_manifest(manifest)
+
+    for k, v in contents.iteritems():
+        if k in ('bases', 'manifests'):
+            continue
+        if k.endswith('.xpt'):
+            formatter.add_interfaces(k, v)
+        else:
+            formatter.add(k, v)
+
 
-        registry['components/interfaces.xpt'] \
-            .copy(self.tmppath('interfaces.xpt'))
-        linked = read_interfaces(self.tmppath('interfaces.xpt'))
-        foo = read_interfaces(foo_xpt.open())
-        bar = read_interfaces(bar_xpt.open())
-        self.assertEqual(foo['foo'], linked['foo'])
-        self.assertEqual(bar['bar'], linked['bar'])
+def get_contents(registry):
+    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):
+            result[k] = v.open().read().splitlines()
+        else:
+            result[k] = v
+    return result
 
-        formatter.add_manifest(ManifestContent('app/chrome', 'content',
-                                               'foo/'))
-        self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([
-            'manifest f/f.manifest\n',
-            'manifest components/components.manifest\n',
-        ]))
-        self.assertEqual(registry['app/chrome.manifest'].open().read(),
-                         'manifest chrome/chrome.manifest\n')
-        self.assertEqual(registry['app/chrome/chrome.manifest'].open().read(),
-                         'content content foo/\n')
+
+class TestFormatters(unittest.TestCase):
+    maxDiff = None
 
     def test_bases(self):
         formatter = FlatFormatter(FileRegistry())
         formatter.add_base('')
         formatter.add_base('browser')
         formatter.add_base('webapprt')
         self.assertEqual(formatter._get_base('platform.ini'), '')
         self.assertEqual(formatter._get_base('browser/application.ini'),
                          'browser')
         self.assertEqual(formatter._get_base('webapprt/webapprt.ini'),
                          'webapprt')
 
+    def test_flat_formatter(self):
+        registry = FileRegistry()
+        formatter = FlatFormatter(registry)
 
-class TestJarFormatter(TestWithTmpDir):
+        fill_formatter(formatter, CONTENTS)
+
+        RESULT = {
+            'chrome.manifest': [
+                'manifest chrome/chrome.manifest',
+                'manifest components/components.manifest',
+            ],
+            'chrome/chrome.manifest': [
+                'manifest f/f.manifest',
+            ],
+            'chrome/f/f.manifest': [
+                'content oo oo/',
+                'content bar oo/bar/',
+                'resource foo resource://bar/',
+            ],
+            'chrome/f/oo/bar/baz': CONTENTS['chrome/f/oo/bar/baz'],
+            'chrome/f/oo/baz': CONTENTS['chrome/f/oo/baz'],
+            'chrome/f/oo/qux': CONTENTS['chrome/f/oo/qux'],
+            'components/components.manifest': [
+                'binary-component foo.so',
+                'interfaces interfaces.xpt',
+            ],
+            'components/foo.so': CONTENTS['components/foo.so'],
+            'components/interfaces.xpt': {
+                'foo': read_interfaces(foo_xpt.open())['foo'],
+                'bar': read_interfaces(bar_xpt.open())['bar'],
+            },
+            'foo': CONTENTS['foo'],
+            'app/chrome.manifest': [
+                'manifest chrome/chrome.manifest',
+                'manifest components/components.manifest',
+            ],
+            'app/chrome/chrome.manifest': [
+                'content content foo/',
+            ],
+            'app/chrome/foo/foo': CONTENTS['app/chrome/foo/foo'],
+            'app/components/components.manifest': [
+                'component {foo-id} foo.js',
+            ],
+            'app/components/foo.js': CONTENTS['app/components/foo.js'],
+        }
+
+        self.assertEqual(get_contents(registry), RESULT)
+
     def test_jar_formatter(self):
         registry = FileRegistry()
         formatter = JarFormatter(registry)
-        formatter.add_manifest(ManifestContent('f', 'oo', 'oo/'))
-        formatter.add_manifest(ManifestContent('f', 'bar', 'oo/bar/'))
-        formatter.add('f/oo/bar/baz', GeneratedFile('foobarbaz'))
-        formatter.add('f/oo/qux', GeneratedFile('fooqux'))
+
+        fill_formatter(formatter, CONTENTS)
 
-        self.assertEqual(registry.paths(),
-                         ['chrome.manifest', 'f/f.manifest', 'f/oo.jar'])
-        self.assertEqual(registry['chrome.manifest'].open().read(),
-                         'manifest f/f.manifest\n')
-        self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([
-            'content oo jar:oo.jar!/\n',
-            'content bar jar:oo.jar!/bar/\n',
-        ]))
-        self.assertTrue(formatter.contains('f/oo/bar/baz'))
-        self.assertFalse(formatter.contains('foo/bar/baz'))
-        self.assertEqual(registry['f/oo.jar'].paths(), ['bar/baz', 'qux'])
+        RESULT = {
+            'chrome.manifest': [
+                'manifest chrome/chrome.manifest',
+                'manifest components/components.manifest',
+            ],
+            'chrome/chrome.manifest': [
+                'manifest f/f.manifest',
+            ],
+            'chrome/f/f.manifest': [
+                'content oo jar:oo.jar!/',
+                'content bar jar:oo.jar!/bar/',
+                'resource foo resource://bar/',
+            ],
+            'chrome/f/oo.jar': {
+                'bar/baz': CONTENTS['chrome/f/oo/bar/baz'],
+                'baz': CONTENTS['chrome/f/oo/baz'],
+                'qux': CONTENTS['chrome/f/oo/qux'],
+            },
+            'components/components.manifest': [
+                'binary-component foo.so',
+                'interfaces interfaces.xpt',
+            ],
+            'components/foo.so': CONTENTS['components/foo.so'],
+            'components/interfaces.xpt': {
+                'foo': read_interfaces(foo_xpt.open())['foo'],
+                'bar': read_interfaces(bar_xpt.open())['bar'],
+            },
+            'foo': CONTENTS['foo'],
+            'app/chrome.manifest': [
+                'manifest chrome/chrome.manifest',
+                'manifest components/components.manifest',
+            ],
+            'app/chrome/chrome.manifest': [
+                'content content jar:foo.jar!/',
+            ],
+            'app/chrome/foo.jar': {
+                'foo': CONTENTS['app/chrome/foo/foo'],
+            },
+            'app/components/components.manifest': [
+                'component {foo-id} foo.js',
+            ],
+            'app/components/foo.js': CONTENTS['app/components/foo.js'],
+        }
 
-        formatter.add_manifest(ManifestResource('f', 'foo', 'resource://bar/'))
-        self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([
-            'content oo jar:oo.jar!/\n',
-            'content bar jar:oo.jar!/bar/\n',
-            'resource foo resource://bar/\n',
-        ]))
+        self.assertEqual(get_contents(registry), RESULT)
 
-
-class TestOmniJarFormatter(TestWithTmpDir):
     def test_omnijar_formatter(self):
         registry = FileRegistry()
         formatter = OmniJarFormatter(registry, 'omni.foo')
-        formatter.add_base('app')
-        formatter.add('chrome/f/oo/bar', GeneratedFile('foobar'))
-        formatter.add('chrome/f/oo/baz', GeneratedFile('foobaz'))
-        formatter.add('chrome/f/oo/qux', GeneratedFile('fooqux'))
-        formatter.add_manifest(ManifestContent('chrome/f/oo', 'bar', 'bar'))
-        formatter.add_manifest(ManifestContent('chrome/f/oo', 'qux', 'qux'))
-        self.assertEqual(registry.paths(), ['omni.foo'])
-        self.assertEqual(registry['omni.foo'].paths(), [
-            'chrome/f/oo/bar',
-            'chrome/f/oo/baz',
-            'chrome/f/oo/qux',
-            'chrome.manifest',
-            'chrome/chrome.manifest',
-            'chrome/f/f.manifest',
-            'chrome/f/oo/oo.manifest',
-        ])
-        self.assertEqual(registry['omni.foo']['chrome.manifest']
-                         .open().read(), 'manifest chrome/chrome.manifest\n')
-        self.assertEqual(registry['omni.foo']['chrome/chrome.manifest']
-                         .open().read(), 'manifest f/f.manifest\n')
-        self.assertEqual(registry['omni.foo']['chrome/f/f.manifest']
-                         .open().read(), 'manifest oo/oo.manifest\n')
-        self.assertEqual(registry['omni.foo']['chrome/f/oo/oo.manifest']
-                         .open().read(), ''.join([
-                             'content bar bar\n',
-                             'content qux qux\n',
-                         ]))
-        self.assertTrue(formatter.contains('chrome/f/oo/bar'))
-        self.assertFalse(formatter.contains('chrome/foo/bar'))
 
-        formatter.add_interfaces('components/foo.xpt', foo_xpt)
-        formatter.add_interfaces('components/bar.xpt', bar_xpt)
-        self.assertEqual(registry['omni.foo'].paths(), [
-            'chrome/f/oo/bar',
-            'chrome/f/oo/baz',
-            'chrome/f/oo/qux',
-            'chrome.manifest',
-            'chrome/chrome.manifest',
-            'chrome/f/f.manifest',
-            'chrome/f/oo/oo.manifest',
-            'components/components.manifest',
-            'components/interfaces.xpt',
-        ])
-        self.assertEqual(registry['omni.foo']['chrome.manifest']
-                         .open().read(), ''.join([
-                             'manifest chrome/chrome.manifest\n',
-                             'manifest components/components.manifest\n'
-                         ]))
-        self.assertEqual(registry['omni.foo']
-                         ['components/components.manifest'].open().read(),
-                         'interfaces interfaces.xpt\n')
+        fill_formatter(formatter, CONTENTS)
 
-        registry['omni.foo'][
-            'components/interfaces.xpt'].copy(self.tmppath('interfaces.xpt'))
-        linked = read_interfaces(self.tmppath('interfaces.xpt'))
-        foo = read_interfaces(foo_xpt.open())
-        bar = read_interfaces(bar_xpt.open())
-        self.assertEqual(foo['foo'], linked['foo'])
-        self.assertEqual(bar['bar'], linked['bar'])
-
-        formatter.add('app/chrome/foo/baz', GeneratedFile('foobaz'))
-        formatter.add_manifest(ManifestContent('app/chrome', 'content',
-                                               'foo/'))
-        self.assertEqual(registry.paths(), ['omni.foo', 'app/omni.foo'])
-        self.assertEqual(registry['app/omni.foo'].paths(), [
-            'chrome/foo/baz',
-            'chrome.manifest',
-            'chrome/chrome.manifest',
-        ])
-        self.assertEqual(registry['app/omni.foo']['chrome.manifest']
-                         .open().read(), 'manifest chrome/chrome.manifest\n')
-        self.assertEqual(registry['app/omni.foo']['chrome/chrome.manifest']
-                         .open().read(), 'content content foo/\n')
-
-        formatter.add_manifest(ManifestBinaryComponent('components', 'foo.so'))
-        formatter.add('components/foo.so', GeneratedFile('foo'))
-        self.assertEqual(registry.paths(), [
-            'omni.foo', 'app/omni.foo', 'chrome.manifest',
-            'components/components.manifest', 'components/foo.so',
-        ])
-        self.assertEqual(registry['chrome.manifest'].open().read(),
-                         'manifest components/components.manifest\n')
-        self.assertEqual(registry['components/components.manifest']
-                         .open().read(), 'binary-component foo.so\n')
-
-        formatter.add_manifest(ManifestBinaryComponent('app/components',
-                                                       'foo.so'))
-        formatter.add('app/components/foo.so', GeneratedFile('foo'))
-        self.assertEqual(registry.paths(), [
-            'omni.foo', 'app/omni.foo', 'chrome.manifest',
-            'components/components.manifest', 'components/foo.so',
-            'app/chrome.manifest', 'app/components/components.manifest',
-            'app/components/foo.so',
-        ])
-        self.assertEqual(registry['app/chrome.manifest'].open().read(),
-                         'manifest components/components.manifest\n')
-        self.assertEqual(registry['app/components/components.manifest']
-                         .open().read(), 'binary-component foo.so\n')
-
-        formatter.add('app/foo', GeneratedFile('foo'))
-        self.assertEqual(registry.paths(), [
-            'omni.foo', 'app/omni.foo', 'chrome.manifest',
-            'components/components.manifest', 'components/foo.so',
-            'app/chrome.manifest', 'app/components/components.manifest',
-            'app/components/foo.so', 'app/foo'
-        ])
+        RESULT = {
+            'omni.foo': {
+                'chrome.manifest': [
+                    'manifest chrome/chrome.manifest',
+                    'manifest components/components.manifest',
+                ],
+                'chrome/chrome.manifest': [
+                    'manifest f/f.manifest',
+                ],
+                'chrome/f/f.manifest': [
+                    'content oo oo/',
+                    'content bar oo/bar/',
+                    'resource foo resource://bar/',
+                ],
+                'chrome/f/oo/bar/baz': CONTENTS['chrome/f/oo/bar/baz'],
+                'chrome/f/oo/baz': CONTENTS['chrome/f/oo/baz'],
+                'chrome/f/oo/qux': CONTENTS['chrome/f/oo/qux'],
+                'components/components.manifest': [
+                    'interfaces interfaces.xpt',
+                ],
+                'components/interfaces.xpt': {
+                    'foo': read_interfaces(foo_xpt.open())['foo'],
+                    'bar': read_interfaces(bar_xpt.open())['bar'],
+                },
+            },
+            'chrome.manifest': [
+                'manifest components/components.manifest',
+            ],
+            'components/components.manifest': [
+                'binary-component foo.so',
+            ],
+            'components/foo.so': CONTENTS['components/foo.so'],
+            'foo': CONTENTS['foo'],
+            'app/omni.foo': {
+                'chrome.manifest': [
+                    'manifest chrome/chrome.manifest',
+                    'manifest components/components.manifest',
+                ],
+                'chrome/chrome.manifest': [
+                    'content content foo/',
+                ],
+                'chrome/foo/foo': CONTENTS['app/chrome/foo/foo'],
+                'components/components.manifest': [
+                    'component {foo-id} foo.js',
+                ],
+                'components/foo.js': CONTENTS['app/components/foo.js'],
+            },
+        }
+        self.assertEqual(get_contents(registry), RESULT)
 
     def test_omnijar_is_resource(self):
         registry = FileRegistry()
         f = OmniJarFormatter(registry, 'omni.foo', non_resources=[
             'defaults/messenger/mailViews.dat',
             'defaults/foo/*',
             '*/dummy',
         ])