OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-2016 Eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 """Tests for update manifest generation script.""" |
| 17 |
| 18 import json |
| 19 import os |
| 20 import subprocess |
| 21 import xml.etree.ElementTree as ET |
| 22 |
| 23 import pytest |
| 24 import py |
| 25 |
| 26 |
| 27 @pytest.fixture() |
| 28 def tests_dir(): |
| 29 """Directory that contains this tests and the data files it uses.""" |
| 30 return py.path.local(__file__).dirpath() |
| 31 |
| 32 |
| 33 @pytest.fixture() |
| 34 def oracle(tests_dir): |
| 35 """Function that returns expected content of generated files.""" |
| 36 def expected_value_of(what): |
| 37 return tests_dir.join('oracle').join(what).read().strip() |
| 38 return expected_value_of |
| 39 |
| 40 |
| 41 @pytest.fixture() |
| 42 def data_dir(tests_dir): |
| 43 return tests_dir.join('data') |
| 44 |
| 45 |
| 46 @pytest.fixture() |
| 47 def keys_dir(tmpdir, tests_dir): |
| 48 keys_dir = tmpdir.mkdir('keys') |
| 49 key_filename = 'adblockplussafari.pem' |
| 50 tests_dir.join(key_filename).copy(keys_dir.join(key_filename)) |
| 51 return keys_dir |
| 52 |
| 53 |
| 54 def call_hg(cwd, *params): |
| 55 return subprocess.check_call(['hg'] + list(params), cwd=str(cwd)) |
| 56 |
| 57 |
| 58 REPOS = { |
| 59 'adblockplus': ('metadata.gecko', '2.7.3'), |
| 60 'adblockplusie': ('README.txt', '1.33.7'), |
| 61 'adblockpluschrome': ('metadata.safari', '1.12.3'), |
| 62 'adblockplusandroid': ('AndroidManifest.xml', '1.3') |
| 63 } |
| 64 |
| 65 |
| 66 @pytest.fixture() |
| 67 def hg_dir(tmpdir, data_dir): |
| 68 """Directory that contains the repository mocks.""" |
| 69 hg_dir = tmpdir.mkdir('hg') |
| 70 |
| 71 # Mock plugin repositories. |
| 72 for repo, config in REPOS.items(): |
| 73 filename, tag = config |
| 74 repo_dir = hg_dir.mkdir(repo) |
| 75 call_hg(repo_dir, 'init') |
| 76 data_dir.join(filename).copy(repo_dir.join(filename)) |
| 77 call_hg(repo_dir, 'add', filename) |
| 78 call_hg(repo_dir, 'commit', '-m', '1') |
| 79 call_hg(repo_dir, 'tag', tag) |
| 80 |
| 81 # Mock the downloads repository. |
| 82 downloads_list = data_dir.join('downloads.list').read().splitlines() |
| 83 downloads_dir = hg_dir.mkdir('downloads') |
| 84 call_hg(downloads_dir, 'init') |
| 85 for item in downloads_list: |
| 86 downloads_dir.join(item).write('') |
| 87 call_hg(downloads_dir, 'add', *downloads_list) |
| 88 call_hg(downloads_dir, 'commit', '-m', 'ok') |
| 89 |
| 90 return hg_dir |
| 91 |
| 92 |
| 93 @pytest.fixture() |
| 94 def config_ini(tests_dir, tmpdir, hg_dir, keys_dir): |
| 95 """Sitescripts configuration.""" |
| 96 template = tests_dir.join('sitescripts.ini.template').read() |
| 97 conf = template.format(hg_dir=hg_dir, out_dir=tmpdir, keys_dir=keys_dir) |
| 98 config_ini = tmpdir.join('sitescripts.ini') |
| 99 config_ini.write(conf) |
| 100 return config_ini |
| 101 |
| 102 |
| 103 def rdf2data(rdf): |
| 104 """Convert RDF to a more comparable data strcuture.""" |
| 105 # We need this to address the RDF item ordering discrepancies. |
| 106 def et2data(node): |
| 107 return { |
| 108 'tag': node.tag, |
| 109 'text': node.text, |
| 110 'attrib': node.attrib, |
| 111 'subs': sorted(et2data(sub) for sub in node) |
| 112 } |
| 113 return et2data(ET.fromstring(rdf)) |
| 114 |
| 115 |
| 116 def test_update_manifests(config_ini, hg_dir, tmpdir, oracle): |
| 117 env = dict(os.environ) |
| 118 env['SITESCRIPTS_CONFIG'] = str(config_ini) |
| 119 cmd = ['python', '-m', 'sitescripts.extensions.bin.updateUpdateManifests'] |
| 120 subprocess.check_call(cmd, env=env) |
| 121 for filename in ['androidupdates.json', 'androidupdates.xml', |
| 122 'ieupdate.json', 'update.rdf', 'updates.plist']: |
| 123 got = tmpdir.join(filename).read().strip() |
| 124 expect = oracle(filename) |
| 125 if filename.endswith('.json'): |
| 126 got = json.loads(got) |
| 127 expect = json.loads(expect) |
| 128 elif filename.endswith('.rdf'): |
| 129 got = rdf2data(got) |
| 130 expect = rdf2data(expect) |
| 131 assert got == expect |
OLD | NEW |