| Left: | ||
| Right: |
| 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 |
| 27 from abp.filters.diff_script import main as diff_script | |
| 25 | 28 |
| 26 | 29 |
| 27 @pytest.fixture | 30 @pytest.fixture |
| 28 def rootdir(tmpdir): | 31 def rootdir(tmpdir): |
| 29 """Directory with example filter lists.""" | 32 """Directory with example filter lists.""" |
| 30 rootdir = tmpdir.join('root') | 33 rootdir = tmpdir.join('root') |
| 31 rootdir.mkdir() | 34 rootdir.mkdir() |
| 32 rootdir.join('base.txt').write_text(BASE, encoding='utf8') | |
| 33 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') | 35 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') |
| 34 | |
| 35 return rootdir | 36 return rootdir |
| 36 | 37 |
| 37 | 38 |
| 38 @pytest.fixture | 39 @pytest.fixture |
| 39 def dstfile(tmpdir): | 40 def archive_dir(rootdir): |
| 40 """Destination file for saving the diff output.""" | 41 return rootdir.mkdir('archive') |
| 41 return tmpdir.join('dst') | 42 |
| 43 | |
| 44 @pytest.fixture | |
| 45 def diff_dir(rootdir): | |
| 46 return rootdir.mkdir('diff') | |
| 47 | |
| 48 | |
| 49 @pytest.fixture | |
| 50 def archived_files(archive_dir): | |
| 51 base2 = BASE + '&adnet=\n' | |
| 52 base2 = re.sub(r'! Version: \d+', '! Version: 112', base2) | |
| 53 archive_dir.join('list111.txt').write_text(BASE, encoding='utf8') | |
| 54 archive_dir.join('list112.txt').write_text(base2, encoding='utf8') | |
| 55 return [str(x) for x in archive_dir.listdir()] | |
| 56 | |
| 57 | |
| 58 @pytest.fixture | |
| 59 def base_no_version(archive_dir): | |
| 60 base = re.sub(r'! Version: \d+', '! ', BASE) | |
| 61 archive_dir.join('list113.txt').write_text(base, encoding='utf8') | |
| 62 return [str(x) for x in archive_dir.listdir()] | |
| 42 | 63 |
| 43 | 64 |
| 44 def run_script(*args, **kw): | 65 def run_script(*args, **kw): |
| 45 """Run diff rendering script with given arguments and return its output.""" | 66 """Run diff rendering script with given arguments and return its output.""" |
| 46 cmd = ['fldiff'] + list(args) | 67 cmd = ['fldiff'] + list(args) |
| 47 | 68 |
| 48 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, | 69 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 49 stdout=subprocess.PIPE, stdin=subprocess.PIPE, | 70 stdout=subprocess.PIPE, stdin=subprocess.PIPE, |
| 50 **kw) | 71 **kw) |
| 51 stdout, stderr = proc.communicate() | 72 stdout, stderr = proc.communicate() |
| 52 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') | 73 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') |
| 53 | 74 |
| 54 | 75 |
| 55 def test_diff_with_outfile(rootdir, dstfile): | 76 def test_diff_with_dict(rootdir, archived_files, diff_dir): |
| 56 run_script(str(rootdir.join('base.txt')), | 77 diff_script([str(rootdir.join('latest.txt')), '-o', str(diff_dir)] + |
| 57 str(rootdir.join('latest.txt')), | 78 archived_files) |
| 58 str(dstfile)) | 79 for _, _, files in os.walk(str(diff_dir)): |
|
Vasily Kuznetsov
2018/10/29 23:47:34
I just realized that we don't really need `os.walk
rhowell
2018/10/31 00:20:58
Yeah, this seems to be less code and more efficien
| |
| 59 with io.open(str(dstfile), encoding='utf-8') as dst: | 80 for file in files: |
| 60 result = dst.read() | 81 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: |
| 61 assert '+ &ad_channel=\xa3' in result | 82 result = dst.read() |
| 83 assert '- &ad.vid=$~xmlhttprequest' in result | |
| 84 assert '+ &ad_channel=\xa3' in result | |
| 85 assert '! Version: 123' in result | |
| 62 | 86 |
| 63 | 87 |
| 64 def test_no_outfile(rootdir): | 88 def test_diff_with_outfile(rootdir, archived_files, diff_dir): |
| 65 _, _, out = run_script(str(rootdir.join('base.txt')), | 89 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
| 66 str(rootdir.join('latest.txt'))) | 90 *archived_files) |
| 67 assert '[Adblock Plus Diff]' in out | 91 for _, _, files in os.walk(str(diff_dir)): |
| 92 assert files | |
| 93 for file in files: | |
| 94 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: | |
| 95 result = dst.read() | |
| 96 assert '- &ad.vid=$~xmlhttprequest' in result | |
| 97 assert '+ &ad_channel=\xa3' in result | |
| 98 assert '! Version: 123' in result | |
| 99 | |
| 100 | |
| 101 def test_diff_no_outfile(rootdir, archived_files, diff_dir): | |
| 102 run_script(str(rootdir.join('latest.txt')), *archived_files) | |
|
Vasily Kuznetsov
2018/10/29 23:47:34
We need to change to a temporary directory before
rhowell
2018/10/31 00:20:58
Done.
| |
| 103 for file in ['diff111.txt', 'diff112.txt']: | |
| 104 with io.open(file, encoding='utf-8') as dst: | |
| 105 result = dst.read() | |
| 106 assert '- &ad.vid=$~xmlhttprequest' in result | |
| 107 assert '+ &ad_channel=\xa3' in result | |
| 108 assert '! Version: 123' in result | |
| 109 os.remove(file) | |
| 68 | 110 |
| 69 | 111 |
| 70 def test_no_base_file(rootdir): | 112 def test_no_base_file(rootdir): |
| 71 code, err, _ = run_script('wrong.txt', str(rootdir.join('latest.txt'))) | 113 code, err, _ = run_script(str(rootdir.join('latest.txt'))) |
| 72 assert code == 1 | 114 assert code == 2 |
| 73 assert 'No such file or directory' in err | 115 assert 'usage: fldiff' in err |
| 74 | 116 |
| 75 | 117 |
| 76 def test_no_latest_file(rootdir): | 118 def test_wrong_file(rootdir): |
| 77 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') | 119 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') |
| 78 assert code == 1 | 120 assert code == 1 |
| 79 assert 'No such file or directory' in err | 121 assert 'No such file or directory' in err |
| 80 | 122 |
| 81 | 123 |
| 82 def test_diff_to_self(rootdir): | 124 def test_diff_to_self(rootdir, diff_dir): |
| 83 _, _, out = run_script(str(rootdir.join('latest.txt')), | 125 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir), |
| 84 str(rootdir.join('latest.txt'))) | 126 str(rootdir.join('latest.txt'))) |
| 85 assert out == '[Adblock Plus Diff]\n' | 127 for _, _, files in os.walk(str(diff_dir)): |
| 128 assert files | |
| 129 for file in files: | |
| 130 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: | |
| 131 result = dst.read() | |
| 132 assert result == '[Adblock Plus Diff]\n' | |
| 133 | |
| 134 | |
| 135 def test_no_version(rootdir, base_no_version): | |
| 136 code, err, _ = run_script(str(rootdir.join('latest.txt')), '-o', | |
| 137 str(diff_dir), *base_no_version) | |
| 138 assert code == 1 | |
| 139 assert 'Unable to find Version in /tmp/' in err | |
| OLD | NEW |