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 ConfigParser |
| 6 import json |
| 7 import os |
| 8 import shutil |
| 9 import xml.etree.ElementTree as ET |
| 10 import zipfile |
| 11 |
| 12 import pytest |
| 13 |
| 14 from buildtools import packager, packagerEdge |
| 15 |
| 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 |
| 38 @pytest.fixture |
| 39 def files(): |
| 40 """Minimal Files() for testing manifest and blockmap.""" |
| 41 files = packager.Files(set(), set()) |
| 42 for size in ['44', '50', '150']: |
| 43 files['Assets/logo_{}.png'.format(size)] = CHARS |
| 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 |
| 48 |
| 49 |
| 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): |
| 65 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
| 66 return { |
| 67 file.get('Name'): { |
| 68 'size': file.get('Size'), |
| 69 'lfhsize': file.get('LfhSize'), |
| 70 'blocks': [b.get('Hash') for b in file] |
| 71 } |
| 72 for file in ET.fromstring(xml_data) |
| 73 } |
| 74 |
| 75 |
| 76 def test_create_appx_blockmap(files): |
| 77 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) |
| 78 assert blockmap['Extension\\foo.xml'] == { |
| 79 'size': '500', |
| 80 'lfhsize': '47', |
| 81 'blocks': ['Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0='] |
| 82 } |
| 83 assert blockmap['Extension\\bar.png'] == { |
| 84 'size': '100000', |
| 85 'lfhsize': '47', |
| 86 'blocks': [ |
| 87 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
| 88 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
| 89 ] |
| 90 } |
| 91 |
| 92 |
| 93 def ctm2dict(content_types_map): |
| 94 """Convert content type map to a dict.""" |
| 95 ret = {'defaults': {}, 'overrides': {}} |
| 96 for node in ET.fromstring(content_types_map): |
| 97 ct = node.get('ContentType') |
| 98 if node.tag.endswith('Default'): |
| 99 ret['defaults'][node.get('Extension')] = ct |
| 100 elif node.tag.endswith('Override'): |
| 101 ret['overrides'][node.get('PartName')] = ct |
| 102 else: |
| 103 raise ValueError('Unrecognised tag in content map: ' + node.tag) |
| 104 return ret |
| 105 |
| 106 |
| 107 def test_empty_content_types_map(): |
| 108 ctm_dict = ctm2dict(packagerEdge.create_content_types_map([])) |
| 109 assert ctm_dict['defaults'] == {} |
| 110 assert ctm_dict['overrides'] == {} |
| 111 |
| 112 |
| 113 def test_full_content_types_map(): |
| 114 filenames = ['no-extension', packagerEdge.MANIFEST, packagerEdge.BLOCKMAP] |
| 115 filenames += ['file.' + x for x in 'json html js png css git otf'.split()] |
| 116 ctm_dict = ctm2dict(packagerEdge.create_content_types_map(filenames)) |
| 117 assert ctm_dict['defaults'] == { |
| 118 'css': 'text/css', |
| 119 'html': 'text/html', |
| 120 'js': 'application/javascript', |
| 121 'json': 'application/json', |
| 122 'otf': 'application/x-font-otf', |
| 123 'png': 'image/png', |
| 124 'xml': 'application/xml' |
| 125 } |
| 126 assert ctm_dict['overrides'] == { |
| 127 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
| 128 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
| 129 } |
| 130 |
| 131 |
| 132 def test_create_appx_manifest(metadata, files): |
| 133 manifest = packagerEdge.create_appx_manifest( |
| 134 {'metadata': metadata}, files, release_build=True, |
| 135 ) |
| 136 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
| 137 manifest_expect = fp.read() |
| 138 assert manifest.strip() == manifest_expect.strip() |
| 139 |
| 140 |
| 141 def test_create_devbuild_appx_manifest(metadata, files): |
| 142 manifest = packagerEdge.create_appx_manifest( |
| 143 {'metadata': metadata}, files, release_build=False, |
| 144 ) |
| 145 assert 'devbuild-marker' in manifest |
| 146 |
| 147 |
| 148 def test_move_files_to_extension(): |
| 149 files = packager.Files(set(), set()) |
| 150 files['foo.xml'] = CHARS |
| 151 files['foo/bar.xml'] = CHARS |
| 152 files['Extension/foo.xml'] = CHARS |
| 153 packagerEdge.move_files_to_extension(files) |
| 154 assert set(files.keys()) == { |
| 155 'Extension/foo.xml', |
| 156 'Extension/foo/bar.xml', |
| 157 'Extension/Extension/foo.xml' |
| 158 } |
| 159 |
| 160 |
| 161 def test_create_build(tmpdir, srcdir): |
| 162 out_file = str(tmpdir.join('abp.appx')) |
| 163 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=True) |
| 164 appx = zipfile.ZipFile(out_file) |
| 165 |
| 166 names = set(appx.namelist()) |
| 167 assert 'AppxManifest.xml' in names |
| 168 assert 'AppxBlockMap.xml' in names |
| 169 assert '[Content_Types].xml' in names |
| 170 |
| 171 assert 'devbuild-marker' not in appx.read('AppxManifest.xml') |
| 172 assert appx.read('Assets/logo_44.png') == '44' |
| 173 assert appx.read('Extension/icons/abp-44.png') == '44' |
| 174 |
| 175 |
| 176 def test_create_devbuild(tmpdir, srcdir): |
| 177 out_file = str(tmpdir.join('abp.appx')) |
| 178 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=False) |
| 179 appx = zipfile.ZipFile(out_file) |
| 180 assert 'devbuild-marker' in appx.read('AppxManifest.xml') |
OLD | NEW |