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 unittest michael@0: import mozunit michael@0: import os michael@0: from mozpack.chrome.manifest import ( michael@0: ManifestContent, michael@0: ManifestLocale, michael@0: ManifestSkin, michael@0: Manifest, michael@0: ManifestResource, michael@0: ManifestOverride, michael@0: ManifestComponent, michael@0: ManifestContract, michael@0: ManifestInterfaces, michael@0: ManifestBinaryComponent, michael@0: ManifestCategory, michael@0: ManifestStyle, michael@0: ManifestOverlay, michael@0: MANIFESTS_TYPES, michael@0: parse_manifest, michael@0: parse_manifest_line, michael@0: ) michael@0: from mozpack.errors import errors, AccumulatedErrors michael@0: from test_errors import TestErrors michael@0: michael@0: michael@0: class TestManifest(unittest.TestCase): michael@0: def test_parse_manifest(self): michael@0: manifest = [ michael@0: 'content global content/global/', michael@0: 'content global content/global/ application=foo application=bar' + michael@0: ' platform', michael@0: 'locale global en-US content/en-US/', michael@0: 'locale global en-US content/en-US/ application=foo', michael@0: 'skin global classic/1.0 content/skin/classic/', michael@0: 'skin global classic/1.0 content/skin/classic/ application=foo' + michael@0: ' os=WINNT', michael@0: '', michael@0: 'manifest pdfjs/chrome.manifest', michael@0: 'resource gre-resources toolkit/res/', michael@0: 'override chrome://global/locale/netError.dtd' + michael@0: ' chrome://browser/locale/netError.dtd', michael@0: '# Comment', michael@0: 'component {b2bba4df-057d-41ea-b6b1-94a10a8ede68} foo.js', michael@0: 'contract @mozilla.org/foo;1' + michael@0: ' {b2bba4df-057d-41ea-b6b1-94a10a8ede68}', michael@0: 'interfaces foo.xpt # Inline comment', michael@0: 'binary-component bar.so', michael@0: 'category command-line-handler m-browser' + michael@0: ' @mozilla.org/browser/clh;1' + michael@0: ' application={ec8030f7-c20a-464f-9b0e-13a3a9e97384}', michael@0: 'style chrome://global/content/customizeToolbar.xul' + michael@0: ' chrome://browser/skin/', michael@0: 'overlay chrome://global/content/viewSource.xul' + michael@0: ' chrome://browser/content/viewSourceOverlay.xul', michael@0: ] michael@0: other_manifest = [ michael@0: 'content global content/global/' michael@0: ] michael@0: expected_result = [ michael@0: ManifestContent('', 'global', 'content/global/'), michael@0: ManifestContent('', 'global', 'content/global/', 'application=foo', michael@0: 'application=bar', 'platform'), michael@0: ManifestLocale('', 'global', 'en-US', 'content/en-US/'), michael@0: ManifestLocale('', 'global', 'en-US', 'content/en-US/', michael@0: 'application=foo'), michael@0: ManifestSkin('', 'global', 'classic/1.0', 'content/skin/classic/'), michael@0: ManifestSkin('', 'global', 'classic/1.0', 'content/skin/classic/', michael@0: 'application=foo', 'os=WINNT'), michael@0: Manifest('', 'pdfjs/chrome.manifest'), michael@0: ManifestResource('', 'gre-resources', 'toolkit/res/'), michael@0: ManifestOverride('', 'chrome://global/locale/netError.dtd', michael@0: 'chrome://browser/locale/netError.dtd'), michael@0: ManifestComponent('', '{b2bba4df-057d-41ea-b6b1-94a10a8ede68}', michael@0: 'foo.js'), michael@0: ManifestContract('', '@mozilla.org/foo;1', michael@0: '{b2bba4df-057d-41ea-b6b1-94a10a8ede68}'), michael@0: ManifestInterfaces('', 'foo.xpt'), michael@0: ManifestBinaryComponent('', 'bar.so'), michael@0: ManifestCategory('', 'command-line-handler', 'm-browser', michael@0: '@mozilla.org/browser/clh;1', 'application=' + michael@0: '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}'), michael@0: ManifestStyle('', 'chrome://global/content/customizeToolbar.xul', michael@0: 'chrome://browser/skin/'), michael@0: ManifestOverlay('', 'chrome://global/content/viewSource.xul', michael@0: 'chrome://browser/content/viewSourceOverlay.xul'), michael@0: ] michael@0: with mozunit.MockedOpen({'manifest': '\n'.join(manifest), michael@0: 'other/manifest': '\n'.join(other_manifest)}): michael@0: # Ensure we have tests for all types of manifests. michael@0: self.assertEqual(set(type(e) for e in expected_result), michael@0: set(MANIFESTS_TYPES.values())) michael@0: self.assertEqual(list(parse_manifest(os.curdir, 'manifest')), michael@0: expected_result) michael@0: self.assertEqual(list(parse_manifest(os.curdir, 'other/manifest')), michael@0: [ManifestContent('other', 'global', michael@0: 'content/global/')]) michael@0: michael@0: def test_manifest_rebase(self): michael@0: m = parse_manifest_line('chrome', 'content global content/global/') michael@0: m = m.rebase('') michael@0: self.assertEqual(str(m), 'content global chrome/content/global/') michael@0: m = m.rebase('chrome') michael@0: self.assertEqual(str(m), 'content global content/global/') michael@0: michael@0: m = parse_manifest_line('chrome/foo', 'content global content/global/') michael@0: m = m.rebase('chrome') michael@0: self.assertEqual(str(m), 'content global foo/content/global/') michael@0: m = m.rebase('chrome/foo') michael@0: self.assertEqual(str(m), 'content global content/global/') michael@0: michael@0: m = parse_manifest_line('modules/foo', 'resource foo ./') michael@0: m = m.rebase('modules') michael@0: self.assertEqual(str(m), 'resource foo foo/') michael@0: m = m.rebase('modules/foo') michael@0: self.assertEqual(str(m), 'resource foo ./') michael@0: michael@0: m = parse_manifest_line('chrome', 'content browser browser/content/') michael@0: m = m.rebase('chrome/browser').move('jar:browser.jar!').rebase('') michael@0: self.assertEqual(str(m), 'content browser jar:browser.jar!/content/') michael@0: michael@0: michael@0: class TestManifestErrors(TestErrors, unittest.TestCase): michael@0: def test_parse_manifest_errors(self): michael@0: manifest = [ michael@0: 'skin global classic/1.0 content/skin/classic/ platform', michael@0: '', michael@0: 'binary-component bar.so', michael@0: 'unsupported foo', michael@0: ] michael@0: with mozunit.MockedOpen({'manifest': '\n'.join(manifest)}): michael@0: with self.assertRaises(AccumulatedErrors): michael@0: with errors.accumulate(): michael@0: list(parse_manifest(os.curdir, 'manifest')) michael@0: out = self.get_output() michael@0: # Expecting 2 errors michael@0: self.assertEqual(len(out), 2) michael@0: path = os.path.abspath('manifest') michael@0: # First on line 1 michael@0: self.assertTrue(out[0].startswith('Error: %s:1: ' % path)) michael@0: # Second on line 4 michael@0: self.assertTrue(out[1].startswith('Error: %s:4: ' % path)) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: mozunit.main()