Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | import mozunit |
michael@0 | 6 | from mozpack.packager.formats import ( |
michael@0 | 7 | FlatFormatter, |
michael@0 | 8 | JarFormatter, |
michael@0 | 9 | OmniJarFormatter, |
michael@0 | 10 | ) |
michael@0 | 11 | from mozpack.copier import FileRegistry |
michael@0 | 12 | from mozpack.files import GeneratedFile |
michael@0 | 13 | from mozpack.chrome.manifest import ( |
michael@0 | 14 | ManifestContent, |
michael@0 | 15 | ManifestResource, |
michael@0 | 16 | ManifestBinaryComponent, |
michael@0 | 17 | ) |
michael@0 | 18 | from mozpack.test.test_files import ( |
michael@0 | 19 | TestWithTmpDir, |
michael@0 | 20 | foo_xpt, |
michael@0 | 21 | bar_xpt, |
michael@0 | 22 | read_interfaces, |
michael@0 | 23 | ) |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | class TestFlatFormatter(TestWithTmpDir): |
michael@0 | 27 | def test_flat_formatter(self): |
michael@0 | 28 | registry = FileRegistry() |
michael@0 | 29 | formatter = FlatFormatter(registry) |
michael@0 | 30 | formatter.add_base('app') |
michael@0 | 31 | formatter.add('f/oo/bar', GeneratedFile('foobar')) |
michael@0 | 32 | formatter.add('f/oo/baz', GeneratedFile('foobaz')) |
michael@0 | 33 | formatter.add('f/oo/qux', GeneratedFile('fooqux')) |
michael@0 | 34 | formatter.add_manifest(ManifestContent('f/oo', 'bar', 'bar')) |
michael@0 | 35 | formatter.add_manifest(ManifestContent('f/oo', 'qux', 'qux')) |
michael@0 | 36 | self.assertEqual(registry.paths(), |
michael@0 | 37 | ['f/oo/bar', 'f/oo/baz', 'f/oo/qux', |
michael@0 | 38 | 'chrome.manifest', 'f/f.manifest', |
michael@0 | 39 | 'f/oo/oo.manifest']) |
michael@0 | 40 | self.assertEqual(registry['chrome.manifest'].open().read(), |
michael@0 | 41 | 'manifest f/f.manifest\n') |
michael@0 | 42 | self.assertEqual(registry['f/f.manifest'].open().read(), |
michael@0 | 43 | 'manifest oo/oo.manifest\n') |
michael@0 | 44 | self.assertEqual(registry['f/oo/oo.manifest'].open().read(), ''.join([ |
michael@0 | 45 | 'content bar bar\n', |
michael@0 | 46 | 'content qux qux\n', |
michael@0 | 47 | ])) |
michael@0 | 48 | |
michael@0 | 49 | formatter.add_interfaces('components/foo.xpt', foo_xpt) |
michael@0 | 50 | formatter.add_interfaces('components/bar.xpt', bar_xpt) |
michael@0 | 51 | self.assertEqual(registry.paths(), |
michael@0 | 52 | ['f/oo/bar', 'f/oo/baz', 'f/oo/qux', |
michael@0 | 53 | 'chrome.manifest', 'f/f.manifest', |
michael@0 | 54 | 'f/oo/oo.manifest', 'components/components.manifest', |
michael@0 | 55 | 'components/interfaces.xpt']) |
michael@0 | 56 | self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([ |
michael@0 | 57 | 'manifest f/f.manifest\n', |
michael@0 | 58 | 'manifest components/components.manifest\n', |
michael@0 | 59 | ])) |
michael@0 | 60 | self.assertEqual( |
michael@0 | 61 | registry['components/components.manifest'].open().read(), |
michael@0 | 62 | 'interfaces interfaces.xpt\n' |
michael@0 | 63 | ) |
michael@0 | 64 | |
michael@0 | 65 | registry['components/interfaces.xpt'] \ |
michael@0 | 66 | .copy(self.tmppath('interfaces.xpt')) |
michael@0 | 67 | linked = read_interfaces(self.tmppath('interfaces.xpt')) |
michael@0 | 68 | foo = read_interfaces(foo_xpt.open()) |
michael@0 | 69 | bar = read_interfaces(bar_xpt.open()) |
michael@0 | 70 | self.assertEqual(foo['foo'], linked['foo']) |
michael@0 | 71 | self.assertEqual(bar['bar'], linked['bar']) |
michael@0 | 72 | |
michael@0 | 73 | formatter.add_manifest(ManifestContent('app/chrome', 'content', |
michael@0 | 74 | 'foo/')) |
michael@0 | 75 | self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([ |
michael@0 | 76 | 'manifest f/f.manifest\n', |
michael@0 | 77 | 'manifest components/components.manifest\n', |
michael@0 | 78 | ])) |
michael@0 | 79 | self.assertEqual(registry['app/chrome.manifest'].open().read(), |
michael@0 | 80 | 'manifest chrome/chrome.manifest\n') |
michael@0 | 81 | self.assertEqual(registry['app/chrome/chrome.manifest'].open().read(), |
michael@0 | 82 | 'content content foo/\n') |
michael@0 | 83 | |
michael@0 | 84 | def test_bases(self): |
michael@0 | 85 | formatter = FlatFormatter(FileRegistry()) |
michael@0 | 86 | formatter.add_base('') |
michael@0 | 87 | formatter.add_base('browser') |
michael@0 | 88 | formatter.add_base('webapprt') |
michael@0 | 89 | self.assertEqual(formatter._get_base('platform.ini'), '') |
michael@0 | 90 | self.assertEqual(formatter._get_base('browser/application.ini'), |
michael@0 | 91 | 'browser') |
michael@0 | 92 | self.assertEqual(formatter._get_base('webapprt/webapprt.ini'), |
michael@0 | 93 | 'webapprt') |
michael@0 | 94 | |
michael@0 | 95 | |
michael@0 | 96 | class TestJarFormatter(TestWithTmpDir): |
michael@0 | 97 | def test_jar_formatter(self): |
michael@0 | 98 | registry = FileRegistry() |
michael@0 | 99 | formatter = JarFormatter(registry) |
michael@0 | 100 | formatter.add_manifest(ManifestContent('f', 'oo', 'oo/')) |
michael@0 | 101 | formatter.add_manifest(ManifestContent('f', 'bar', 'oo/bar/')) |
michael@0 | 102 | formatter.add('f/oo/bar/baz', GeneratedFile('foobarbaz')) |
michael@0 | 103 | formatter.add('f/oo/qux', GeneratedFile('fooqux')) |
michael@0 | 104 | |
michael@0 | 105 | self.assertEqual(registry.paths(), |
michael@0 | 106 | ['chrome.manifest', 'f/f.manifest', 'f/oo.jar']) |
michael@0 | 107 | self.assertEqual(registry['chrome.manifest'].open().read(), |
michael@0 | 108 | 'manifest f/f.manifest\n') |
michael@0 | 109 | self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([ |
michael@0 | 110 | 'content oo jar:oo.jar!/\n', |
michael@0 | 111 | 'content bar jar:oo.jar!/bar/\n', |
michael@0 | 112 | ])) |
michael@0 | 113 | self.assertTrue(formatter.contains('f/oo/bar/baz')) |
michael@0 | 114 | self.assertFalse(formatter.contains('foo/bar/baz')) |
michael@0 | 115 | self.assertEqual(registry['f/oo.jar'].paths(), ['bar/baz', 'qux']) |
michael@0 | 116 | |
michael@0 | 117 | formatter.add_manifest(ManifestResource('f', 'foo', 'resource://bar/')) |
michael@0 | 118 | self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([ |
michael@0 | 119 | 'content oo jar:oo.jar!/\n', |
michael@0 | 120 | 'content bar jar:oo.jar!/bar/\n', |
michael@0 | 121 | 'resource foo resource://bar/\n', |
michael@0 | 122 | ])) |
michael@0 | 123 | |
michael@0 | 124 | |
michael@0 | 125 | class TestOmniJarFormatter(TestWithTmpDir): |
michael@0 | 126 | def test_omnijar_formatter(self): |
michael@0 | 127 | registry = FileRegistry() |
michael@0 | 128 | formatter = OmniJarFormatter(registry, 'omni.foo') |
michael@0 | 129 | formatter.add_base('app') |
michael@0 | 130 | formatter.add('chrome/f/oo/bar', GeneratedFile('foobar')) |
michael@0 | 131 | formatter.add('chrome/f/oo/baz', GeneratedFile('foobaz')) |
michael@0 | 132 | formatter.add('chrome/f/oo/qux', GeneratedFile('fooqux')) |
michael@0 | 133 | formatter.add_manifest(ManifestContent('chrome/f/oo', 'bar', 'bar')) |
michael@0 | 134 | formatter.add_manifest(ManifestContent('chrome/f/oo', 'qux', 'qux')) |
michael@0 | 135 | self.assertEqual(registry.paths(), ['omni.foo']) |
michael@0 | 136 | self.assertEqual(registry['omni.foo'].paths(), [ |
michael@0 | 137 | 'chrome/f/oo/bar', |
michael@0 | 138 | 'chrome/f/oo/baz', |
michael@0 | 139 | 'chrome/f/oo/qux', |
michael@0 | 140 | 'chrome.manifest', |
michael@0 | 141 | 'chrome/chrome.manifest', |
michael@0 | 142 | 'chrome/f/f.manifest', |
michael@0 | 143 | 'chrome/f/oo/oo.manifest', |
michael@0 | 144 | ]) |
michael@0 | 145 | self.assertEqual(registry['omni.foo']['chrome.manifest'] |
michael@0 | 146 | .open().read(), 'manifest chrome/chrome.manifest\n') |
michael@0 | 147 | self.assertEqual(registry['omni.foo']['chrome/chrome.manifest'] |
michael@0 | 148 | .open().read(), 'manifest f/f.manifest\n') |
michael@0 | 149 | self.assertEqual(registry['omni.foo']['chrome/f/f.manifest'] |
michael@0 | 150 | .open().read(), 'manifest oo/oo.manifest\n') |
michael@0 | 151 | self.assertEqual(registry['omni.foo']['chrome/f/oo/oo.manifest'] |
michael@0 | 152 | .open().read(), ''.join([ |
michael@0 | 153 | 'content bar bar\n', |
michael@0 | 154 | 'content qux qux\n', |
michael@0 | 155 | ])) |
michael@0 | 156 | self.assertTrue(formatter.contains('chrome/f/oo/bar')) |
michael@0 | 157 | self.assertFalse(formatter.contains('chrome/foo/bar')) |
michael@0 | 158 | |
michael@0 | 159 | formatter.add_interfaces('components/foo.xpt', foo_xpt) |
michael@0 | 160 | formatter.add_interfaces('components/bar.xpt', bar_xpt) |
michael@0 | 161 | self.assertEqual(registry['omni.foo'].paths(), [ |
michael@0 | 162 | 'chrome/f/oo/bar', |
michael@0 | 163 | 'chrome/f/oo/baz', |
michael@0 | 164 | 'chrome/f/oo/qux', |
michael@0 | 165 | 'chrome.manifest', |
michael@0 | 166 | 'chrome/chrome.manifest', |
michael@0 | 167 | 'chrome/f/f.manifest', |
michael@0 | 168 | 'chrome/f/oo/oo.manifest', |
michael@0 | 169 | 'components/components.manifest', |
michael@0 | 170 | 'components/interfaces.xpt', |
michael@0 | 171 | ]) |
michael@0 | 172 | self.assertEqual(registry['omni.foo']['chrome.manifest'] |
michael@0 | 173 | .open().read(), ''.join([ |
michael@0 | 174 | 'manifest chrome/chrome.manifest\n', |
michael@0 | 175 | 'manifest components/components.manifest\n' |
michael@0 | 176 | ])) |
michael@0 | 177 | self.assertEqual(registry['omni.foo'] |
michael@0 | 178 | ['components/components.manifest'].open().read(), |
michael@0 | 179 | 'interfaces interfaces.xpt\n') |
michael@0 | 180 | |
michael@0 | 181 | registry['omni.foo'][ |
michael@0 | 182 | 'components/interfaces.xpt'].copy(self.tmppath('interfaces.xpt')) |
michael@0 | 183 | linked = read_interfaces(self.tmppath('interfaces.xpt')) |
michael@0 | 184 | foo = read_interfaces(foo_xpt.open()) |
michael@0 | 185 | bar = read_interfaces(bar_xpt.open()) |
michael@0 | 186 | self.assertEqual(foo['foo'], linked['foo']) |
michael@0 | 187 | self.assertEqual(bar['bar'], linked['bar']) |
michael@0 | 188 | |
michael@0 | 189 | formatter.add('app/chrome/foo/baz', GeneratedFile('foobaz')) |
michael@0 | 190 | formatter.add_manifest(ManifestContent('app/chrome', 'content', |
michael@0 | 191 | 'foo/')) |
michael@0 | 192 | self.assertEqual(registry.paths(), ['omni.foo', 'app/omni.foo']) |
michael@0 | 193 | self.assertEqual(registry['app/omni.foo'].paths(), [ |
michael@0 | 194 | 'chrome/foo/baz', |
michael@0 | 195 | 'chrome.manifest', |
michael@0 | 196 | 'chrome/chrome.manifest', |
michael@0 | 197 | ]) |
michael@0 | 198 | self.assertEqual(registry['app/omni.foo']['chrome.manifest'] |
michael@0 | 199 | .open().read(), 'manifest chrome/chrome.manifest\n') |
michael@0 | 200 | self.assertEqual(registry['app/omni.foo']['chrome/chrome.manifest'] |
michael@0 | 201 | .open().read(), 'content content foo/\n') |
michael@0 | 202 | |
michael@0 | 203 | formatter.add_manifest(ManifestBinaryComponent('components', 'foo.so')) |
michael@0 | 204 | formatter.add('components/foo.so', GeneratedFile('foo')) |
michael@0 | 205 | self.assertEqual(registry.paths(), [ |
michael@0 | 206 | 'omni.foo', 'app/omni.foo', 'chrome.manifest', |
michael@0 | 207 | 'components/components.manifest', 'components/foo.so', |
michael@0 | 208 | ]) |
michael@0 | 209 | self.assertEqual(registry['chrome.manifest'].open().read(), |
michael@0 | 210 | 'manifest components/components.manifest\n') |
michael@0 | 211 | self.assertEqual(registry['components/components.manifest'] |
michael@0 | 212 | .open().read(), 'binary-component foo.so\n') |
michael@0 | 213 | |
michael@0 | 214 | formatter.add_manifest(ManifestBinaryComponent('app/components', |
michael@0 | 215 | 'foo.so')) |
michael@0 | 216 | formatter.add('app/components/foo.so', GeneratedFile('foo')) |
michael@0 | 217 | self.assertEqual(registry.paths(), [ |
michael@0 | 218 | 'omni.foo', 'app/omni.foo', 'chrome.manifest', |
michael@0 | 219 | 'components/components.manifest', 'components/foo.so', |
michael@0 | 220 | 'app/chrome.manifest', 'app/components/components.manifest', |
michael@0 | 221 | 'app/components/foo.so', |
michael@0 | 222 | ]) |
michael@0 | 223 | self.assertEqual(registry['app/chrome.manifest'].open().read(), |
michael@0 | 224 | 'manifest components/components.manifest\n') |
michael@0 | 225 | self.assertEqual(registry['app/components/components.manifest'] |
michael@0 | 226 | .open().read(), 'binary-component foo.so\n') |
michael@0 | 227 | |
michael@0 | 228 | formatter.add('app/foo', GeneratedFile('foo')) |
michael@0 | 229 | self.assertEqual(registry.paths(), [ |
michael@0 | 230 | 'omni.foo', 'app/omni.foo', 'chrome.manifest', |
michael@0 | 231 | 'components/components.manifest', 'components/foo.so', |
michael@0 | 232 | 'app/chrome.manifest', 'app/components/components.manifest', |
michael@0 | 233 | 'app/components/foo.so', 'app/foo' |
michael@0 | 234 | ]) |
michael@0 | 235 | |
michael@0 | 236 | def test_omnijar_is_resource(self): |
michael@0 | 237 | registry = FileRegistry() |
michael@0 | 238 | f = OmniJarFormatter(registry, 'omni.foo', non_resources=[ |
michael@0 | 239 | 'defaults/messenger/mailViews.dat', |
michael@0 | 240 | 'defaults/foo/*', |
michael@0 | 241 | '*/dummy', |
michael@0 | 242 | ]) |
michael@0 | 243 | f.add_base('app') |
michael@0 | 244 | for base in ['', 'app/']: |
michael@0 | 245 | self.assertTrue(f.is_resource(base + 'chrome')) |
michael@0 | 246 | self.assertTrue( |
michael@0 | 247 | f.is_resource(base + 'chrome/foo/bar/baz.properties')) |
michael@0 | 248 | self.assertFalse(f.is_resource(base + 'chrome/icons/foo.png')) |
michael@0 | 249 | self.assertTrue(f.is_resource(base + 'components/foo.js')) |
michael@0 | 250 | self.assertFalse(f.is_resource(base + 'components/foo.so')) |
michael@0 | 251 | self.assertTrue(f.is_resource(base + 'res/foo.css')) |
michael@0 | 252 | self.assertFalse(f.is_resource(base + 'res/cursors/foo.png')) |
michael@0 | 253 | self.assertFalse(f.is_resource(base + 'res/MainMenu.nib/')) |
michael@0 | 254 | self.assertTrue(f.is_resource(base + 'defaults/pref/foo.js')) |
michael@0 | 255 | self.assertFalse( |
michael@0 | 256 | f.is_resource(base + 'defaults/pref/channel-prefs.js')) |
michael@0 | 257 | self.assertTrue( |
michael@0 | 258 | f.is_resource(base + 'defaults/preferences/foo.js')) |
michael@0 | 259 | self.assertFalse( |
michael@0 | 260 | f.is_resource(base + 'defaults/preferences/channel-prefs.js')) |
michael@0 | 261 | self.assertTrue(f.is_resource(base + 'modules/foo.jsm')) |
michael@0 | 262 | self.assertTrue(f.is_resource(base + 'greprefs.js')) |
michael@0 | 263 | self.assertTrue(f.is_resource(base + 'hyphenation/foo')) |
michael@0 | 264 | self.assertTrue(f.is_resource(base + 'update.locale')) |
michael@0 | 265 | self.assertTrue( |
michael@0 | 266 | f.is_resource(base + 'jsloader/resource/gre/modules/foo.jsm')) |
michael@0 | 267 | self.assertFalse(f.is_resource(base + 'foo')) |
michael@0 | 268 | self.assertFalse(f.is_resource('foo/bar/greprefs.js')) |
michael@0 | 269 | self.assertTrue(f.is_resource(base + 'defaults/messenger/foo.dat')) |
michael@0 | 270 | self.assertFalse( |
michael@0 | 271 | f.is_resource(base + 'defaults/messenger/mailViews.dat')) |
michael@0 | 272 | self.assertTrue(f.is_resource(base + 'defaults/pref/foo.js')) |
michael@0 | 273 | self.assertFalse(f.is_resource(base + 'defaults/foo/bar.dat')) |
michael@0 | 274 | self.assertFalse(f.is_resource(base + 'defaults/foo/bar/baz.dat')) |
michael@0 | 275 | self.assertTrue(f.is_resource(base + 'chrome/foo/bar/baz/dummy_')) |
michael@0 | 276 | self.assertFalse(f.is_resource(base + 'chrome/foo/bar/baz/dummy')) |
michael@0 | 277 | self.assertTrue(f.is_resource(base + 'chrome/foo/bar/dummy_')) |
michael@0 | 278 | self.assertFalse(f.is_resource(base + 'chrome/foo/bar/dummy')) |
michael@0 | 279 | |
michael@0 | 280 | |
michael@0 | 281 | if __name__ == '__main__': |
michael@0 | 282 | mozunit.main() |