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(( | |
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) | |
Vasily Kuznetsov
2017/08/10 19:48:28
Nit: comma at the end of the line.
tlucas
2017/08/11 12:17:09
Done.
| |
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 | |
Vasily Kuznetsov
2017/08/10 19:48:28
Nit: comma at the end of the line.
tlucas
2017/08/11 12:17:09
Done.
| |
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(files, tmpdir, gecko_metadata): | |
150 locale_dir = tmpdir.dirpath(tmpdir.basename, 'chrome', 'locale') | |
151 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.
| |
152 .write(MESSAGES.format('fr')) | |
153 | |
154 locales = packagerGecko.getLocales(str(tmpdir), False) | |
155 | |
156 params = { | |
157 'metadata': gecko_metadata, | |
158 'locales': locales, | |
159 'baseDir': str(tmpdir) | |
160 } | |
161 | |
162 # Should add missing fr/test.properties to files | |
163 packagerGecko.fixupLocales(params, files) | |
164 | |
165 packagerGecko.importLocales(params, files) | |
166 for locale in locales: | |
167 properties = files['chrome/locale/{}/test.properties'.format(locale)] | |
168 translation_data = list( | |
169 localeTools.parsePropertiesString(properties, '')) | |
170 | |
171 for trans in [ | |
172 ('name', None, 'Name translated'), | |
173 ('description', None, 'Description translated')]: | |
174 assert trans in translation_data | |
175 | |
176 | |
177 def test_process_json_files(tmpdir, prefs_json): | |
178 params = { | |
179 'baseDir': str(tmpdir), | |
180 'jsonRequires': {}, | |
181 } | |
182 | |
183 files = Files(packagerGecko.getPackageFiles(params), set()) | |
184 files.read(str(tmpdir)) | |
185 | |
186 packagerGecko.processJSONFiles(params, files) | |
187 | |
188 assert params['jsonRequires'] == {'prefs.json': {'foo': 'bar'}} | |
189 | |
190 | |
191 @pytest.mark.usefixtures('scripts') | |
192 def test_add_missing_files(tmpdir, gecko_metadata): | |
193 params = { | |
194 'baseDir': str(tmpdir), | |
195 'metadata': gecko_metadata, | |
196 'jsonRequires': {}, | |
197 'multicompartment': True, | |
198 'hasWebExtension': False, | |
199 } | |
200 | |
201 files = Files(packagerGecko.getPackageFiles(params), set()) | |
202 files.read(str(tmpdir)) | |
203 | |
204 packagerGecko.addMissingFiles(params, files) | |
205 | |
206 assert 'let shutdownHandlers = [];' in files['bootstrap.js'] | |
207 assert 'Services.obs.addObserver(' in files['bootstrap.js'] | |
208 for filename in ['bootstrap.js', 'chrome/content/common.js', | |
209 'lib/ext.js', 'lib/hooks.js']: | |
210 assert filename in files | |
211 | |
212 | |
213 @pytest.mark.usefixtures('gecko_metadata', 'locales', 'subscriptions') | |
214 @pytest.mark.parametrize('release', [True, False]) | |
215 @pytest.mark.parametrize('multicompartment', [True, False]) | |
216 @pytest.mark.parametrize('all_locales', ['all', None]) | |
217 def test_create_build(tmpdir, capsys, release, multicompartment, all_locales): | |
218 base_files = [ | |
219 'bootstrap.js', | |
220 'chrome/locale/de/test.json', | |
221 'chrome/locale/de/test.properties', | |
222 'chrome/locale/en-US/test.json', | |
223 'chrome/locale/en-US/test.properties', | |
224 'install.rdf', | |
225 'subs.xml' | |
226 ] | |
227 | |
228 if all_locales is None: | |
229 expected = base_files | |
230 else: | |
231 expected = base_files + [ | |
232 'chrome/locale/kn/test.json', | |
233 'chrome/locale/kn/test.properties' | |
234 ] | |
235 | |
236 out_file = tmpdir.join('{}_{}_{}.zip'.format( | |
237 all_locales, release, multicompartment)) | |
238 | |
239 out_file = str(out_file) | |
240 | |
241 packagerGecko.createBuild( | |
242 str(tmpdir), | |
243 locales=all_locales, | |
244 outFile=out_file, | |
245 releaseBuild=release, | |
246 multicompartment=multicompartment) | |
247 | |
248 out, err = capsys.readouterr() | |
249 | |
250 assert err ==\ | |
251 "Warning: Mapped file adblockplusui/firstRun.html doesn't exist\n" | |
252 | |
253 with ZipFile(out_file, 'r') as zipfp: | |
254 zipfp.testzip() | |
255 | |
256 filenames = [zipinfo.filename for zipinfo in zipfp.infolist()] | |
257 | |
258 for name in expected: | |
259 assert name in filenames | |
OLD | NEW |