michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import mozunit michael@0: from mozpack.packager.formats import ( michael@0: FlatFormatter, michael@0: JarFormatter, michael@0: OmniJarFormatter, michael@0: ) michael@0: from mozpack.copier import FileRegistry michael@0: from mozpack.files import GeneratedFile michael@0: from mozpack.chrome.manifest import ( michael@0: ManifestContent, michael@0: ManifestResource, michael@0: ManifestBinaryComponent, michael@0: ) michael@0: from mozpack.test.test_files import ( michael@0: TestWithTmpDir, michael@0: foo_xpt, michael@0: bar_xpt, michael@0: read_interfaces, michael@0: ) michael@0: michael@0: michael@0: class TestFlatFormatter(TestWithTmpDir): michael@0: def test_flat_formatter(self): michael@0: registry = FileRegistry() michael@0: formatter = FlatFormatter(registry) michael@0: formatter.add_base('app') michael@0: formatter.add('f/oo/bar', GeneratedFile('foobar')) michael@0: formatter.add('f/oo/baz', GeneratedFile('foobaz')) michael@0: formatter.add('f/oo/qux', GeneratedFile('fooqux')) michael@0: formatter.add_manifest(ManifestContent('f/oo', 'bar', 'bar')) michael@0: formatter.add_manifest(ManifestContent('f/oo', 'qux', 'qux')) michael@0: self.assertEqual(registry.paths(), michael@0: ['f/oo/bar', 'f/oo/baz', 'f/oo/qux', michael@0: 'chrome.manifest', 'f/f.manifest', michael@0: 'f/oo/oo.manifest']) michael@0: self.assertEqual(registry['chrome.manifest'].open().read(), michael@0: 'manifest f/f.manifest\n') michael@0: self.assertEqual(registry['f/f.manifest'].open().read(), michael@0: 'manifest oo/oo.manifest\n') michael@0: self.assertEqual(registry['f/oo/oo.manifest'].open().read(), ''.join([ michael@0: 'content bar bar\n', michael@0: 'content qux qux\n', michael@0: ])) michael@0: michael@0: formatter.add_interfaces('components/foo.xpt', foo_xpt) michael@0: formatter.add_interfaces('components/bar.xpt', bar_xpt) michael@0: self.assertEqual(registry.paths(), michael@0: ['f/oo/bar', 'f/oo/baz', 'f/oo/qux', michael@0: 'chrome.manifest', 'f/f.manifest', michael@0: 'f/oo/oo.manifest', 'components/components.manifest', michael@0: 'components/interfaces.xpt']) michael@0: self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([ michael@0: 'manifest f/f.manifest\n', michael@0: 'manifest components/components.manifest\n', michael@0: ])) michael@0: self.assertEqual( michael@0: registry['components/components.manifest'].open().read(), michael@0: 'interfaces interfaces.xpt\n' michael@0: ) michael@0: michael@0: registry['components/interfaces.xpt'] \ michael@0: .copy(self.tmppath('interfaces.xpt')) michael@0: linked = read_interfaces(self.tmppath('interfaces.xpt')) michael@0: foo = read_interfaces(foo_xpt.open()) michael@0: bar = read_interfaces(bar_xpt.open()) michael@0: self.assertEqual(foo['foo'], linked['foo']) michael@0: self.assertEqual(bar['bar'], linked['bar']) michael@0: michael@0: formatter.add_manifest(ManifestContent('app/chrome', 'content', michael@0: 'foo/')) michael@0: self.assertEqual(registry['chrome.manifest'].open().read(), ''.join([ michael@0: 'manifest f/f.manifest\n', michael@0: 'manifest components/components.manifest\n', michael@0: ])) michael@0: self.assertEqual(registry['app/chrome.manifest'].open().read(), michael@0: 'manifest chrome/chrome.manifest\n') michael@0: self.assertEqual(registry['app/chrome/chrome.manifest'].open().read(), michael@0: 'content content foo/\n') michael@0: michael@0: def test_bases(self): michael@0: formatter = FlatFormatter(FileRegistry()) michael@0: formatter.add_base('') michael@0: formatter.add_base('browser') michael@0: formatter.add_base('webapprt') michael@0: self.assertEqual(formatter._get_base('platform.ini'), '') michael@0: self.assertEqual(formatter._get_base('browser/application.ini'), michael@0: 'browser') michael@0: self.assertEqual(formatter._get_base('webapprt/webapprt.ini'), michael@0: 'webapprt') michael@0: michael@0: michael@0: class TestJarFormatter(TestWithTmpDir): michael@0: def test_jar_formatter(self): michael@0: registry = FileRegistry() michael@0: formatter = JarFormatter(registry) michael@0: formatter.add_manifest(ManifestContent('f', 'oo', 'oo/')) michael@0: formatter.add_manifest(ManifestContent('f', 'bar', 'oo/bar/')) michael@0: formatter.add('f/oo/bar/baz', GeneratedFile('foobarbaz')) michael@0: formatter.add('f/oo/qux', GeneratedFile('fooqux')) michael@0: michael@0: self.assertEqual(registry.paths(), michael@0: ['chrome.manifest', 'f/f.manifest', 'f/oo.jar']) michael@0: self.assertEqual(registry['chrome.manifest'].open().read(), michael@0: 'manifest f/f.manifest\n') michael@0: self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([ michael@0: 'content oo jar:oo.jar!/\n', michael@0: 'content bar jar:oo.jar!/bar/\n', michael@0: ])) michael@0: self.assertTrue(formatter.contains('f/oo/bar/baz')) michael@0: self.assertFalse(formatter.contains('foo/bar/baz')) michael@0: self.assertEqual(registry['f/oo.jar'].paths(), ['bar/baz', 'qux']) michael@0: michael@0: formatter.add_manifest(ManifestResource('f', 'foo', 'resource://bar/')) michael@0: self.assertEqual(registry['f/f.manifest'].open().read(), ''.join([ michael@0: 'content oo jar:oo.jar!/\n', michael@0: 'content bar jar:oo.jar!/bar/\n', michael@0: 'resource foo resource://bar/\n', michael@0: ])) michael@0: michael@0: michael@0: class TestOmniJarFormatter(TestWithTmpDir): michael@0: def test_omnijar_formatter(self): michael@0: registry = FileRegistry() michael@0: formatter = OmniJarFormatter(registry, 'omni.foo') michael@0: formatter.add_base('app') michael@0: formatter.add('chrome/f/oo/bar', GeneratedFile('foobar')) michael@0: formatter.add('chrome/f/oo/baz', GeneratedFile('foobaz')) michael@0: formatter.add('chrome/f/oo/qux', GeneratedFile('fooqux')) michael@0: formatter.add_manifest(ManifestContent('chrome/f/oo', 'bar', 'bar')) michael@0: formatter.add_manifest(ManifestContent('chrome/f/oo', 'qux', 'qux')) michael@0: self.assertEqual(registry.paths(), ['omni.foo']) michael@0: self.assertEqual(registry['omni.foo'].paths(), [ michael@0: 'chrome/f/oo/bar', michael@0: 'chrome/f/oo/baz', michael@0: 'chrome/f/oo/qux', michael@0: 'chrome.manifest', michael@0: 'chrome/chrome.manifest', michael@0: 'chrome/f/f.manifest', michael@0: 'chrome/f/oo/oo.manifest', michael@0: ]) michael@0: self.assertEqual(registry['omni.foo']['chrome.manifest'] michael@0: .open().read(), 'manifest chrome/chrome.manifest\n') michael@0: self.assertEqual(registry['omni.foo']['chrome/chrome.manifest'] michael@0: .open().read(), 'manifest f/f.manifest\n') michael@0: self.assertEqual(registry['omni.foo']['chrome/f/f.manifest'] michael@0: .open().read(), 'manifest oo/oo.manifest\n') michael@0: self.assertEqual(registry['omni.foo']['chrome/f/oo/oo.manifest'] michael@0: .open().read(), ''.join([ michael@0: 'content bar bar\n', michael@0: 'content qux qux\n', michael@0: ])) michael@0: self.assertTrue(formatter.contains('chrome/f/oo/bar')) michael@0: self.assertFalse(formatter.contains('chrome/foo/bar')) michael@0: michael@0: formatter.add_interfaces('components/foo.xpt', foo_xpt) michael@0: formatter.add_interfaces('components/bar.xpt', bar_xpt) michael@0: self.assertEqual(registry['omni.foo'].paths(), [ michael@0: 'chrome/f/oo/bar', michael@0: 'chrome/f/oo/baz', michael@0: 'chrome/f/oo/qux', michael@0: 'chrome.manifest', michael@0: 'chrome/chrome.manifest', michael@0: 'chrome/f/f.manifest', michael@0: 'chrome/f/oo/oo.manifest', michael@0: 'components/components.manifest', michael@0: 'components/interfaces.xpt', michael@0: ]) michael@0: self.assertEqual(registry['omni.foo']['chrome.manifest'] michael@0: .open().read(), ''.join([ michael@0: 'manifest chrome/chrome.manifest\n', michael@0: 'manifest components/components.manifest\n' michael@0: ])) michael@0: self.assertEqual(registry['omni.foo'] michael@0: ['components/components.manifest'].open().read(), michael@0: 'interfaces interfaces.xpt\n') michael@0: michael@0: registry['omni.foo'][ michael@0: 'components/interfaces.xpt'].copy(self.tmppath('interfaces.xpt')) michael@0: linked = read_interfaces(self.tmppath('interfaces.xpt')) michael@0: foo = read_interfaces(foo_xpt.open()) michael@0: bar = read_interfaces(bar_xpt.open()) michael@0: self.assertEqual(foo['foo'], linked['foo']) michael@0: self.assertEqual(bar['bar'], linked['bar']) michael@0: michael@0: formatter.add('app/chrome/foo/baz', GeneratedFile('foobaz')) michael@0: formatter.add_manifest(ManifestContent('app/chrome', 'content', michael@0: 'foo/')) michael@0: self.assertEqual(registry.paths(), ['omni.foo', 'app/omni.foo']) michael@0: self.assertEqual(registry['app/omni.foo'].paths(), [ michael@0: 'chrome/foo/baz', michael@0: 'chrome.manifest', michael@0: 'chrome/chrome.manifest', michael@0: ]) michael@0: self.assertEqual(registry['app/omni.foo']['chrome.manifest'] michael@0: .open().read(), 'manifest chrome/chrome.manifest\n') michael@0: self.assertEqual(registry['app/omni.foo']['chrome/chrome.manifest'] michael@0: .open().read(), 'content content foo/\n') michael@0: michael@0: formatter.add_manifest(ManifestBinaryComponent('components', 'foo.so')) michael@0: formatter.add('components/foo.so', GeneratedFile('foo')) michael@0: self.assertEqual(registry.paths(), [ michael@0: 'omni.foo', 'app/omni.foo', 'chrome.manifest', michael@0: 'components/components.manifest', 'components/foo.so', michael@0: ]) michael@0: self.assertEqual(registry['chrome.manifest'].open().read(), michael@0: 'manifest components/components.manifest\n') michael@0: self.assertEqual(registry['components/components.manifest'] michael@0: .open().read(), 'binary-component foo.so\n') michael@0: michael@0: formatter.add_manifest(ManifestBinaryComponent('app/components', michael@0: 'foo.so')) michael@0: formatter.add('app/components/foo.so', GeneratedFile('foo')) michael@0: self.assertEqual(registry.paths(), [ michael@0: 'omni.foo', 'app/omni.foo', 'chrome.manifest', michael@0: 'components/components.manifest', 'components/foo.so', michael@0: 'app/chrome.manifest', 'app/components/components.manifest', michael@0: 'app/components/foo.so', michael@0: ]) michael@0: self.assertEqual(registry['app/chrome.manifest'].open().read(), michael@0: 'manifest components/components.manifest\n') michael@0: self.assertEqual(registry['app/components/components.manifest'] michael@0: .open().read(), 'binary-component foo.so\n') michael@0: michael@0: formatter.add('app/foo', GeneratedFile('foo')) michael@0: self.assertEqual(registry.paths(), [ michael@0: 'omni.foo', 'app/omni.foo', 'chrome.manifest', michael@0: 'components/components.manifest', 'components/foo.so', michael@0: 'app/chrome.manifest', 'app/components/components.manifest', michael@0: 'app/components/foo.so', 'app/foo' michael@0: ]) michael@0: michael@0: def test_omnijar_is_resource(self): michael@0: registry = FileRegistry() michael@0: f = OmniJarFormatter(registry, 'omni.foo', non_resources=[ michael@0: 'defaults/messenger/mailViews.dat', michael@0: 'defaults/foo/*', michael@0: '*/dummy', michael@0: ]) michael@0: f.add_base('app') michael@0: for base in ['', 'app/']: michael@0: self.assertTrue(f.is_resource(base + 'chrome')) michael@0: self.assertTrue( michael@0: f.is_resource(base + 'chrome/foo/bar/baz.properties')) michael@0: self.assertFalse(f.is_resource(base + 'chrome/icons/foo.png')) michael@0: self.assertTrue(f.is_resource(base + 'components/foo.js')) michael@0: self.assertFalse(f.is_resource(base + 'components/foo.so')) michael@0: self.assertTrue(f.is_resource(base + 'res/foo.css')) michael@0: self.assertFalse(f.is_resource(base + 'res/cursors/foo.png')) michael@0: self.assertFalse(f.is_resource(base + 'res/MainMenu.nib/')) michael@0: self.assertTrue(f.is_resource(base + 'defaults/pref/foo.js')) michael@0: self.assertFalse( michael@0: f.is_resource(base + 'defaults/pref/channel-prefs.js')) michael@0: self.assertTrue( michael@0: f.is_resource(base + 'defaults/preferences/foo.js')) michael@0: self.assertFalse( michael@0: f.is_resource(base + 'defaults/preferences/channel-prefs.js')) michael@0: self.assertTrue(f.is_resource(base + 'modules/foo.jsm')) michael@0: self.assertTrue(f.is_resource(base + 'greprefs.js')) michael@0: self.assertTrue(f.is_resource(base + 'hyphenation/foo')) michael@0: self.assertTrue(f.is_resource(base + 'update.locale')) michael@0: self.assertTrue( michael@0: f.is_resource(base + 'jsloader/resource/gre/modules/foo.jsm')) michael@0: self.assertFalse(f.is_resource(base + 'foo')) michael@0: self.assertFalse(f.is_resource('foo/bar/greprefs.js')) michael@0: self.assertTrue(f.is_resource(base + 'defaults/messenger/foo.dat')) michael@0: self.assertFalse( michael@0: f.is_resource(base + 'defaults/messenger/mailViews.dat')) michael@0: self.assertTrue(f.is_resource(base + 'defaults/pref/foo.js')) michael@0: self.assertFalse(f.is_resource(base + 'defaults/foo/bar.dat')) michael@0: self.assertFalse(f.is_resource(base + 'defaults/foo/bar/baz.dat')) michael@0: self.assertTrue(f.is_resource(base + 'chrome/foo/bar/baz/dummy_')) michael@0: self.assertFalse(f.is_resource(base + 'chrome/foo/bar/baz/dummy')) michael@0: self.assertTrue(f.is_resource(base + 'chrome/foo/bar/dummy_')) michael@0: self.assertFalse(f.is_resource(base + 'chrome/foo/bar/dummy')) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: mozunit.main()