Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: tests/test_packagerGecko.py

Issue 29501558: Issue 5383 - Add tests for the Chrome and Firefox packagers (Closed)
Patch Set: Created Aug. 4, 2017, 3:06 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/test_packagerGecko.py
diff --git a/tests/test_packagerGecko.py b/tests/test_packagerGecko.py
new file mode 100644
index 0000000000000000000000000000000000000000..8f16d214400fd7a1189349b83614659ad9478d49
--- /dev/null
+++ b/tests/test_packagerGecko.py
@@ -0,0 +1,259 @@
+# 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 os
+import io
+
+import pytest
+
+import json
+
+from zipfile import ZipFile
+
+from xml.etree import ElementTree
+
+from buildtools import packagerGecko
+from buildtools import localeTools
+
+from buildtools.packager import getBuildVersion, Files, readMetadata
+from buildtools.tests.tools import copy_metadata
+
+MESSAGES = '\n'.join((
+ 'name=Name {0}',
+ 'description=Awesome description {0}',
+))
+
+
+@pytest.fixture
+def gecko_metadata(tmpdir):
+ filename = 'metadata.gecko'
+ copy_metadata(filename, tmpdir)
+
+ return readMetadata(str(tmpdir), 'gecko')
+
+
+@pytest.fixture
+def scripts(tmpdir):
+ """Example scripts for testing addMissingFiles"""
+ lib_dir = tmpdir.mkdir('lib')
+ lib_dir.join('ext.js').write('require("hooks");')
+
+ content_dir = tmpdir.mkdir('chrome').mkdir('content')
+ content_dir.join('common.js').write('require("hooks");')
+
+
+@pytest.fixture
+def prefs_json(tmpdir):
+ """Minimal .json file for testing processJSONFiles"""
+ lib_dir = tmpdir.mkdir('lib')
+ lib_dir.join('prefs.json').write(json.dumps(
+ {'foo': 'bar'}
+ ))
+
+
+@pytest.fixture
+def locales(tmpdir):
+ """Minimal locales for testing locale-processing"""
+ chrome_dir = tmpdir.mkdir('chrome')
+ locale_dir = chrome_dir.mkdir('locale')
+
+ data = {
+ 'name': {'message': 'Name translated'},
+ 'description': {'message': 'Description translated'},
+ }
+
+ for locale in ['en-US', 'de', 'kn']:
+ new_dir = locale_dir.mkdir(locale)
+ new_dir.join('meta.properties').write(MESSAGES.format(locale))
+ new_dir.join('test.json').write(json.dumps(data))
+ if locale == 'kn':
+ new_dir.join('.incomplete').write('')
+
+
+@pytest.fixture
+def subscriptions(tmpdir):
+ """Examplary sbuscription-configuration"""
+ tmpdir.join('subs.xml').write(
+ '<subscriptions>'
+ '<subscription title="EasyList"'
+ ' specialization="English"'
+ ' url="https://easylist-downloads.adblockplus.org/easylist.txt"'
+ ' homepage="https://easylist.adblockplus.org/"'
+ ' prefixes="en"'
+ ' author="fanboy, MonztA, Famlam, Khrin"'
+ ' type="ads"/>'
+ '</subscriptions>'
+ )
+
+
+def test_package_files(tmpdir):
+ tmpdir.join('foo.xml').write('')
+ tmpdir.join('foo.txt').write('')
+ tmpdir.join('foo.js').write('')
+
+ params = {
+ 'baseDir': str(tmpdir)
+ }
+
+ files = packagerGecko.getPackageFiles(params)
+ assert 'foo.xml' in files
+ assert 'foo.js' in files
+ assert 'foo.txt' not in files
+
+
+@pytest.mark.usefixtures('locales')
+@pytest.mark.parametrize('incomplete', [True, False])
+def test_get_locales(tmpdir, incomplete):
+ locales = packagerGecko.getLocales(str(tmpdir), incomplete)
+
+ assert 'de' in locales
+ assert 'en-US' in locales
+ assert ('kn' in locales) == incomplete
+
+
+@pytest.mark.usefixtures('locales', 'subscriptions')
+@pytest.mark.parametrize('release', [True, False])
+@pytest.mark.parametrize('multicompartment', [True, False])
+def test_create_manifest(tmpdir, release, multicompartment, gecko_metadata):
+ locales = packagerGecko.getLocales(str(tmpdir))
+ contributors = packagerGecko.getContributors(gecko_metadata)
+ version = getBuildVersion(str(tmpdir), gecko_metadata, release, None)
+
+ rdfpath = os.path.join(
+ os.path.dirname(__file__),
+ 'expecteddata',
+ 'manifest_gecko_{}_{}.rdf'.format(release, multicompartment)
Vasily Kuznetsov 2017/08/10 19:48:28 Nit: comma at the end of the line.
tlucas 2017/08/11 12:17:09 Done.
+ )
+
+ with io.open(rdfpath, 'r') as fp:
+ expected = ElementTree.fromstring(fp.read())
+
+ params = {
+ 'baseDir': str(tmpdir),
+ 'locales': locales,
+ 'metadata': gecko_metadata,
+ 'version': version.encode('utf-8'),
+ 'multicompartment': multicompartment,
+ 'contributors': contributors
Vasily Kuznetsov 2017/08/10 19:48:28 Nit: comma at the end of the line.
tlucas 2017/08/11 12:17:09 Done.
+ }
+ manifest = packagerGecko.createManifest(params)
+
+ tree = ElementTree.fromstring(manifest)
+
+ from buildtools.tests.tools import get_leafs_string
+ assert set(get_leafs_string(tree)) == set(get_leafs_string(expected))
+
+
+@pytest.mark.usefixtures('locales')
+def test_fixup_import_locales(files, tmpdir, gecko_metadata):
+ locale_dir = tmpdir.dirpath(tmpdir.basename, 'chrome', 'locale')
+ locale_dir.mkdir('fr').join('meta.properties')\
Vasily Kuznetsov 2017/08/10 19:48:28 You don't need to break this line, it actually fit
tlucas 2017/08/11 12:17:09 Done.
+ .write(MESSAGES.format('fr'))
+
+ locales = packagerGecko.getLocales(str(tmpdir), False)
+
+ params = {
+ 'metadata': gecko_metadata,
+ 'locales': locales,
+ 'baseDir': str(tmpdir)
+ }
+
+ # Should add missing fr/test.properties to files
+ packagerGecko.fixupLocales(params, files)
+
+ packagerGecko.importLocales(params, files)
+ for locale in locales:
+ properties = files['chrome/locale/{}/test.properties'.format(locale)]
+ translation_data = list(
+ localeTools.parsePropertiesString(properties, ''))
+
+ for trans in [
+ ('name', None, 'Name translated'),
+ ('description', None, 'Description translated')]:
+ assert trans in translation_data
+
+
+def test_process_json_files(tmpdir, prefs_json):
+ params = {
+ 'baseDir': str(tmpdir),
+ 'jsonRequires': {},
+ }
+
+ files = Files(packagerGecko.getPackageFiles(params), set())
+ files.read(str(tmpdir))
+
+ packagerGecko.processJSONFiles(params, files)
+
+ assert params['jsonRequires'] == {'prefs.json': {'foo': 'bar'}}
+
+
+@pytest.mark.usefixtures('scripts')
+def test_add_missing_files(tmpdir, gecko_metadata):
+ params = {
+ 'baseDir': str(tmpdir),
+ 'metadata': gecko_metadata,
+ 'jsonRequires': {},
+ 'multicompartment': True,
+ 'hasWebExtension': False,
+ }
+
+ files = Files(packagerGecko.getPackageFiles(params), set())
+ files.read(str(tmpdir))
+
+ packagerGecko.addMissingFiles(params, files)
+
+ assert 'let shutdownHandlers = [];' in files['bootstrap.js']
+ assert 'Services.obs.addObserver(' in files['bootstrap.js']
+ for filename in ['bootstrap.js', 'chrome/content/common.js',
+ 'lib/ext.js', 'lib/hooks.js']:
+ assert filename in files
+
+
+@pytest.mark.usefixtures('gecko_metadata', 'locales', 'subscriptions')
+@pytest.mark.parametrize('release', [True, False])
+@pytest.mark.parametrize('multicompartment', [True, False])
+@pytest.mark.parametrize('all_locales', ['all', None])
+def test_create_build(tmpdir, capsys, release, multicompartment, all_locales):
+ base_files = [
+ 'bootstrap.js',
+ 'chrome/locale/de/test.json',
+ 'chrome/locale/de/test.properties',
+ 'chrome/locale/en-US/test.json',
+ 'chrome/locale/en-US/test.properties',
+ 'install.rdf',
+ 'subs.xml'
+ ]
+
+ if all_locales is None:
+ expected = base_files
+ else:
+ expected = base_files + [
+ 'chrome/locale/kn/test.json',
+ 'chrome/locale/kn/test.properties'
+ ]
+
+ out_file = tmpdir.join('{}_{}_{}.zip'.format(
+ all_locales, release, multicompartment))
+
+ out_file = str(out_file)
+
+ packagerGecko.createBuild(
+ str(tmpdir),
+ locales=all_locales,
+ outFile=out_file,
+ releaseBuild=release,
+ multicompartment=multicompartment)
+
+ out, err = capsys.readouterr()
+
+ assert err ==\
+ "Warning: Mapped file adblockplusui/firstRun.html doesn't exist\n"
+
+ with ZipFile(out_file, 'r') as zipfp:
+ zipfp.testzip()
+
+ filenames = [zipinfo.filename for zipinfo in zipfp.infolist()]
+
+ for name in expected:
+ assert name in filenames
« tests/test_packagerChrome.py ('K') | « tests/test_packagerEdge.py ('k') | tests/tools.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld