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: Created Oct. 24, 2018, 2:27 a.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
23 24
24 from test_differ import BASE, LATEST 25 from test_differ import BASE, LATEST
26 from abp.filters.diff_script import main as diff_script
27
28 BASE2 = BASE + '&adnet=\n'
25 29
26 30
27 @pytest.fixture 31 @pytest.fixture
28 def rootdir(tmpdir): 32 def rootdir(tmpdir):
29 """Directory with example filter lists.""" 33 """Directory with example filter lists."""
30 rootdir = tmpdir.join('root') 34 rootdir = tmpdir.join('root')
31 rootdir.mkdir() 35 rootdir.mkdir()
32 rootdir.join('base.txt').write_text(BASE, encoding='utf8')
33 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8') 36 rootdir.join('latest.txt').write_text(LATEST, encoding='utf8')
34
35 return rootdir 37 return rootdir
36 38
37 39
38 @pytest.fixture 40 @pytest.fixture
41 def archive_dir(tmpdir, rootdir):
Vasily Kuznetsov 2018/10/24 11:30:10 You don't seem to be using tmpdir in this function
rhowell 2018/10/26 21:42:21 Done.
42 archive_dir = rootdir.join('archive')
43 archive_dir.mkdir()
44 archive_dir.join('list1.txt').write_text(BASE, encoding='utf8')
45 archive_dir.join('list2.txt').write_text(BASE2, encoding='utf8')
46 return archive_dir
47
48
49 @pytest.fixture
50 def diff_dir(tmpdir, rootdir):
Vasily Kuznetsov 2018/10/24 11:30:09 tmpdir also unused here, and the function could be
rhowell 2018/10/26 21:42:21 Done.
51 diff_dir = rootdir.join('diff')
52 diff_dir.mkdir()
53 return diff_dir
54
55
56 @pytest.fixture
39 def dstfile(tmpdir): 57 def dstfile(tmpdir):
40 """Destination file for saving the diff output.""" 58 """Destination file for saving the diff output."""
41 return tmpdir.join('dst') 59 return tmpdir.join('dst')
42 60
43 61
44 def run_script(*args, **kw): 62 def run_script(*args, **kw):
45 """Run diff rendering script with given arguments and return its output.""" 63 """Run diff rendering script with given arguments and return its output."""
46 cmd = ['fldiff'] + list(args) 64 cmd = ['fldiff'] + list(args)
47 65
48 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, 66 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE,
49 stdout=subprocess.PIPE, stdin=subprocess.PIPE, 67 stdout=subprocess.PIPE, stdin=subprocess.PIPE,
50 **kw) 68 **kw)
51 stdout, stderr = proc.communicate() 69 stdout, stderr = proc.communicate()
Vasily Kuznetsov 2018/10/24 11:30:09 Since we can't really output to stdout anymore (be
rhowell 2018/10/26 21:42:22 Looks like subprocess.run() was added in Python 3.
Vasily Kuznetsov 2018/10/29 16:31:48 Indeed, sorry, I forgot that we're also supporting
52 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') 70 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8')
53 71
54 72
55 def test_diff_with_outfile(rootdir, dstfile): 73 def test_diff_with_dict(rootdir, archive_dir, diff_dir):
56 run_script(str(rootdir.join('base.txt')), 74 args = []
57 str(rootdir.join('latest.txt')), 75 args.append('-l ' + str(rootdir.join('latest.txt')))
58 str(dstfile)) 76 args.append('-o ' + str(diff_dir))
59 with io.open(str(dstfile), encoding='utf-8') as dst: 77 args.append(str(archive_dir.join('list1.txt')))
Vasily Kuznetsov 2018/10/24 11:30:09 With the way we use `archive_dir` fixture, I'm thi
rhowell 2018/10/26 21:42:22 Yeah, this way seems much cleaner.
60 result = dst.read() 78 args.append(str(archive_dir.join('list2.txt')))
61 assert '+ &ad_channel=\xa3' in result 79 diff_script(args)
80 for root, dirs, files in os.walk(str(diff_dir)):
81 for file in files:
82 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst:
83 result = dst.read()
84 assert '- &ad.vid=$~xmlhttprequest' in result
85 assert '+ &ad_channel=\xa3' in result
86 assert '! Version: 123' in result
62 87
63 88
64 def test_no_outfile(rootdir): 89 def test_diff_with_outfile(rootdir, archive_dir, diff_dir):
65 _, _, out = run_script(str(rootdir.join('base.txt')), 90 run_script('-o ' + str(diff_dir),
66 str(rootdir.join('latest.txt'))) 91 '-l ' + str(rootdir.join('latest.txt')),
92 str(archive_dir.join('list1.txt')),
93 str(archive_dir.join('list2.txt')),
94 )
95 for root, dirs, files in os.walk(str(diff_dir)):
96 for file in files:
97 with io.open(str(diff_dir.join(file)), encoding='utf-8') as dst:
98 result = dst.read()
99 assert '- &ad.vid=$~xmlhttprequest' in result
100 assert '+ &ad_channel=\xa3' in result
101 assert '! Version: 123' in result
102
103
104 def test_no_outfile(rootdir, archive_dir, diff_dir):
Vasily Kuznetsov 2018/10/24 11:30:09 Since this mode of running doesn't really work wit
rhowell 2018/10/26 21:42:21 Done.
105 _, _, out = run_script('-l ' + str(rootdir.join('latest.txt')),
106 str(archive_dir.join('list1.txt')),
107 str(archive_dir.join('list2.txt')),
108 )
67 assert '[Adblock Plus Diff]' in out 109 assert '[Adblock Plus Diff]' in out
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('-l ' + str(rootdir.join('latest.txt')))
72 assert code == 1 114 assert code == 1
73 assert 'No such file or directory' in err 115 assert err == 'Archived lists are required\n'
74 116
75 117
76 def test_no_latest_file(rootdir): 118 def test_no_latest_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 err == 'The --latest list is required\n'
80 122
81 123
82 def test_diff_to_self(rootdir): 124 def test_diff_to_self(rootdir):
83 _, _, out = run_script(str(rootdir.join('latest.txt')), 125 _, _, out = run_script('-l ' + str(rootdir.join('latest.txt')),
84 str(rootdir.join('latest.txt'))) 126 str(rootdir.join('latest.txt')))
85 assert out == '[Adblock Plus Diff]\n' 127 assert out == '[Adblock Plus Diff]\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