OLD | NEW |
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present 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 """Functional tests for the diff script.""" | 16 """Functional tests for the diff script.""" |
17 | 17 |
18 from __future__ import unicode_literals | 18 from __future__ import unicode_literals |
19 | 19 |
20 import pytest | 20 import pytest |
21 import subprocess | 21 import subprocess |
22 import io | 22 import io |
| 23 import os |
| 24 import re |
23 | 25 |
24 from test_differ import BASE, LATEST | 26 from test_differ import BASE, LATEST |
25 | 27 |
26 | 28 |
27 @pytest.fixture | 29 @pytest.fixture |
28 def rootdir(tmpdir): | 30 def rootdir(tmpdir): |
29 """Directory with example filter lists.""" | 31 """Root directory for test files.""" |
30 rootdir = tmpdir.join('root') | 32 rootdir = tmpdir.join('root') |
31 rootdir.mkdir() | 33 rootdir.mkdir() |
32 rootdir.join('base.txt').write_text(BASE, encoding='utf8') | |
33 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') | 34 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') |
34 | |
35 return rootdir | 35 return rootdir |
36 | 36 |
37 | 37 |
38 @pytest.fixture | 38 @pytest.fixture |
39 def dstfile(tmpdir): | 39 def archive_dir(rootdir): |
40 """Destination file for saving the diff output.""" | 40 return rootdir.mkdir('archive') |
41 return tmpdir.join('dst') | 41 |
| 42 |
| 43 @pytest.fixture |
| 44 def diff_dir(rootdir): |
| 45 return rootdir.mkdir('diff') |
| 46 |
| 47 |
| 48 @pytest.fixture |
| 49 def archived_files(archive_dir): |
| 50 base2 = BASE + '&adnet=\n' |
| 51 base2 = re.sub(r'! Version: \d+', '! Version: 112', base2) |
| 52 archive_dir.join('list111.txt').write_text(BASE, encoding='utf8') |
| 53 archive_dir.join('list112.txt').write_text(base2, encoding='utf8') |
| 54 return [str(x) for x in archive_dir.listdir()] |
| 55 |
| 56 |
| 57 @pytest.fixture |
| 58 def base_no_version(archive_dir): |
| 59 base = re.sub(r'! Version: \d+', '! ', BASE) |
| 60 archive_dir.join('list113.txt').write_text(base, encoding='utf8') |
| 61 return [str(x) for x in archive_dir.listdir()] |
42 | 62 |
43 | 63 |
44 def run_script(*args, **kw): | 64 def run_script(*args, **kw): |
45 """Run diff rendering script with given arguments and return its output.""" | 65 """Run diff rendering script with given arguments and return its output.""" |
46 cmd = ['fldiff'] + list(args) | 66 cmd = ['fldiff'] + list(args) |
47 | 67 |
48 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, | 68 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
49 stdout=subprocess.PIPE, stdin=subprocess.PIPE, | 69 stdout=subprocess.PIPE, stdin=subprocess.PIPE, |
50 **kw) | 70 **kw) |
51 stdout, stderr = proc.communicate() | 71 stdout, stderr = proc.communicate() |
52 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') | 72 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') |
53 | 73 |
54 | 74 |
55 def test_diff_with_outfile(rootdir, dstfile): | 75 def test_diff_with_outfile(rootdir, archived_files, diff_dir): |
56 run_script(str(rootdir.join('base.txt')), | 76 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
57 str(rootdir.join('latest.txt')), | 77 *archived_files) |
58 str(dstfile)) | 78 assert len(diff_dir.listdir()) == 2 |
59 with io.open(str(dstfile), encoding='utf-8') as dst: | 79 for file in diff_dir.visit(): |
60 result = dst.read() | 80 with io.open(str(file), encoding='utf-8') as dst: |
61 assert '+ &ad_channel=\xa3' in result | 81 result = dst.read() |
| 82 assert '- &ad.vid=$~xmlhttprequest' in result |
| 83 assert '+ &ad_channel=\xa3' in result |
| 84 assert '! Version: 123' in result |
62 | 85 |
63 | 86 |
64 def test_no_outfile(rootdir): | 87 def test_diff_no_outfile(rootdir, archived_files): |
65 _, _, out = run_script(str(rootdir.join('base.txt')), | 88 os.chdir(str(rootdir)) |
66 str(rootdir.join('latest.txt'))) | 89 run_script(str(rootdir.join('latest.txt')), *archived_files) |
67 assert '[Adblock Plus Diff]' in out | 90 for file in ['diff111.txt', 'diff112.txt']: |
| 91 with io.open(file, encoding='utf-8') as dst: |
| 92 result = dst.read() |
| 93 assert '- &ad.vid=$~xmlhttprequest' in result |
| 94 assert '+ &ad_channel=\xa3' in result |
| 95 assert '! Version: 123' in result |
68 | 96 |
69 | 97 |
70 def test_no_base_file(rootdir): | 98 def test_no_base_file(rootdir): |
71 code, err, _ = run_script('wrong.txt', str(rootdir.join('latest.txt'))) | 99 code, err, _ = run_script(str(rootdir.join('latest.txt'))) |
72 assert code == 1 | 100 assert code == 2 |
73 assert 'No such file or directory' in err | 101 assert 'usage: fldiff' in err |
74 | 102 |
75 | 103 |
76 def test_no_latest_file(rootdir): | 104 def test_wrong_file(rootdir): |
77 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') | 105 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') |
78 assert code == 1 | 106 assert code == 1 |
79 assert 'No such file or directory' in err | 107 assert 'No such file or directory' in err |
80 | 108 |
81 | 109 |
82 def test_diff_to_self(rootdir): | 110 def test_diff_to_self(rootdir, diff_dir): |
83 _, _, out = run_script(str(rootdir.join('latest.txt')), | 111 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
84 str(rootdir.join('latest.txt'))) | 112 str(rootdir.join('latest.txt'))) |
85 assert out == '[Adblock Plus Diff]\n' | 113 assert len(diff_dir.listdir()) == 1 |
| 114 for file in diff_dir.visit(): |
| 115 with io.open(str(file), encoding='utf-8') as dst: |
| 116 result = dst.read() |
| 117 assert result == '[Adblock Plus Diff]\n' |
| 118 |
| 119 |
| 120 def test_no_version(rootdir, base_no_version): |
| 121 code, err, _ = run_script(str(rootdir.join('latest.txt')), '-o', |
| 122 str(diff_dir), *base_no_version) |
| 123 assert code == 1 |
| 124 assert 'Unable to find Version in ' in err |
| 125 |
| 126 |
| 127 def test_write_and_overwrite(rootdir, archived_files, diff_dir): |
| 128 test_diff_with_outfile(rootdir, archived_files, diff_dir) |
| 129 latest = re.sub(r'&act=ads_', '! ', BASE) |
| 130 latest = latest + '&adurl=\n' |
| 131 rootdir.join('latest.txt').write_text(latest, encoding='utf8') |
| 132 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
| 133 *archived_files) |
| 134 assert len(diff_dir.listdir()) == 2 |
| 135 for file in diff_dir.visit(): |
| 136 with io.open(str(file), encoding='utf-8') as dst: |
| 137 result = dst.read() |
| 138 assert '- &act=ads_' in result |
| 139 assert '+ &adurl=' in result |
| 140 assert '- &ad.vid=$~xmlhttprequest' not in result |
OLD | NEW |