OLD | NEW |
(Empty) | |
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2017-present 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 import glob |
| 17 import os |
| 18 import subprocess |
| 19 import sys |
| 20 |
| 21 import pytest |
| 22 |
| 23 CMSCMP = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'cms_cmp.py') |
| 24 |
| 25 |
| 26 def run_cms_cmp(*args, **kw): |
| 27 return subprocess.check_call([sys.executable, CMSCMP] + list(args), **kw) |
| 28 |
| 29 |
| 30 def hg(*args, **kw): |
| 31 cmd = ['hg'] |
| 32 if 'repo' in kw: |
| 33 cmd += ['-R', kw['repo']] |
| 34 del kw['repo'] |
| 35 # Disable default options from local user config. |
| 36 cmd += ['--config', 'defaults.{}='.format(args[0])] |
| 37 return subprocess.check_call(cmd + list(args), **kw) |
| 38 |
| 39 |
| 40 @pytest.fixture(scope='session') |
| 41 def website(tmpdir_factory): |
| 42 root = tmpdir_factory.mktemp('website') |
| 43 root.join('foo').write('foo') |
| 44 hg('init', str(root)) |
| 45 hg('commit', '-A', '-m', 'x', repo=str(root)) |
| 46 return root |
| 47 |
| 48 |
| 49 @pytest.fixture(scope='session') |
| 50 def cms(tmpdir_factory): |
| 51 root = tmpdir_factory.mktemp('cms') |
| 52 generate = root.mkdir('cms').mkdir('bin').join('generate_static_pages.py') |
| 53 generate.write('\n'.join([ |
| 54 'import sys, shutil', |
| 55 'shutil.copytree(sys.argv[1], sys.argv[2])' |
| 56 ])) |
| 57 hg('init', str(root)) |
| 58 hg('commit', '-A', '-m', 'x', repo=str(root)) |
| 59 root.join('foo').write('bar') |
| 60 hg('commit', '-A', '-m', 'y', repo=str(root)) |
| 61 generate.write( |
| 62 generate.read() + |
| 63 '\nshutil.copy(sys.argv[2] + "/foo", sys.argv[2] + "/bar")' |
| 64 ) |
| 65 hg('commit', '-m', 'z', repo=str(root)) |
| 66 hg('bookmark', '-r', '0', 'master', repo=str(root)) |
| 67 hg('bookmark', '-r', '1', 'yoda', repo=str(root)) |
| 68 hg('bookmark', '-r', '2', 'other', repo=str(root)) |
| 69 return root |
| 70 |
| 71 |
| 72 def test_same_revision(website, cms, tmpdir): |
| 73 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), |
| 74 '-b', 'master', '-t', 'yoda', |
| 75 str(website)) |
| 76 |
| 77 |
| 78 def test_different_revision(website, cms, tmpdir): |
| 79 with pytest.raises(subprocess.CalledProcessError): |
| 80 # Here the websites will be different, so the comparison will fail... |
| 81 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), |
| 82 '-b', 'master', '-t', 'other', |
| 83 str(website)) |
| 84 # ...but it will succeed if we ignore 'bar'. |
| 85 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), |
| 86 '-b', 'master', '-t', 'other', |
| 87 '-i', 'bar', |
| 88 str(website)) |
| 89 |
| 90 |
| 91 def test_bad_python(website, cms, tmpdir): |
| 92 with pytest.raises(subprocess.CalledProcessError): |
| 93 # The run should fail with a broken python... |
| 94 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), |
| 95 '-t', 'yoda', '-p', 'foobar', |
| 96 str(website)) |
| 97 # ...but succeed with a good one. |
| 98 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), |
| 99 '-t', 'yoda', '-p', sys.executable, |
| 100 str(website)) |
| 101 |
| 102 |
| 103 def test_remove_old(website, cms, tmpdir): |
| 104 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), '-t', 'yoda', |
| 105 str(website)) |
| 106 # Let's add a file to one of the output directories: |
| 107 d = glob.glob(os.path.join(str(tmpdir), 'website*'))[0] |
| 108 with open(os.path.join(d, 'baz'), 'w') as f: |
| 109 f.write('here') |
| 110 # Now the comparison will fail... |
| 111 with pytest.raises(subprocess.CalledProcessError): |
| 112 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), '-t', 'yoda', |
| 113 str(website)) |
| 114 # ...but it will succeed if we tell it to delete old outputs. |
| 115 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), '-t', 'yoda', '-r', |
| 116 str(website)) |
| 117 |
| 118 |
| 119 def test_working_copy(website, cms, tmpdir): |
| 120 with pytest.raises(subprocess.CalledProcessError): |
| 121 # This will fail because current version is 'other'. |
| 122 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), str(website)) |
| 123 generate = cms.join('cms').join('bin').join('generate_static_pages.py') |
| 124 # Remove last line that breaks stuff. |
| 125 generate.write('\n'.join(generate.read().splitlines()[:-1])) |
| 126 # Now it should be better. |
| 127 run_cms_cmp('-d', str(tmpdir), '-c', str(cms), str(website)) |
OLD | NEW |