| Left: | ||
| Right: |
| 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 | |
| 22 import pytest | |
| 23 import py | |
| 24 | |
| 25 | |
| 26 @pytest.fixture() | |
| 27 def tests_dir(): | |
| 28 """Directory that contains this tests and the data files it uses.""" | |
| 29 return py.path.local(__file__).dirpath() | |
| 30 | |
| 31 | |
| 32 @pytest.fixture() | |
| 33 def oracle(tests_dir): | |
| 34 """Function that returns expected content of generated files.""" | |
| 35 def expected_value_of(what): | |
| 36 return tests_dir.join('oracle').join(what).read() | |
| 37 return expected_value_of | |
| 38 | |
| 39 | |
| 40 @pytest.fixture() | |
| 41 def cache_dir(tests_dir): | |
| 42 cache_dir = tests_dir.join('.test-cache') | |
| 43 cache_dir.ensure_dir() | |
| 44 return cache_dir | |
| 45 | |
| 46 | |
| 47 @pytest.fixture() | |
| 48 def root_dir(tmpdir): | |
|
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') | |
| 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' | |
| 57 tests_dir.join(key_filename).copy(keys_dir.join(key_filename)) | |
| 58 return keys_dir | |
| 59 | |
| 60 | |
| 61 def call_hg(cwd, *params): | |
| 62 return subprocess.check_call(['hg'] + list(params), cwd=str(cwd)) | |
| 63 | |
| 64 | |
| 65 @pytest.fixture() | |
| 66 def downloads_repo(tests_dir, hg_dir): | |
| 67 """Mock of downloads repo with empty files.""" | |
| 68 downloads_list = tests_dir.join('downloads.list').read().splitlines() | |
| 69 downloads_dir = hg_dir.mkdir('downloads') | |
| 70 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: | |
| 72 downloads_dir.join(item).write('') | |
| 73 call_hg(downloads_dir, 'add', *downloads_list) | |
| 74 call_hg(downloads_dir, 'commit', '-m', 'ok') | |
| 75 | |
| 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 | |
| 95 | |
| 96 | |
| 97 @pytest.fixture() | |
| 98 def config_ini(tests_dir, root_dir, hg_dir, keys_dir, downloads_repo): | |
| 99 """Sitescripts configuration.""" | |
| 100 template = tests_dir.join('sitescripts.ini.template').read() | |
| 101 conf = template.format(hg_dir=hg_dir, out_dir=root_dir, keys_dir=keys_dir) | |
| 102 config_ini = root_dir.join('sitescripts.ini') | |
| 103 config_ini.write(conf) | |
| 104 return config_ini | |
| 105 | |
| 106 | |
| 107 def test_update_manifests(config_ini, hg_dir, root_dir, oracle): | |
| 108 env = dict(os.environ) | |
| 109 env['SITESCRIPTS_CONFIG'] = str(config_ini) | |
| 110 cmd = ['python', '-m', | |
| 111 'sitescripts.extensions.bin.updateUpdateManifests'] | |
| 112 subprocess.check_call(cmd, env=env) | |
| 113 for filename in ['androidupdates.json', 'androidupdates.xml', | |
| 114 'ieupdate.json', 'update.rdf', 'updates.plist']: | |
| 115 got = root_dir.join(filename).read() | |
| 116 expect = oracle(filename) | |
| 117 if filename.endswith('.json'): | |
| 118 got = json.loads(got) | |
| 119 expect = json.loads(expect) | |
| 120 assert got == expect | |
| OLD | NEW |