Debian Codegraph @

python/mozbuild/mozpack/test/test_packager_unpack.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import mozunit
6
7 from mozpack.copier import FileCopier, FileRegistry
8 from mozpack.packager.formats import FlatFormatter, JarFormatter, OmniJarFormatter
9 from mozpack.packager.unpack import unpack_to_registry
10 from mozpack.test.test_files import TestWithTmpDir
11 from mozpack.test.test_packager_formats import CONTENTS, fill_formatter, get_contents
12
13
14 class TestUnpack(TestWithTmpDir):
15 maxDiff = None
16
17 @staticmethod
18 def _get_copier(cls):
19 copier = FileCopier()
20 formatter = cls(copier)
21 fill_formatter(formatter, CONTENTS)
22 return copier
23
24 @classmethod
25 def setUpClass(cls):
26 cls.contents = get_contents(
27 cls._get_copier(FlatFormatter), read_all=True, mode="rb"
28 )
29
30 def _unpack_test(self, cls):
31 # Format a package with the given formatter class
32 copier = self._get_copier(cls)
33 copier.copy(self.tmpdir)
34
35 # Unpack that package. Its content is expected to match that of a Flat
36 # formatted package.
37 registry = FileRegistry()
38 unpack_to_registry(self.tmpdir, registry, getattr(cls, "OMNIJAR_NAME", None))
39 self.assertEqual(
40 get_contents(registry, read_all=True, mode="rb"), self.contents
41 )
42
43 def test_flat_unpack(self):
44 self._unpack_test(FlatFormatter)
45
46 def test_jar_unpack(self):
47 self._unpack_test(JarFormatter)
48
49 @staticmethod
50 def _omni_foo_formatter(name):
51 class OmniFooFormatter(OmniJarFormatter):
52 OMNIJAR_NAME = name
53
54 def __init__(self, registry):
55 super(OmniFooFormatter, self).__init__(registry, name)
56
57 return OmniFooFormatter
58
59 def test_omnijar_unpack(self):
60 self._unpack_test(self._omni_foo_formatter("omni.foo"))
61
62 def test_omnijar_subpath_unpack(self):
63 self._unpack_test(self._omni_foo_formatter("bar/omni.foo"))
64
65
66 if __name__ == "__main__":
67 mozunit.main()