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

Side by Side Diff: tests/test_packagerEdge.py

Issue 29501558: Issue 5383 - Add tests for the Chrome and Firefox packagers (Closed)
Patch Set: Completely purge PIL / Pillow, added edge-extension fixture / assert Created Sept. 11, 2017, 8:43 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 5 import os
8 import shutil
9 import xml.etree.ElementTree as ET
10 import zipfile
11
12 import pytest 6 import pytest
13 7
14 from buildtools import packager, packagerEdge 8 import xml.etree.ElementTree as ET
15 9
16 TEST_DIR = os.path.dirname(__file__) 10 from buildtools.tests.tools import run_webext_build
Vasily Kuznetsov 2017/09/11 12:50:06 You can combine all the imports from `buildtools.t
tlucas 2017/09/12 11:32:10 Acknowledged.
tlucas 2017/09/13 13:43:24 Done.
17 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') 11 from buildtools.tests.tools import locale_files
18 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) 12 from buildtools.tests.tools import copy_metadata
19 MESSAGES_EN_US = json.dumps({ 13 from buildtools.tests.tools import ZipContent
20 'name': {'message': 'Adblock Plus'}, 14 from buildtools.tests.tools import assert_manifest_content
21 'name_devbuild': {'message': 'devbuild-marker'}, 15 from buildtools.tests.tools import assert_all_locales_present
22 'description': { 16 from buildtools.tests.conftest import ALL_LANGUAGES
23 'message': 'Adblock Plus is the most popular ad blocker ever, ' 17 from buildtools import packager
24 'and also supports websites by not blocking ' 18 from buildtools import packagerEdge
25 'unobstrusive ads by default (configurable).'
26 },
27 })
28 19
29 20
30 @pytest.fixture 21 @pytest.fixture
31 def metadata(): 22 def locale_files_edge(tmpdir):
32 """Loaded metadata config.""" 23 return locale_files(ALL_LANGUAGES, '_locales', tmpdir)
33 conf_parser = ConfigParser.ConfigParser() 24
34 conf_parser.read(TEST_METADATA) 25
35 return conf_parser 26 @pytest.fixture
27 def edge_metadata(tmpdir):
28 filename = 'metadata.edge'
29 copy_metadata(filename, tmpdir)
30
31 return packager.readMetadata(str(tmpdir), 'edge')
36 32
37 33
38 @pytest.fixture 34 @pytest.fixture
39 def files(): 35 def files():
40 """Minimal Files() for testing manifest and blockmap.""" 36 """Minimal Files() for testing blockmap."""
37 str500 = b''.join(chr(i % 200 + 30) for i in range(500))
41 files = packager.Files(set(), set()) 38 files = packager.Files(set(), set())
42 for size in ['44', '50', '150']: 39 files['Extension/foo.xml'] = str500
43 files['Assets/logo_{}.png'.format(size)] = CHARS 40 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 41 return files
48 42
49 43
50 @pytest.fixture 44 @pytest.fixture
51 def srcdir(tmpdir): 45 def js_extensions(srcdir):
52 """Source directory for building the package.""" 46 srcdir.mkdir('ext').join('foo.js').write('var foo;')
53 srcdir = tmpdir.mkdir('src') 47 srcdir.join('bar.js').write('var bar;')
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 48
63 49
64 def blockmap2dict(xml_data): 50 def blockmap2dict(xml_data):
65 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" 51 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect."""
66 return { 52 return {
67 file.get('Name'): { 53 file.get('Name'): {
68 'size': file.get('Size'), 54 'size': file.get('Size'),
69 'lfhsize': file.get('LfhSize'), 55 'lfhsize': file.get('LfhSize'),
70 'blocks': [b.get('Hash') for b in file] 56 'blocks': [b.get('Hash') for b in file]
71 } 57 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 'otf': 'application/octet-stream', 108 'otf': 'application/octet-stream',
123 'png': 'image/png', 109 'png': 'image/png',
124 'xml': 'application/xml' 110 'xml': 'application/xml'
125 } 111 }
126 assert ctm_dict['overrides'] == { 112 assert ctm_dict['overrides'] == {
127 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', 113 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml',
128 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' 114 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml'
129 } 115 }
130 116
131 117
132 def test_create_appx_manifest(metadata, files): 118 @pytest.mark.usefixtures('locale_files_edge', 'js_extensions')
133 namespaces = { 119 @pytest.mark.parametrize('build_release', ['release', 'build'])
134 'ns': 'http://schemas.microsoft.com/' 120 def test_build_edge(build_release, tmpdir, srcdir, edge_metadata):
135 'appx/manifest/foundation/windows10', 121 from buildtools import packagerEdge
136 'uap': 'http://schemas.microsoft.com/appx/manifest/uap/windows10', 122 release = build_release == 'release'
137 'uap3': 'http://schemas.microsoft.com/appx/manifest/uap/windows10/3',
138 }
139 123
140 def first(elem): 124 run_webext_build('edge', build_release, srcdir, packagerEdge)
141 return elem[0]
142 125
143 def text(elem): 126 if release:
144 return elem.text 127 out_file = 'adblockplusedge-1.2.3.appx'
128 else:
129 out_file = 'adblockplusedge-1.2.3.0.appx'
145 130
146 def attr(attr): 131 with ZipContent(out_file) as package:
147 def wrapper(elem): 132 filenames = set(package.namelist())
148 return elem.attrib[attr]
149 return wrapper
150 133
151 base = [ 134 assert_all_locales_present(package, 'Extension/_locales')
152 ('.//*', [len], 21.0),
153 ('./ns:Identity', [first, attr('Publisher')],
154 'CN=4F066043-8AFE-41C9-B762-6C15E77E3F88'),
155 ('./ns:Identity', [first, attr('Version')], '1.2.3.0'),
156 ('./ns:Properties/ns:PublisherDisplayName', [first, text],
157 'Eyeo GmbH'),
158 ('./ns:Properties/ns:Logo', [first, text], 'Assets\\logo_50.png'),
159 ('./ns:Dependencies/ns:TargetDeviceFamily',
160 [first, attr('MinVersion')],
161 '10.0.14332.0'),
162 ('./ns:Dependencies/ns:TargetDeviceFamily',
163 [first, attr('MaxVersionTested')],
164 '12.0.0.0'),
165 ('./ns:Applications/ns:Application/uap:VisualElements',
166 [first, attr('Square150x150Logo')],
167 'Assets\\logo_150.png'),
168 ('./ns:Applications/ns:Application/uap:VisualElements',
169 [first, attr('Square44x44Logo')],
170 'Assets\\logo_44.png'),
171 ('./ns:Applications/ns:Application/uap:VisualElements',
172 [first, attr('Description')],
173 'Adblock Plus is the most popular ad blocker ever, and also '
174 'supports websites by not blocking unobstrusive ads by '
175 'default (configurable).'),
176 ('./ns:Applications/ns:Application/uap:VisualElements',
177 [first, attr('BackgroundColor')],
178 'red'),
179 ]
180 135
181 devbuild = base + [ 136 assert 'AppxManifest.xml' in filenames
182 ('./ns:Identity', [first, attr('Name')], 137 assert 'AppxBlockMap.xml' in filenames
183 'EyeoGmbH.AdblockPlusdevelopmentbuild'), 138 assert '[Content_Types].xml' in filenames
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 139
198 release = base + [ 140 assert 'Extension/ext/foo.js' in filenames
199 ('./ns:Identity', [first, attr('Name')], 'EyeoGmbH.AdblockPlus'), 141 assert 'Extension/bar.js' in filenames
200 ('./ns:Properties/ns:DisplayName', [first, text], 'Adblock Plus'),
201 ('./ns:Applications/ns:Application/uap:VisualElements',
202 [first, attr('DisplayName')],
203 'Adblock Plus'),
204 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/'
205 'uap3:AppExtension',
206 [first, attr('Id')],
207 '1.0'),
208 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/'
209 'uap3:AppExtension',
210 [first, attr('DisplayName')],
211 'Adblock Plus'),
212 ]
213 142
214 for release_build, pairs in [(False, devbuild), (True, release)]: 143 assert package.read('Assets/logo_44.png') == '44'
215 manifest = ET.fromstring(packagerEdge.create_appx_manifest( 144 assert package.read('Extension/icons/abp-44.png') == '44'
216 {'metadata': metadata},
217 files,
218 release_build=release_build))
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 145
146 if release:
147 filename = 'manifest_edge_True.xml'
148 else:
149 filename = 'manifest_edge_False.xml'
226 150
227 def test_move_files_to_extension(): 151 expected = os.path.join(
228 files = packager.Files(set(), set()) 152 os.path.dirname(__file__),
229 files['foo.xml'] = CHARS 153 'expecteddata',
230 files['foo/bar.xml'] = CHARS 154 filename,
231 files['Extension/foo.xml'] = CHARS 155 )
232 packagerEdge.move_files_to_extension(files) 156 assert_manifest_content(package.read('AppxManifest.xml'), expected)
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')
OLDNEW

Powered by Google App Engine
This is Rietveld