| 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 os | 
 |    7 import shutil | 
 |    8 import xml.etree.ElementTree as ET | 
 |    9 import zipfile | 
 |   10  | 
 |   11 import pytest | 
 |   12  | 
 |   13 from buildtools import packager, packagerEdge | 
 |   14  | 
 |   15 TEST_DIR = os.path.dirname(__file__) | 
 |   16 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') | 
 |   17 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) | 
 |   18  | 
 |   19  | 
 |   20 @pytest.fixture | 
 |   21 def metadata(): | 
 |   22     """Loaded metadata config.""" | 
 |   23     conf_parser = ConfigParser.ConfigParser() | 
 |   24     conf_parser.read(TEST_METADATA) | 
 |   25     return conf_parser | 
 |   26  | 
 |   27  | 
 |   28 @pytest.fixture | 
 |   29 def srcdir(tmpdir): | 
 |   30     """Source directory for building the package.""" | 
 |   31     srcdir = tmpdir.mkdir('src') | 
 |   32     shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) | 
 |   33     for size in ['44', '50', '150']: | 
 |   34         path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) | 
 |   35         path.write(size, ensure=True) | 
 |   36     return srcdir | 
 |   37  | 
 |   38  | 
 |   39 def blockmap2dict(xml_data): | 
 |   40     """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" | 
 |   41     return { | 
 |   42         file.get('Name'): { | 
 |   43             'size': file.get('Size'), | 
 |   44             'lfhsize': file.get('LfhSize'), | 
 |   45             'blocks': [ | 
 |   46                 {'hash': b.get('Hash'), 'size': b.get('Size', None)} | 
 |   47                 for b in file | 
 |   48             ] | 
 |   49         } | 
 |   50         for file in ET.fromstring(xml_data) | 
 |   51     } | 
 |   52  | 
 |   53  | 
 |   54 def test_create_appx_blockmap(): | 
 |   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)) | 
 |   59     assert blockmap['foo.xml'] == { | 
 |   60         'size': '500', | 
 |   61         'lfhsize': '37', | 
 |   62         'blocks': [ | 
 |   63             {'hash': 'Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0=', | 
 |   64              'size': None} | 
 |   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         ] | 
 |   76     } | 
 |   77  | 
 |   78  | 
 |   79 def ctm2dict(content_types_map): | 
 |   80     """Convert content type map to a dict.""" | 
 |   81     ret = {'defaults': {}, 'overrides': {}} | 
 |   82     for node in ET.fromstring(content_types_map): | 
 |   83         ct = node.get('ContentType') | 
 |   84         if node.tag.endswith('Default'): | 
 |   85             ret['defaults'][node.get('Extension')] = ct | 
 |   86         elif node.tag.endswith('Override'): | 
 |   87             ret['overrides'][node.get('PartName')] = ct | 
 |   88         else: | 
 |   89             raise ValueError('Unrecognised tag in content map: ' + node.tag) | 
 |   90     return ret | 
 |   91  | 
 |   92  | 
 |   93 def test_empty_content_types_map(): | 
 |   94     files = packager.Files(set(), set()) | 
 |   95     ctm_dict = ctm2dict(packagerEdge.create_content_types_map(files)) | 
 |   96     assert ctm_dict['defaults'] == {'xml': 'text/xml'} | 
 |   97     assert ctm_dict['overrides'] == { | 
 |   98         '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | 
 |   99         '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | 
 |  100     } | 
 |  101  | 
 |  102  | 
 |  103 def test_full_content_types_map(): | 
 |  104     files = packager.Files(set(), set()) | 
 |  105     files['no-extension'] = 'data' | 
 |  106     for ext in 'json html js png css git otf'.split(): | 
 |  107         files['file.' + ext] = 'data' | 
 |  108     ctm_dict = ctm2dict(packagerEdge.create_content_types_map(files)) | 
 |  109     import pprint | 
 |  110     pprint.pprint(ctm_dict['defaults']) | 
 |  111     assert ctm_dict['defaults'] == { | 
 |  112         'css': 'text/css', | 
 |  113         'html': 'text/html', | 
 |  114         'js': 'application/javascript', | 
 |  115         'json': 'application/json', | 
 |  116         'otf': 'application/x-font-otf', | 
 |  117         'png': 'image/png', | 
 |  118         'xml': 'text/xml' | 
 |  119     } | 
 |  120  | 
 |  121  | 
 |  122 def test_create_appx_manifest(metadata): | 
 |  123     files = packager.Files(set(), set()) | 
 |  124     for size in ['44', '50', '150']: | 
 |  125         files['Assets/logo_{}.png'.format(size)] = CHARS | 
 |  126     manifest = packagerEdge.create_appx_manifest({'metadata': metadata}, files) | 
 |  127     with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: | 
 |  128         manifest_expect = fp.read() | 
 |  129     assert manifest.strip() == manifest_expect.strip() | 
 |  130  | 
 |  131  | 
 |  132 def test_move_files_to_extension(): | 
 |  133     files = packager.Files(set(), set()) | 
 |  134     files['foo.xml'] = CHARS | 
 |  135     files['foo/bar.xml'] = CHARS | 
 |  136     files['Extension/foo.xml'] = CHARS | 
 |  137     packagerEdge.move_files_to_extension(files) | 
 |  138     assert set(files.keys()) == { | 
 |  139         'Extension/foo.xml', | 
 |  140         'Extension/foo/bar.xml', | 
 |  141         'Extension/Extension/foo.xml' | 
 |  142     } | 
 |  143  | 
 |  144  | 
 |  145 def test_create_build(tmpdir, srcdir): | 
 |  146     out_file = str(tmpdir.join('abp.appx')) | 
 |  147     packagerEdge.createBuild(str(srcdir), outFile=out_file) | 
 |  148     appx = zipfile.ZipFile(out_file) | 
 |  149  | 
 |  150     names = set(appx.namelist()) | 
 |  151     assert 'AppxManifest.xml' in names | 
 |  152     assert 'AppxBlockMap.xml' in names | 
 |  153     assert '[Content_Types].xml' in names | 
 |  154  | 
 |  155     assert appx.read('Assets/logo_44.png') == '44' | 
 |  156     assert appx.read('Extension/icons/abp-44.png') == '44' | 
| OLD | NEW |