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 io |
| 7 import os |
| 8 import shutil |
| 9 import sys |
| 10 import xml.etree.ElementTree as ET |
| 11 import zipfile |
| 12 |
| 13 import pytest |
| 14 |
| 15 TEST_DIR = os.path.dirname(__file__) |
| 16 |
| 17 sys.path.insert(0, os.path.dirname(os.path.dirname(TEST_DIR))) |
| 18 |
| 19 from buildtools import packager, packagerEdge # noqa: must modify path before t
his. |
| 20 |
| 21 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') |
| 22 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) |
| 23 |
| 24 |
| 25 @pytest.fixture |
| 26 def metadata(): |
| 27 """Loaded metadata config.""" |
| 28 conf_parser = ConfigParser.ConfigParser() |
| 29 conf_parser.read(TEST_METADATA) |
| 30 return conf_parser |
| 31 |
| 32 |
| 33 @pytest.fixture |
| 34 def srcdir(tmpdir): |
| 35 """Source directory for building the package.""" |
| 36 srcdir = tmpdir.mkdir('src') |
| 37 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) |
| 38 for size in ['44', '50', '150']: |
| 39 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) |
| 40 path.write(size, ensure=True) |
| 41 return srcdir |
| 42 |
| 43 |
| 44 def blockmap2dict(xml_data): |
| 45 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
| 46 return { |
| 47 file.get('Name'): { |
| 48 'size': file.get('Size'), |
| 49 'lfhsize': file.get('LfhSize'), |
| 50 'blocks': [ |
| 51 {'hash': b.get('Hash'), 'size': b.get('Size', None)} |
| 52 for b in file |
| 53 ] |
| 54 } |
| 55 for file in ET.fromstring(xml_data) |
| 56 } |
| 57 |
| 58 |
| 59 def test_create_appx_blockmap(): |
| 60 files = packager.Files(set(), set()) |
| 61 files['foo.xml'] = CHARS |
| 62 files['foo/bar.png'] = CHARS * 200 |
| 63 blockmap = blockmap2dict(packagerEdge.createAppxBlockmap(files)) |
| 64 assert blockmap['foo.xml'] == { |
| 65 'size': '500', |
| 66 'lfhsize': '37', |
| 67 'blocks': [ |
| 68 {'hash': 'Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0=', |
| 69 'size': '500'} |
| 70 ] |
| 71 } |
| 72 assert blockmap['foo\\bar.png'] == { |
| 73 'size': '100000', |
| 74 'lfhsize': '41', |
| 75 'blocks': [ |
| 76 {'hash': 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
| 77 'size': '65536'}, |
| 78 {'hash': 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
| 79 'size': '34464'} |
| 80 ] |
| 81 } |
| 82 |
| 83 |
| 84 def test_create_appx_manifest(metadata): |
| 85 manifest = packagerEdge.createAppxManifest({'metadata': metadata}) |
| 86 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
| 87 manifest_expect = fp.read() |
| 88 assert manifest.strip() == manifest_expect.strip() |
| 89 |
| 90 |
| 91 def test_convert_to_appx(): |
| 92 files = packager.Files(set(), set()) |
| 93 files['foo.xml'] = CHARS |
| 94 files['foo/bar.xml'] = CHARS |
| 95 packagerEdge.convertToAppx(files) |
| 96 assert set(files.keys()) == { |
| 97 'Extension/foo.xml', |
| 98 'Extension/foo/bar.xml', |
| 99 '[Content_Types].xml' |
| 100 } |
| 101 |
| 102 |
| 103 def test_edge_files_zip(): |
| 104 """Test zip conversion of EdgeFiles that is overriden.""" |
| 105 files = packagerEdge.Files(set(), set()) |
| 106 files['Foo.xml'] = CHARS |
| 107 files['bar.xml'] = CHARS |
| 108 buffer = io.BytesIO() |
| 109 files.zip(buffer, sortKey=lambda name: name.lower()) |
| 110 result = zipfile.ZipFile(buffer) |
| 111 assert result.getinfo('Foo.xml').compress_type == zipfile.ZIP_STORED |
| 112 assert result.namelist() == ['bar.xml', 'Foo.xml', 'AppxBlockMap.xml'] |
| 113 |
| 114 |
| 115 def test_create_build(tmpdir, srcdir): |
| 116 out_file = str(tmpdir.join('abp.appx')) |
| 117 packagerEdge.createBuild(str(srcdir), outFile=out_file) |
| 118 appx = zipfile.ZipFile(out_file) |
| 119 |
| 120 names = set(appx.namelist()) |
| 121 assert 'AppxManifest.xml' in names |
| 122 assert 'AppxBlockMap.xml' in names |
| 123 assert '[Content_Types].xml' in names |
| 124 |
| 125 assert appx.read('Assets/logo_44.png') == '44' |
| 126 assert appx.read('Extension/icons/abp-44.png') == '44' |
OLD | NEW |