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 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 |
| 20 |
| 21 @pytest.fixture |
| 22 def metadata(): |
| 23 """Loaded metadata config.""" |
| 24 conf_parser = ConfigParser.ConfigParser() |
| 25 conf_parser.read(TEST_METADATA) |
| 26 return conf_parser |
| 27 |
| 28 |
| 29 @pytest.fixture |
| 30 def srcdir(tmpdir): |
| 31 """Source directory for building the package.""" |
| 32 srcdir = tmpdir.mkdir('src') |
| 33 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) |
| 34 for size in ['44', '50', '150']: |
| 35 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) |
| 36 path.write(size, ensure=True) |
| 37 return srcdir |
| 38 |
| 39 |
| 40 def blockmap2dict(xml_data): |
| 41 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
| 42 return { |
| 43 file.get('Name'): { |
| 44 'size': file.get('Size'), |
| 45 'lfhsize': file.get('LfhSize'), |
| 46 'blocks': [ |
| 47 {'hash': b.get('Hash'), 'size': b.get('Size', None)} |
| 48 for b in file |
| 49 ] |
| 50 } |
| 51 for file in ET.fromstring(xml_data) |
| 52 } |
| 53 |
| 54 |
| 55 def test_create_appx_blockmap(): |
| 56 files = packager.Files(set(), set()) |
| 57 files['foo.xml'] = CHARS |
| 58 files['foo/bar.png'] = CHARS * 200 |
| 59 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) |
| 60 assert blockmap['foo.xml'] == { |
| 61 'size': '500', |
| 62 'lfhsize': '37', |
| 63 'blocks': [ |
| 64 {'hash': 'Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0=', |
| 65 'size': '500'} |
| 66 ] |
| 67 } |
| 68 assert blockmap['foo\\bar.png'] == { |
| 69 'size': '100000', |
| 70 'lfhsize': '41', |
| 71 'blocks': [ |
| 72 {'hash': 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
| 73 'size': '65536'}, |
| 74 {'hash': 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
| 75 'size': '34464'} |
| 76 ] |
| 77 } |
| 78 |
| 79 |
| 80 def test_create_appx_manifest(metadata): |
| 81 files = packager.Files(set(), set()) |
| 82 for size in ['44', '50', '150']: |
| 83 files['Assets/logo_{}.png'.format(size)] = CHARS |
| 84 manifest = packagerEdge.create_appx_manifest({'metadata': metadata}, files) |
| 85 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
| 86 manifest_expect = fp.read() |
| 87 assert manifest.strip() == manifest_expect.strip() |
| 88 |
| 89 |
| 90 def test_move_files_to_extension(): |
| 91 files = packager.Files(set(), set()) |
| 92 files['foo.xml'] = CHARS |
| 93 files['foo/bar.xml'] = CHARS |
| 94 files['Extension/foo.xml'] = CHARS |
| 95 packagerEdge.move_files_to_extension(files) |
| 96 assert set(files.keys()) == { |
| 97 'Extension/foo.xml', |
| 98 'Extension/foo/bar.xml', |
| 99 'Extension/Extension/foo.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 |