Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_diff_script.py

Issue 29922555: Issue 7059 - Modify fldiff so it can handle multiple files (Closed) Base URL: https://hg.adblockplus.org/python-abp
Patch Set: Address comments on PS1 Created Oct. 26, 2018, 9:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« abp/filters/diff_script.py ('K') | « abp/filters/diff_script.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 _make_args(rootdir, diff_dir):
56 run_script(str(rootdir.join('base.txt')), 77 return [str(rootdir.join('latest.txt')), '-o ' + str(diff_dir)]
Vasily Kuznetsov 2018/10/29 16:31:48 '-o' and str(diff_dir) would be different argument
rhowell 2018/10/29 20:01:40 Ended up just removing this function, but I did sw
Vasily Kuznetsov 2018/10/29 23:47:33 Acknowledged.
57 str(rootdir.join('latest.txt')),
58 str(dstfile))
59 with io.open(str(dstfile), encoding='utf-8') as dst:
60 result = dst.read()
61 assert '+ &ad_channel=\xa3' in result
62 78
63 79
64 def test_no_outfile(rootdir): 80 def test_diff_with_dict(rootdir, archived_files, diff_dir):
65 _, _, out = run_script(str(rootdir.join('base.txt')), 81 arg = _make_args(rootdir, diff_dir)
Vasily Kuznetsov 2018/10/29 16:31:48 I wonder if _make_args is actually worth it. Here
rhowell 2018/10/29 20:01:40 Done.
66 str(rootdir.join('latest.txt'))) 82 arg.extend(archived_files)
67 assert '[Adblock Plus Diff]' in out 83 diff_script(arg)
84 for root, dirs, files in os.walk(str(diff_dir)):
85 for file in files:
86 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst:
87 result = dst.read()
88 assert '- &ad.vid=$~xmlhttprequest' in result
89 assert '+ &ad_channel=\xa3' in result
90 assert '! Version: 123' in result
91
92
93 def test_diff_with_outfile(rootdir, archived_files, diff_dir):
94 arg = _make_args(rootdir, diff_dir)
95 arg.extend(archived_files)
96 run_script(*arg)
Vasily Kuznetsov 2018/10/29 16:31:48 Following on the previous comment, here we'd get s
rhowell 2018/10/29 20:01:40 Done.
97 for root, dirs, files in os.walk(str(diff_dir)):
98 assert files
99 for file in files:
100 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst:
101 result = dst.read()
102 assert '- &ad.vid=$~xmlhttprequest' in result
103 assert '+ &ad_channel=\xa3' in result
104 assert '! Version: 123' in result
68 105
69 106
70 def test_no_base_file(rootdir): 107 def test_no_base_file(rootdir):
71 code, err, _ = run_script('wrong.txt', str(rootdir.join('latest.txt'))) 108 code, err, _ = run_script(str(rootdir.join('latest.txt')))
72 assert code == 1 109 assert code == 2
73 assert 'No such file or directory' in err 110 assert 'usage: fldiff' in err
74 111
75 112
76 def test_no_latest_file(rootdir): 113 def test_missing_file(rootdir):
77 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') 114 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt')
78 assert code == 1 115 assert code == 1
79 assert 'No such file or directory' in err 116 assert 'No such file or directory' in err
80 117
81 118
82 def test_diff_to_self(rootdir): 119 def test_diff_to_self(rootdir, diff_dir):
83 _, _, out = run_script(str(rootdir.join('latest.txt')), 120 arg = _make_args(rootdir, diff_dir)
84 str(rootdir.join('latest.txt'))) 121 arg.append(str(rootdir.join('latest.txt')))
85 assert out == '[Adblock Plus Diff]\n' 122 run_script(*arg)
123 for root, dirs, files in os.walk(str(diff_dir)):
124 assert files
125 for file in files:
126 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst:
127 result = dst.read()
128 assert result == '[Adblock Plus Diff]\n'
129
130
131 def test_no_version(rootdir, base_no_version):
132 arg = _make_args(rootdir, diff_dir)
133 arg.extend(base_no_version)
134 code, err, _ = run_script(*arg)
135 assert code == 1
136 assert err == 'Unable to find Version in [Adblock Plus 2.0]\n'
OLDNEW
« abp/filters/diff_script.py ('K') | « abp/filters/diff_script.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld