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

Delta Between Two Patch Sets: 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
Left Patch Set: Address comments on PS1 Created Oct. 26, 2018, 9:40 p.m.
Right Patch Set: Address comments on PS4 Created Oct. 31, 2018, 7:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « abp/filters/diff_script.py ('k') | tests/test_differ.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 23 import os
24 import re 24 import re
25 25
26 from test_differ import BASE, LATEST 26 from test_differ import BASE, LATEST
27 from abp.filters.diff_script import main as diff_script
28 27
29 28
30 @pytest.fixture 29 @pytest.fixture
31 def rootdir(tmpdir): 30 def rootdir(tmpdir):
32 """Directory with example filter lists.""" 31 """Root directory for test files."""
33 rootdir = tmpdir.join('root') 32 rootdir = tmpdir.join('root')
34 rootdir.mkdir() 33 rootdir.mkdir()
35 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') 34 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8')
36 return rootdir 35 return rootdir
37 36
38 37
39 @pytest.fixture 38 @pytest.fixture
40 def archive_dir(rootdir): 39 def archive_dir(rootdir):
41 return rootdir.mkdir('archive') 40 return rootdir.mkdir('archive')
42 41
(...skipping 23 matching lines...) Expand all
66 """Run diff rendering script with given arguments and return its output.""" 65 """Run diff rendering script with given arguments and return its output."""
67 cmd = ['fldiff'] + list(args) 66 cmd = ['fldiff'] + list(args)
68 67
69 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, 68 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE,
70 stdout=subprocess.PIPE, stdin=subprocess.PIPE, 69 stdout=subprocess.PIPE, stdin=subprocess.PIPE,
71 **kw) 70 **kw)
72 stdout, stderr = proc.communicate() 71 stdout, stderr = proc.communicate()
73 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') 72 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8')
74 73
75 74
76 def _make_args(rootdir, diff_dir): 75 def test_diff_with_outfile(rootdir, archived_files, diff_dir):
77 return [str(rootdir.join('latest.txt')), '-o ' + str(diff_dir)] 76 run_script(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.
77 *archived_files)
78 assert len(diff_dir.listdir()) == 2
79 for file in diff_dir.visit():
80 with io.open(str(file), encoding='utf-8') as dst:
81 result = dst.read()
82 assert '- &ad.vid=$~xmlhttprequest' in result
83 assert '+ &ad_channel=\xa3' in result
84 assert '! Version: 123' in result
78 85
79 86
80 def test_diff_with_dict(rootdir, archived_files, diff_dir): 87 def test_diff_no_outfile(rootdir, archived_files):
81 arg = _make_args(rootdir, diff_dir) 88 os.chdir(str(rootdir))
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.
82 arg.extend(archived_files) 89 run_script(str(rootdir.join('latest.txt')), *archived_files)
83 diff_script(arg) 90 for file in ['diff111.txt', 'diff112.txt']:
84 for root, dirs, files in os.walk(str(diff_dir)): 91 with io.open(file, encoding='utf-8') as dst:
85 for file in files: 92 result = dst.read()
86 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: 93 assert '- &ad.vid=$~xmlhttprequest' in result
87 result = dst.read() 94 assert '+ &ad_channel=\xa3' in result
88 assert '- &ad.vid=$~xmlhttprequest' in result 95 assert '! Version: 123' 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
105 96
106 97
107 def test_no_base_file(rootdir): 98 def test_no_base_file(rootdir):
108 code, err, _ = run_script(str(rootdir.join('latest.txt'))) 99 code, err, _ = run_script(str(rootdir.join('latest.txt')))
109 assert code == 2 100 assert code == 2
110 assert 'usage: fldiff' in err 101 assert 'usage: fldiff' in err
111 102
112 103
113 def test_missing_file(rootdir): 104 def test_wrong_file(rootdir):
114 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt') 105 code, err, _ = run_script(str(rootdir.join('base.txt')), 'wrong.txt')
115 assert code == 1 106 assert code == 1
116 assert 'No such file or directory' in err 107 assert 'No such file or directory' in err
117 108
118 109
119 def test_diff_to_self(rootdir, diff_dir): 110 def test_diff_to_self(rootdir, diff_dir):
120 arg = _make_args(rootdir, diff_dir) 111 run_script(str(rootdir.join('latest.txt')), '-o', str(diff_dir),
121 arg.append(str(rootdir.join('latest.txt'))) 112 str(rootdir.join('latest.txt')))
122 run_script(*arg) 113 assert len(diff_dir.listdir()) == 1
123 for root, dirs, files in os.walk(str(diff_dir)): 114 for file in diff_dir.visit():
124 assert files 115 with io.open(str(file), encoding='utf-8') as dst:
125 for file in files: 116 result = dst.read()
126 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst: 117 assert result == '[Adblock Plus Diff]\n'
127 result = dst.read()
128 assert result == '[Adblock Plus Diff]\n'
129 118
130 119
131 def test_no_version(rootdir, base_no_version): 120 def test_no_version(rootdir, base_no_version):
132 arg = _make_args(rootdir, diff_dir) 121 code, err, _ = run_script(str(rootdir.join('latest.txt')), '-o',
133 arg.extend(base_no_version) 122 str(diff_dir), *base_no_version)
134 code, err, _ = run_script(*arg)
135 assert code == 1 123 assert code == 1
136 assert err == 'Unable to find Version in [Adblock Plus 2.0]\n' 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
LEFTRIGHT

Powered by Google App Engine
This is Rietveld