Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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 os | |
6 import io | |
7 | |
8 import pytest | |
9 | |
10 import json | |
11 | |
12 from zipfile import ZipFile | |
13 | |
14 from xml.etree import ElementTree | |
15 | |
16 from buildtools import packagerGecko | |
17 from buildtools import localeTools | |
18 | |
19 from buildtools.packager import getBuildVersion, Files, readMetadata | |
20 from buildtools.tests.tools import copy_metadata | |
21 | |
22 MESSAGES = '\n'.join(( | |
Vasily Kuznetsov
2017/08/11 16:46:02
Our style guide recommends that things that you it
| |
23 'name=Name {0}', | |
24 'description=Awesome description {0}', | |
25 )) | |
26 | |
27 | |
28 @pytest.fixture | |
29 def gecko_metadata(tmpdir): | |
30 filename = 'metadata.gecko' | |
31 copy_metadata(filename, tmpdir) | |
32 | |
33 return readMetadata(str(tmpdir), 'gecko') | |
34 | |
35 | |
36 @pytest.fixture | |
37 def scripts(tmpdir): | |
38 """Example scripts for testing addMissingFiles""" | |
39 lib_dir = tmpdir.mkdir('lib') | |
40 lib_dir.join('ext.js').write('require("hooks");') | |
41 | |
42 content_dir = tmpdir.mkdir('chrome').mkdir('content') | |
43 content_dir.join('common.js').write('require("hooks");') | |
44 | |
45 | |
46 @pytest.fixture | |
47 def prefs_json(tmpdir): | |
48 """Minimal .json file for testing processJSONFiles""" | |
49 lib_dir = tmpdir.mkdir('lib') | |
50 lib_dir.join('prefs.json').write(json.dumps( | |
51 {'foo': 'bar'} | |
52 )) | |
53 | |
54 | |
55 @pytest.fixture | |
56 def locales(tmpdir): | |
57 """Minimal locales for testing locale-processing""" | |
58 chrome_dir = tmpdir.mkdir('chrome') | |
59 locale_dir = chrome_dir.mkdir('locale') | |
60 | |
61 data = { | |
62 'name': {'message': 'Name translated'}, | |
63 'description': {'message': 'Description translated'}, | |
64 } | |
65 | |
66 for locale in ['en-US', 'de', 'kn']: | |
67 new_dir = locale_dir.mkdir(locale) | |
68 new_dir.join('meta.properties').write(MESSAGES.format(locale)) | |
69 new_dir.join('test.json').write(json.dumps(data)) | |
70 if locale == 'kn': | |
71 new_dir.join('.incomplete').write('') | |
72 | |
73 | |
74 @pytest.fixture | |
75 def subscriptions(tmpdir): | |
76 """Examplary sbuscription-configuration""" | |
77 tmpdir.join('subs.xml').write( | |
78 '<subscriptions>' | |
79 '<subscription title="EasyList"' | |
80 ' specialization="English"' | |
81 ' url="https://easylist-downloads.adblockplus.org/easylist.txt"' | |
82 ' homepage="https://easylist.adblockplus.org/"' | |
83 ' prefixes="en"' | |
84 ' author="fanboy, MonztA, Famlam, Khrin"' | |
85 ' type="ads"/>' | |
86 '</subscriptions>' | |
87 ) | |
88 | |
89 | |
90 def test_package_files(tmpdir): | |
91 tmpdir.join('foo.xml').write('') | |
92 tmpdir.join('foo.txt').write('') | |
93 tmpdir.join('foo.js').write('') | |
94 | |
95 params = { | |
96 'baseDir': str(tmpdir) | |
97 } | |
98 | |
99 files = packagerGecko.getPackageFiles(params) | |
100 assert 'foo.xml' in files | |
101 assert 'foo.js' in files | |
102 assert 'foo.txt' not in files | |
103 | |
104 | |
105 @pytest.mark.usefixtures('locales') | |
106 @pytest.mark.parametrize('incomplete', [True, False]) | |
107 def test_get_locales(tmpdir, incomplete): | |
108 locales = packagerGecko.getLocales(str(tmpdir), incomplete) | |
109 | |
110 assert 'de' in locales | |
111 assert 'en-US' in locales | |
112 assert ('kn' in locales) == incomplete | |
113 | |
114 | |
115 @pytest.mark.usefixtures('locales', 'subscriptions') | |
116 @pytest.mark.parametrize('release', [True, False]) | |
117 @pytest.mark.parametrize('multicompartment', [True, False]) | |
118 def test_create_manifest(tmpdir, release, multicompartment, gecko_metadata): | |
119 locales = packagerGecko.getLocales(str(tmpdir)) | |
120 contributors = packagerGecko.getContributors(gecko_metadata) | |
121 version = getBuildVersion(str(tmpdir), gecko_metadata, release, None) | |
122 | |
123 rdfpath = os.path.join( | |
124 os.path.dirname(__file__), | |
125 'expecteddata', | |
126 'manifest_gecko_{}_{}.rdf'.format(release, multicompartment), | |
127 ) | |
128 | |
129 with io.open(rdfpath, 'r') as fp: | |
130 expected = ElementTree.fromstring(fp.read()) | |
131 | |
132 params = { | |
133 'baseDir': str(tmpdir), | |
134 'locales': locales, | |
135 'metadata': gecko_metadata, | |
136 'version': version.encode('utf-8'), | |
137 'multicompartment': multicompartment, | |
138 'contributors': contributors, | |
139 } | |
140 manifest = packagerGecko.createManifest(params) | |
141 | |
142 tree = ElementTree.fromstring(manifest) | |
143 | |
144 from buildtools.tests.tools import get_leafs_string | |
145 assert set(get_leafs_string(tree)) == set(get_leafs_string(expected)) | |
146 | |
147 | |
148 @pytest.mark.usefixtures('locales') | |
149 def test_fixup_import_locales(base_files, tmpdir, gecko_metadata): | |
150 locale_dir = tmpdir.dirpath(tmpdir.basename, 'chrome', 'locale') | |
151 locale_dir.mkdir('fr').join('meta.properties').write(MESSAGES.format('fr')) | |
152 | |
153 locales = packagerGecko.getLocales(str(tmpdir), False) | |
154 | |
155 params = { | |
156 'metadata': gecko_metadata, | |
157 'locales': locales, | |
158 'baseDir': str(tmpdir) | |
159 } | |
160 | |
161 # Should add missing fr/test.properties to files | |
162 packagerGecko.fixupLocales(params, base_files) | |
163 | |
164 packagerGecko.importLocales(params, base_files) | |
165 for locale in locales: | |
166 properties = base_files[ | |
167 'chrome/locale/{}/test.properties'.format(locale) | |
168 ] | |
169 translation_data = list( | |
170 localeTools.parsePropertiesString(properties, '')) | |
171 | |
172 for trans in [ | |
173 ('name', None, 'Name translated'), | |
174 ('description', None, 'Description translated')]: | |
175 assert trans in translation_data | |
176 | |
177 | |
178 def test_process_json_files(tmpdir, prefs_json): | |
179 params = { | |
180 'baseDir': str(tmpdir), | |
181 'jsonRequires': {}, | |
182 } | |
183 | |
184 files = Files(packagerGecko.getPackageFiles(params), set()) | |
185 files.read(str(tmpdir)) | |
186 | |
187 packagerGecko.processJSONFiles(params, files) | |
188 | |
189 assert params['jsonRequires'] == {'prefs.json': {'foo': 'bar'}} | |
190 | |
191 | |
192 @pytest.mark.usefixtures('scripts') | |
193 def test_add_missing_files(tmpdir, gecko_metadata): | |
194 params = { | |
195 'baseDir': str(tmpdir), | |
196 'metadata': gecko_metadata, | |
197 'jsonRequires': {}, | |
198 'multicompartment': True, | |
199 'hasWebExtension': False, | |
200 } | |
201 | |
202 files = Files(packagerGecko.getPackageFiles(params), set()) | |
203 files.read(str(tmpdir)) | |
204 | |
205 packagerGecko.addMissingFiles(params, files) | |
206 | |
207 assert 'let shutdownHandlers = [];' in files['bootstrap.js'] | |
208 assert 'Services.obs.addObserver(' in files['bootstrap.js'] | |
209 for filename in ['bootstrap.js', 'chrome/content/common.js', | |
210 'lib/ext.js', 'lib/hooks.js']: | |
211 assert filename in files | |
212 | |
213 | |
214 @pytest.mark.usefixtures('gecko_metadata', 'locales', 'subscriptions') | |
215 @pytest.mark.parametrize('release', [True, False]) | |
216 @pytest.mark.parametrize('multicompartment', [True, False]) | |
217 @pytest.mark.parametrize('all_locales', ['all', None]) | |
218 def test_create_build(tmpdir, capsys, release, multicompartment, all_locales): | |
219 base_files = [ | |
Vasily Kuznetsov
2017/08/11 16:46:02
Not a big deal, but a small simplification, perhap
| |
220 'bootstrap.js', | |
221 'chrome/locale/de/test.json', | |
222 'chrome/locale/de/test.properties', | |
223 'chrome/locale/en-US/test.json', | |
224 'chrome/locale/en-US/test.properties', | |
225 'install.rdf', | |
226 'subs.xml' | |
227 ] | |
228 | |
229 if all_locales is None: | |
230 expected = base_files | |
231 else: | |
232 expected = base_files + [ | |
233 'chrome/locale/kn/test.json', | |
234 'chrome/locale/kn/test.properties' | |
235 ] | |
236 | |
237 out_file = tmpdir.join('{}_{}_{}.zip'.format( | |
238 all_locales, release, multicompartment)) | |
239 | |
240 out_file = str(out_file) | |
241 | |
242 packagerGecko.createBuild( | |
243 str(tmpdir), | |
244 locales=all_locales, | |
245 outFile=out_file, | |
246 releaseBuild=release, | |
247 multicompartment=multicompartment) | |
248 | |
249 out, err = capsys.readouterr() | |
250 | |
251 assert err ==\ | |
252 "Warning: Mapped file adblockplusui/firstRun.html doesn't exist\n" | |
253 | |
254 with ZipFile(out_file, 'r') as zipfp: | |
255 zipfp.testzip() | |
256 | |
257 filenames = [zipinfo.filename for zipinfo in zipfp.infolist()] | |
258 | |
259 for name in expected: | |
260 assert name in filenames | |
OLD | NEW |