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