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

Side by Side Diff: tests/test_render_script.py

Issue 29824555: Issue #4116: Make infile and outfile parameters of flrender script from python-abp optional (Closed)
Patch Set: Created July 6, 2018, 11:39 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
« README.md ('K') | « abp/filters/sources.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
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 @pytest.fixture 87 @pytest.fixture
88 def dstfile(tmpdir): 88 def dstfile(tmpdir):
89 """Destination file for saving rendered list.""" 89 """Destination file for saving rendered list."""
90 return tmpdir.join('dst') 90 return tmpdir.join('dst')
91 91
92 92
93 def run_script(*args, **kw): 93 def run_script(*args, **kw):
94 """Run rendering script with given arguments and return its output.""" 94 """Run rendering script with given arguments and return its output."""
95 cmd = ['flrender'] + list(args) 95 cmd = ['flrender'] + list(args)
96 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, **kw) 96 test_in = None
97 stdout, stderr = proc.communicate() 97 if 'test_in'in kw.keys():
Vasily Kuznetsov 2018/07/09 17:00:57 Space missing here (weird that flake8 is ok...)
Tudor Avram 2018/07/10 16:09:47 Done.
98 return proc.returncode, stderr.decode('utf-8') 98 test_in = kw.pop('test_in').encode('utf-8')
99 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE,
100 stdout=subprocess.PIPE, stdin=subprocess.PIPE,
101 **kw)
102 stdout, stderr = proc.communicate(input=test_in)
103 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8')
99 104
100 105
101 def test_render_no_includes(rootdir, dstfile): 106 def test_render_no_includes(rootdir, dstfile):
102 run_script(str(rootdir.join('simple.txt')), str(dstfile)) 107 run_script(str(rootdir.join('simple.txt')), str(dstfile))
103 result = dstfile.read() 108 result = dstfile.read()
104 assert 'Ok' in result 109 assert 'Ok' in result
105 assert '! Checksum:' in result 110 assert '! Checksum:' in result
106 111
107 112
108 def test_render_unicode(rootdir, dstfile): 113 def test_render_unicode(rootdir, dstfile):
109 code, err = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) 114 code, err, _ = run_script(str(rootdir.join('unicode.txt')), str(dstfile))
110 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') 115 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8')
111 116
112 117
113 def test_render_with_includes(rootdir, dstfile): 118 def test_render_with_includes(rootdir, dstfile):
114 run_script(str(rootdir.join('includer.txt')), str(dstfile), 119 run_script(str(rootdir.join('includer.txt')), str(dstfile),
115 '-i', 'inc=' + str(rootdir.join('inc'))) 120 '-i', 'inc=' + str(rootdir.join('inc')))
116 assert 'I am included!' in dstfile.read() 121 assert 'I am included!' in dstfile.read()
117 122
118 123
119 def test_render_with_includes_relative(rootdir, dstfile): 124 def test_render_with_includes_relative(rootdir, dstfile):
120 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir)) 125 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir))
121 assert 'I am included!' in dstfile.read() 126 assert 'I am included!' in dstfile.read()
122 127
123 128
124 def test_render_verbose(rootdir, dstfile): 129 def test_render_verbose(rootdir, dstfile):
125 code, err = run_script('includer.txt', str(dstfile), 130 code, err, _ = run_script('includer.txt', str(dstfile),
126 '-i', 'inc=inc', '-v', cwd=str(rootdir)) 131 '-i', 'inc=inc', '-v', cwd=str(rootdir))
127 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' 132 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n'
128 133
129 134
135 def test_stdin_stdout(rootdir):
Vasily Kuznetsov 2018/07/09 17:00:57 Wouldn't it be simpler to model this and the follo
Tudor Avram 2018/07/10 08:06:20 Ok, will do that. From what I see, the case for in
136 test_input = '[Adblock]\ntest\nwww.test.test\ntest.test.test\n'
137 expected_body = 'test\nwww.test.test\ntest.test.test\n'
138 expected_header = '[Adblock]'
139
140 _, _, stdout = run_script('-i', 'inc=inc', '-v',
141 cwd=str(rootdir), test_in=test_input)
142 lines = stdout.split('\n')
143 assert expected_header == lines[0]
144 assert expected_body in stdout
145
146
147 def test_filein_stdout(rootdir):
148 expected = 'I am included!'
149 _, _, stdout = run_script('includer.txt', '-i', 'inc=inc',
150 cwd=str(rootdir))
151
152 assert expected in stdout
153
154
130 def test_no_header(rootdir, dstfile): 155 def test_no_header(rootdir, dstfile):
131 code, err = run_script('inc/includee.txt', str(dstfile), cwd=str(rootdir)) 156 code, err, _ = run_script('inc/includee.txt', str(dstfile),
157 cwd=str(rootdir))
132 assert code == 1 158 assert code == 1
133 assert err == 'No header found at the beginning of the input.\n' 159 assert err == 'No header found at the beginning of the input.\n'
134 160
135 161
136 def test_wrong_file(dstfile): 162 def test_wrong_file(dstfile):
137 code, err = run_script('wrong.txt', str(dstfile)) 163 code, err, _ = run_script('wrong.txt', str(dstfile))
138 assert code == 1 164 assert code == 1
139 assert err == "File not found: 'wrong.txt'\n" 165 assert err == "File not found: 'wrong.txt'\n"
140 166
141 167
142 def test_wrong_include_source(rootdir, dstfile): 168 def test_wrong_include_source(rootdir, dstfile):
143 code, err = run_script('brk.txt', str(dstfile), cwd=str(rootdir)) 169 code, err, _ = run_script('brk.txt', str(dstfile), cwd=str(rootdir))
144 assert code == 1 170 assert code == 1
145 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' " 171 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' "
146 "from 'brk.txt'\n") 172 "from 'brk.txt'\n")
147 173
148 174
149 def test_wrong_include(rootdir, dstfile): 175 def test_wrong_include(rootdir, dstfile):
150 code, err = run_script('brk.txt', str(dstfile), 176 code, err, _ = run_script('brk.txt', str(dstfile),
151 '-i', 'inc=inc', cwd=str(rootdir)) 177 '-i', 'inc=inc', cwd=str(rootdir))
152 missing_path = str(rootdir.join('inc', 'missing.txt')) 178 missing_path = str(rootdir.join('inc', 'missing.txt'))
153 expect = ("File not found: '{}' when including 'missing.txt' " 179 expect = ("File not found: '{}' when including 'missing.txt' "
154 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path) 180 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path)
155 assert code == 1 181 assert code == 1
156 assert err == expect 182 assert err == expect
157 183
158 184
159 def test_circular_includes(rootdir, dstfile): 185 def test_circular_includes(rootdir, dstfile):
160 code, err = run_script('circ.txt', str(dstfile), 186 code, err, _ = run_script('circ.txt', str(dstfile),
161 '-i', 'inc=inc', cwd=str(rootdir)) 187 '-i', 'inc=inc', cwd=str(rootdir))
162 expect = ("Include loop encountered when including 'circular.txt' " 188 expect = ("Include loop encountered when including 'circular.txt' "
163 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n") 189 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n")
164 assert code == 1 190 assert code == 1
165 assert err == expect 191 assert err == expect
166 192
167 193
168 def test_wrong_source(rootdir, dstfile): 194 def test_wrong_source(rootdir, dstfile):
169 code, err = run_script('foo:bar.txt', str(dstfile)) 195 code, err, _ = run_script('foo:bar.txt', str(dstfile))
170 assert code == 1 196 assert code == 1
171 assert err == "Unknown source: 'foo'\n" 197 assert err == "Unknown source: 'foo'\n"
172 198
173 199
174 @pytest.mark.tryfirst 200 @pytest.mark.tryfirst
175 @pytest.mark.slowtest 201 @pytest.mark.slowtest
176 def test_web_include(rootdir, dstfile, webserver_port): 202 def test_web_include(rootdir, dstfile, webserver_port):
177 url = 'http://localhost:{}/metainc.txt'.format(webserver_port) 203 url = 'http://localhost:{}/metainc.txt'.format(webserver_port)
178 webinc = rootdir.join('webinc.txt') 204 webinc = rootdir.join('webinc.txt')
179 webinc.write('[Adblock]\n%include {}%'.format(url)) 205 webinc.write('[Adblock]\n%include {}%'.format(url))
180 code, err = run_script(str(webinc), str(dstfile)) 206 code, err, _ = run_script(str(webinc), str(dstfile))
181 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8') 207 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8')
182 208
183 209
184 @pytest.mark.slowtest 210 @pytest.mark.slowtest
185 def test_failed_web_include(rootdir, dstfile, webserver_port): 211 def test_failed_web_include(rootdir, dstfile, webserver_port):
186 url = 'http://localhost:{}/missing.txt'.format(webserver_port) 212 url = 'http://localhost:{}/missing.txt'.format(webserver_port)
187 webinc = rootdir.join('webinc.txt') 213 webinc = rootdir.join('webinc.txt')
188 webinc.write('[Adblock]\n%include {}%'.format(url)) 214 webinc.write('[Adblock]\n%include {}%'.format(url))
189 code, err = run_script(str(webinc), str(dstfile)) 215 code, err, _ = run_script(str(webinc), str(dstfile))
190 assert code == 1 216 assert code == 1
191 assert err.startswith( 217 assert err.startswith(
192 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) 218 "HTTP 404 Not found: '{0}' when including '{0}'".format(url))
OLDNEW
« README.md ('K') | « abp/filters/sources.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld