LEFT | RIGHT |
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 | 5 import ConfigParser |
| 6 import json |
6 import os | 7 import os |
7 import shutil | 8 import shutil |
8 import xml.etree.ElementTree as ET | 9 import xml.etree.ElementTree as ET |
9 import zipfile | 10 import zipfile |
10 | 11 |
11 import pytest | 12 import pytest |
12 | 13 |
13 from buildtools import packager, packagerEdge | 14 from buildtools import packager, packagerEdge |
14 | 15 |
15 TEST_DIR = os.path.dirname(__file__) | 16 TEST_DIR = os.path.dirname(__file__) |
16 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') | 17 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') |
17 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) | 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 }) |
18 | 28 |
19 | 29 |
20 @pytest.fixture | 30 @pytest.fixture |
21 def metadata(): | 31 def metadata(): |
22 """Loaded metadata config.""" | 32 """Loaded metadata config.""" |
23 conf_parser = ConfigParser.ConfigParser() | 33 conf_parser = ConfigParser.ConfigParser() |
24 conf_parser.read(TEST_METADATA) | 34 conf_parser.read(TEST_METADATA) |
25 return conf_parser | 35 return conf_parser |
26 | 36 |
27 | 37 |
28 @pytest.fixture | 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 |
29 def srcdir(tmpdir): | 51 def srcdir(tmpdir): |
30 """Source directory for building the package.""" | 52 """Source directory for building the package.""" |
31 srcdir = tmpdir.mkdir('src') | 53 srcdir = tmpdir.mkdir('src') |
32 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) | 54 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) |
33 for size in ['44', '50', '150']: | 55 for size in ['44', '50', '150']: |
34 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) | 56 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) |
35 path.write(size, ensure=True) | 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) |
36 return srcdir | 61 return srcdir |
37 | 62 |
38 | 63 |
39 def blockmap2dict(xml_data): | 64 def blockmap2dict(xml_data): |
40 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" | 65 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
41 return { | 66 return { |
42 file.get('Name'): { | 67 file.get('Name'): { |
43 'size': file.get('Size'), | 68 'size': file.get('Size'), |
44 'lfhsize': file.get('LfhSize'), | 69 'lfhsize': file.get('LfhSize'), |
45 'blocks': [ | 70 'blocks': [b.get('Hash') for b in file] |
46 {'hash': b.get('Hash'), 'size': b.get('Size', None)} | |
47 for b in file | |
48 ] | |
49 } | 71 } |
50 for file in ET.fromstring(xml_data) | 72 for file in ET.fromstring(xml_data) |
51 } | 73 } |
52 | 74 |
53 | 75 |
54 def test_create_appx_blockmap(): | 76 def test_create_appx_blockmap(files): |
55 files = packager.Files(set(), set()) | |
56 files['foo.xml'] = CHARS | |
57 files['foo/bar.png'] = CHARS * 200 | |
58 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) | 77 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) |
59 assert blockmap['foo.xml'] == { | 78 assert blockmap['Extension\\foo.xml'] == { |
60 'size': '500', | 79 'size': '500', |
61 'lfhsize': '37', | 80 'lfhsize': '47', |
| 81 'blocks': ['Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0='] |
| 82 } |
| 83 assert blockmap['Extension\\bar.png'] == { |
| 84 'size': '100000', |
| 85 'lfhsize': '47', |
62 'blocks': [ | 86 'blocks': [ |
63 {'hash': 'Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0=', | 87 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
64 'size': None} | 88 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
65 ] | |
66 } | |
67 assert blockmap['foo\\bar.png'] == { | |
68 'size': '100000', | |
69 'lfhsize': '41', | |
70 'blocks': [ | |
71 {'hash': 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', | |
72 'size': None}, | |
73 {'hash': 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', | |
74 'size': None} | |
75 ] | 89 ] |
76 } | 90 } |
77 | 91 |
78 | 92 |
79 def ctm2dict(content_types_map): | 93 def ctm2dict(content_types_map): |
80 """Convert content type map to a dict.""" | 94 """Convert content type map to a dict.""" |
81 ret = {'defaults': {}, 'overrides': {}} | 95 ret = {'defaults': {}, 'overrides': {}} |
82 for node in ET.fromstring(content_types_map): | 96 for node in ET.fromstring(content_types_map): |
83 ct = node.get('ContentType') | 97 ct = node.get('ContentType') |
84 if node.tag.endswith('Default'): | 98 if node.tag.endswith('Default'): |
(...skipping 23 matching lines...) Expand all Loading... |
108 'otf': 'application/x-font-otf', | 122 'otf': 'application/x-font-otf', |
109 'png': 'image/png', | 123 'png': 'image/png', |
110 'xml': 'application/xml' | 124 'xml': 'application/xml' |
111 } | 125 } |
112 assert ctm_dict['overrides'] == { | 126 assert ctm_dict['overrides'] == { |
113 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | 127 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
114 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | 128 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
115 } | 129 } |
116 | 130 |
117 | 131 |
118 def test_create_appx_manifest(metadata): | 132 def test_create_appx_manifest(metadata, files): |
119 files = packager.Files(set(), set()) | 133 manifest = packagerEdge.create_appx_manifest( |
120 for size in ['44', '50', '150']: | 134 {'metadata': metadata}, files, release_build=True, |
121 files['Assets/logo_{}.png'.format(size)] = CHARS | 135 ) |
122 manifest = packagerEdge.create_appx_manifest({'metadata': metadata}, files) | |
123 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: | 136 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
124 manifest_expect = fp.read() | 137 manifest_expect = fp.read() |
125 assert manifest.strip() == manifest_expect.strip() | 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 |
126 | 146 |
127 | 147 |
128 def test_move_files_to_extension(): | 148 def test_move_files_to_extension(): |
129 files = packager.Files(set(), set()) | 149 files = packager.Files(set(), set()) |
130 files['foo.xml'] = CHARS | 150 files['foo.xml'] = CHARS |
131 files['foo/bar.xml'] = CHARS | 151 files['foo/bar.xml'] = CHARS |
132 files['Extension/foo.xml'] = CHARS | 152 files['Extension/foo.xml'] = CHARS |
133 packagerEdge.move_files_to_extension(files) | 153 packagerEdge.move_files_to_extension(files) |
134 assert set(files.keys()) == { | 154 assert set(files.keys()) == { |
135 'Extension/foo.xml', | 155 'Extension/foo.xml', |
136 'Extension/foo/bar.xml', | 156 'Extension/foo/bar.xml', |
137 'Extension/Extension/foo.xml' | 157 'Extension/Extension/foo.xml' |
138 } | 158 } |
139 | 159 |
140 | 160 |
141 def test_create_build(tmpdir, srcdir): | 161 def test_create_build(tmpdir, srcdir): |
142 out_file = str(tmpdir.join('abp.appx')) | 162 out_file = str(tmpdir.join('abp.appx')) |
143 packagerEdge.createBuild(str(srcdir), outFile=out_file) | 163 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=True) |
144 appx = zipfile.ZipFile(out_file) | 164 appx = zipfile.ZipFile(out_file) |
145 | 165 |
146 names = set(appx.namelist()) | 166 names = set(appx.namelist()) |
147 assert 'AppxManifest.xml' in names | 167 assert 'AppxManifest.xml' in names |
148 assert 'AppxBlockMap.xml' in names | 168 assert 'AppxBlockMap.xml' in names |
149 assert '[Content_Types].xml' in names | 169 assert '[Content_Types].xml' in names |
150 | 170 |
| 171 assert 'devbuild-marker' not in appx.read('AppxManifest.xml') |
151 assert appx.read('Assets/logo_44.png') == '44' | 172 assert appx.read('Assets/logo_44.png') == '44' |
152 assert appx.read('Extension/icons/abp-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') |
LEFT | RIGHT |