Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This Source Code Form is subject to the terms of the Mozilla Public | 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 | 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/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import ConfigParser | |
6 import json | |
7 import os | |
8 import shutil | |
9 import xml.etree.ElementTree as ET | |
10 import zipfile | |
11 | |
12 import pytest | 5 import pytest |
13 | 6 |
7 import xml.etree.ElementTree as ET | |
Vasily Kuznetsov
2017/10/21 17:41:09
This should be imported before pytest: xml.etree i
tlucas
2017/10/22 11:19:12
Done.
| |
8 | |
14 from buildtools import packager, packagerEdge | 9 from buildtools import packager, packagerEdge |
15 | 10 |
16 TEST_DIR = os.path.dirname(__file__) | |
17 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') | |
18 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) | |
19 MESSAGES_EN_US = json.dumps({ | |
20 'name': {'message': 'Adblock Plus'}, | |
21 'name_devbuild': {'message': 'devbuild-marker'}, | |
22 'description': { | |
23 'message': 'Adblock Plus is the most popular ad blocker ever, ' | |
24 'and also supports websites by not blocking ' | |
25 'unobstrusive ads by default (configurable).' | |
26 }, | |
27 }) | |
28 | |
29 | |
30 @pytest.fixture | |
31 def metadata(): | |
32 """Loaded metadata config.""" | |
33 conf_parser = ConfigParser.ConfigParser() | |
34 conf_parser.read(TEST_METADATA) | |
35 return conf_parser | |
36 | |
37 | 11 |
38 @pytest.fixture | 12 @pytest.fixture |
39 def files(): | 13 def files(): |
40 """Minimal Files() for testing manifest and blockmap.""" | 14 """Minimal Files() for testing blockmap.""" |
15 str500 = b''.join(chr(i % 200 + 30) for i in range(500)) | |
41 files = packager.Files(set(), set()) | 16 files = packager.Files(set(), set()) |
42 for size in ['44', '50', '150']: | 17 files['Extension/foo.xml'] = str500 |
43 files['Assets/logo_{}.png'.format(size)] = CHARS | 18 files['Extension/bar.png'] = str500 * 200 |
44 files['Extension/_locales/en_US/messages.json'] = MESSAGES_EN_US | |
45 files['Extension/foo.xml'] = CHARS | |
46 files['Extension/bar.png'] = CHARS * 200 | |
47 return files | 19 return files |
48 | 20 |
49 | 21 |
50 @pytest.fixture | |
51 def srcdir(tmpdir): | |
52 """Source directory for building the package.""" | |
53 srcdir = tmpdir.mkdir('src') | |
54 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) | |
55 for size in ['44', '50', '150']: | |
56 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) | |
57 path.write(size, ensure=True) | |
58 localedir = srcdir.mkdir('_locales') | |
59 en_us_dir = localedir.mkdir('en_US') | |
60 en_us_dir.join('messages.json').write(MESSAGES_EN_US) | |
61 return srcdir | |
62 | |
63 | |
64 def blockmap2dict(xml_data): | 22 def blockmap2dict(xml_data): |
65 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" | 23 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
66 return { | 24 return { |
67 file.get('Name'): { | 25 file.get('Name'): { |
68 'size': file.get('Size'), | 26 'size': file.get('Size'), |
69 'lfhsize': file.get('LfhSize'), | 27 'lfhsize': file.get('LfhSize'), |
70 'blocks': [b.get('Hash') for b in file] | 28 'blocks': [b.get('Hash') for b in file] |
71 } | 29 } |
72 for file in ET.fromstring(xml_data) | 30 for file in ET.fromstring(xml_data) |
73 } | 31 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 'js': 'application/javascript', | 78 'js': 'application/javascript', |
121 'json': 'application/json', | 79 'json': 'application/json', |
122 'otf': 'application/octet-stream', | 80 'otf': 'application/octet-stream', |
123 'png': 'image/png', | 81 'png': 'image/png', |
124 'xml': 'application/xml' | 82 'xml': 'application/xml' |
125 } | 83 } |
126 assert ctm_dict['overrides'] == { | 84 assert ctm_dict['overrides'] == { |
127 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | 85 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
128 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | 86 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
129 } | 87 } |
130 | |
131 | |
132 def test_create_appx_manifest(metadata, files): | |
133 namespaces = { | |
134 'ns': 'http://schemas.microsoft.com/' | |
135 'appx/manifest/foundation/windows10', | |
136 'uap': 'http://schemas.microsoft.com/appx/manifest/uap/windows10', | |
137 'uap3': 'http://schemas.microsoft.com/appx/manifest/uap/windows10/3', | |
138 } | |
139 | |
140 def first(elem): | |
141 return elem[0] | |
142 | |
143 def text(elem): | |
144 return elem.text | |
145 | |
146 def attr(attr): | |
147 def wrapper(elem): | |
148 return elem.attrib[attr] | |
149 return wrapper | |
150 | |
151 base = [ | |
152 ('.//*', [len], 21.0), | |
153 ('./ns:Identity', [first, attr('Publisher')], | |
154 'CN=4F066043-8AFE-41C9-B762-6C15E77E3F88'), | |
155 ('./ns:Properties/ns:PublisherDisplayName', [first, text], | |
156 'Eyeo GmbH'), | |
157 ('./ns:Properties/ns:Logo', [first, text], 'Assets\\logo_50.png'), | |
158 ('./ns:Dependencies/ns:TargetDeviceFamily', | |
159 [first, attr('MinVersion')], | |
160 '10.0.14332.0'), | |
161 ('./ns:Dependencies/ns:TargetDeviceFamily', | |
162 [first, attr('MaxVersionTested')], | |
163 '12.0.0.0'), | |
164 ('./ns:Applications/ns:Application/uap:VisualElements', | |
165 [first, attr('Square150x150Logo')], | |
166 'Assets\\logo_150.png'), | |
167 ('./ns:Applications/ns:Application/uap:VisualElements', | |
168 [first, attr('Square44x44Logo')], | |
169 'Assets\\logo_44.png'), | |
170 ('./ns:Applications/ns:Application/uap:VisualElements', | |
171 [first, attr('Description')], | |
172 'Adblock Plus is the most popular ad blocker ever, and also ' | |
173 'supports websites by not blocking unobstrusive ads by ' | |
174 'default (configurable).'), | |
175 ('./ns:Applications/ns:Application/uap:VisualElements', | |
176 [first, attr('BackgroundColor')], | |
177 'red'), | |
178 ] | |
179 | |
180 devbuild = base + [ | |
181 ('./ns:Identity', [first, attr('Name')], | |
182 'EyeoGmbH.AdblockPlusdevelopmentbuild'), | |
183 ('./ns:Identity', [first, attr('Version')], '1.2.1000.0'), | |
184 ('./ns:Properties/ns:DisplayName', [first, text], 'devbuild-marker'), | |
185 ('./ns:Applications/ns:Application/uap:VisualElements', | |
186 [first, attr('DisplayName')], | |
187 'devbuild-marker'), | |
188 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
189 'uap3:AppExtension', | |
190 [first, attr('Id')], | |
191 'EdgeExtension'), | |
192 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
193 'uap3:AppExtension', | |
194 [first, attr('DisplayName')], | |
195 'devbuild-marker'), | |
196 ] | |
197 | |
198 release = base + [ | |
199 ('./ns:Identity', [first, attr('Name')], 'EyeoGmbH.AdblockPlus'), | |
200 ('./ns:Identity', [first, attr('Version')], '1.2.3.0'), | |
201 ('./ns:Properties/ns:DisplayName', [first, text], 'Adblock Plus'), | |
202 ('./ns:Applications/ns:Application/uap:VisualElements', | |
203 [first, attr('DisplayName')], | |
204 'Adblock Plus'), | |
205 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
206 'uap3:AppExtension', | |
207 [first, attr('Id')], | |
208 '1.0'), | |
209 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
210 'uap3:AppExtension', | |
211 [first, attr('DisplayName')], | |
212 'Adblock Plus'), | |
213 ] | |
214 | |
215 for args, pairs in [(('1000', False), devbuild), ((None, True), release)]: | |
216 manifest = ET.fromstring(packagerEdge.create_appx_manifest( | |
217 {'metadata': metadata}, files, *args | |
218 )) | |
219 for expression, modifiers, value in pairs: | |
220 res = reduce( | |
221 lambda val, func: func(val), | |
222 modifiers, | |
223 manifest.findall(expression, namespaces=namespaces)) | |
224 assert res == value | |
225 | |
226 | |
227 def test_move_files_to_extension(): | |
228 files = packager.Files(set(), set()) | |
229 files['foo.xml'] = CHARS | |
230 files['foo/bar.xml'] = CHARS | |
231 files['Extension/foo.xml'] = CHARS | |
232 packagerEdge.move_files_to_extension(files) | |
233 assert set(files.keys()) == { | |
234 'Extension/foo.xml', | |
235 'Extension/foo/bar.xml', | |
236 'Extension/Extension/foo.xml' | |
237 } | |
238 | |
239 | |
240 def test_create_build(tmpdir, srcdir): | |
241 out_file = str(tmpdir.join('abp.appx')) | |
242 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=True) | |
243 appx = zipfile.ZipFile(out_file) | |
244 | |
245 names = set(appx.namelist()) | |
246 assert 'AppxManifest.xml' in names | |
247 assert 'AppxBlockMap.xml' in names | |
248 assert '[Content_Types].xml' in names | |
249 | |
250 assert 'devbuild-marker' not in appx.read('AppxManifest.xml') | |
251 assert appx.read('Assets/logo_44.png') == '44' | |
252 assert appx.read('Extension/icons/abp-44.png') == '44' | |
253 | |
254 | |
255 def test_create_devbuild(tmpdir, srcdir): | |
256 out_file = str(tmpdir.join('abp.appx')) | |
257 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=False) | |
258 appx = zipfile.ZipFile(out_file) | |
259 assert 'devbuild-marker' in appx.read('AppxManifest.xml') | |
OLD | NEW |