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 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
97 stdout, stderr = proc.communicate() | 97 test_in = None |
98 return proc.returncode, stderr.decode('utf-8') | 98 if 'test_in' in kw.keys(): |
99 test_in = kw.pop('test_in') | |
Vasily Kuznetsov
2018/07/13 17:41:04
I just found that you can replace this and the two
Tudor Avram
2018/07/16 09:17:11
Done.
| |
100 if test_in is not None: | |
101 test_in = test_in.encode('utf-8') | |
102 | |
103 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, | |
104 stdout=subprocess.PIPE, stdin=subprocess.PIPE, | |
105 **kw) | |
106 stdout, stderr = proc.communicate(input=test_in) | |
107 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') | |
99 | 108 |
100 | 109 |
101 def test_render_no_includes(rootdir, dstfile): | 110 @pytest.mark.parametrize('test_input, args', [ |
102 run_script(str(rootdir.join('simple.txt')), str(dstfile)) | 111 ('None', ("'simple.txt'", 'str(dstfile)')), |
Vasily Kuznetsov
2018/07/13 17:41:04
We iterate over the inner tuple, so according to o
| |
103 result = dstfile.read() | 112 ('None', ("'simple.txt'",)), |
104 assert 'Ok' in result | 113 ("rootdir.join('simple.txt').read()", ()), |
105 assert '! Checksum:' in result | 114 ]) |
115 def test_render_no_includes(test_input, args, rootdir, dstfile): | |
116 _locals = locals() | |
117 test_input = eval(test_input, _locals) | |
Vasily Kuznetsov
2018/07/13 17:41:04
But this eval doesn't need the _locals, right? It
Tudor Avram
2018/07/16 09:17:11
Done.
| |
118 args = [eval(arg, _locals) for arg in args] | |
Vasily Kuznetsov
2018/07/13 17:41:04
It seems that if you use map(eval, args) instead o
Tudor Avram
2018/07/16 09:17:12
Done.
| |
119 | |
120 _, _, stdout = run_script(*args, cwd=str(rootdir), test_in=test_input) | |
121 | |
122 if len(args) > 1: | |
123 assert 'Ok' in dstfile.read() | |
124 else: | |
125 assert 'Ok' in stdout | |
106 | 126 |
107 | 127 |
108 def test_render_unicode(rootdir, dstfile): | 128 def test_render_unicode(rootdir, dstfile): |
109 code, err = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) | 129 code, err, _ = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) |
110 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') | 130 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') |
111 | 131 |
112 | 132 |
113 def test_render_with_includes(rootdir, dstfile): | 133 def test_render_with_includes(rootdir, dstfile): |
114 run_script(str(rootdir.join('includer.txt')), str(dstfile), | 134 run_script(str(rootdir.join('includer.txt')), str(dstfile), |
115 '-i', 'inc=' + str(rootdir.join('inc'))) | 135 '-i', 'inc=' + str(rootdir.join('inc'))) |
116 assert 'I am included!' in dstfile.read() | 136 assert 'I am included!' in dstfile.read() |
117 | 137 |
118 | 138 |
119 def test_render_with_includes_relative(rootdir, dstfile): | 139 def test_render_with_includes_relative(rootdir, dstfile): |
120 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir)) | 140 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir)) |
121 assert 'I am included!' in dstfile.read() | 141 assert 'I am included!' in dstfile.read() |
122 | 142 |
123 | 143 |
124 def test_render_verbose(rootdir, dstfile): | 144 def test_render_verbose(rootdir, dstfile): |
125 code, err = run_script('includer.txt', str(dstfile), | 145 code, err, _ = run_script('includer.txt', str(dstfile), |
126 '-i', 'inc=inc', '-v', cwd=str(rootdir)) | 146 '-i', 'inc=inc', '-v', cwd=str(rootdir)) |
127 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' | 147 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' |
128 | 148 |
129 | 149 |
130 def test_no_header(rootdir, dstfile): | 150 def test_no_header(rootdir, dstfile): |
131 code, err = run_script('inc/includee.txt', str(dstfile), cwd=str(rootdir)) | 151 code, err, _ = run_script('inc/includee.txt', str(dstfile), |
152 cwd=str(rootdir)) | |
132 assert code == 1 | 153 assert code == 1 |
133 assert err == 'No header found at the beginning of the input.\n' | 154 assert err == 'No header found at the beginning of the input.\n' |
134 | 155 |
135 | 156 |
136 def test_wrong_file(dstfile): | 157 def test_wrong_file(dstfile): |
137 code, err = run_script('wrong.txt', str(dstfile)) | 158 code, err, _ = run_script('wrong.txt', str(dstfile)) |
138 assert code == 1 | 159 assert code == 1 |
139 assert err == "File not found: 'wrong.txt'\n" | 160 assert err == "File not found: 'wrong.txt'\n" |
140 | 161 |
141 | 162 |
142 def test_wrong_include_source(rootdir, dstfile): | 163 def test_wrong_include_source(rootdir, dstfile): |
143 code, err = run_script('brk.txt', str(dstfile), cwd=str(rootdir)) | 164 code, err, _ = run_script('brk.txt', str(dstfile), cwd=str(rootdir)) |
144 assert code == 1 | 165 assert code == 1 |
145 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' " | 166 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' " |
146 "from 'brk.txt'\n") | 167 "from 'brk.txt'\n") |
147 | 168 |
148 | 169 |
149 def test_wrong_include(rootdir, dstfile): | 170 def test_wrong_include(rootdir, dstfile): |
150 code, err = run_script('brk.txt', str(dstfile), | 171 code, err, _ = run_script('brk.txt', str(dstfile), |
151 '-i', 'inc=inc', cwd=str(rootdir)) | 172 '-i', 'inc=inc', cwd=str(rootdir)) |
152 missing_path = str(rootdir.join('inc', 'missing.txt')) | 173 missing_path = str(rootdir.join('inc', 'missing.txt')) |
153 expect = ("File not found: '{}' when including 'missing.txt' " | 174 expect = ("File not found: '{}' when including 'missing.txt' " |
154 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path) | 175 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path) |
155 assert code == 1 | 176 assert code == 1 |
156 assert err == expect | 177 assert err == expect |
157 | 178 |
158 | 179 |
159 def test_circular_includes(rootdir, dstfile): | 180 def test_circular_includes(rootdir, dstfile): |
160 code, err = run_script('circ.txt', str(dstfile), | 181 code, err, _ = run_script('circ.txt', str(dstfile), |
161 '-i', 'inc=inc', cwd=str(rootdir)) | 182 '-i', 'inc=inc', cwd=str(rootdir)) |
162 expect = ("Include loop encountered when including 'circular.txt' " | 183 expect = ("Include loop encountered when including 'circular.txt' " |
163 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n") | 184 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n") |
164 assert code == 1 | 185 assert code == 1 |
165 assert err == expect | 186 assert err == expect |
166 | 187 |
167 | 188 |
168 def test_wrong_source(rootdir, dstfile): | 189 def test_wrong_source(rootdir, dstfile): |
169 code, err = run_script('foo:bar.txt', str(dstfile)) | 190 code, err, _ = run_script('foo:bar.txt', str(dstfile)) |
170 assert code == 1 | 191 assert code == 1 |
171 assert err == "Unknown source: 'foo'\n" | 192 assert err == "Unknown source: 'foo'\n" |
172 | 193 |
173 | 194 |
174 @pytest.mark.tryfirst | 195 @pytest.mark.tryfirst |
175 @pytest.mark.slowtest | 196 @pytest.mark.slowtest |
176 def test_web_include(rootdir, dstfile, webserver_port): | 197 def test_web_include(rootdir, dstfile, webserver_port): |
177 url = 'http://localhost:{}/metainc.txt'.format(webserver_port) | 198 url = 'http://localhost:{}/metainc.txt'.format(webserver_port) |
178 webinc = rootdir.join('webinc.txt') | 199 webinc = rootdir.join('webinc.txt') |
179 webinc.write('[Adblock]\n%include {}%'.format(url)) | 200 webinc.write('[Adblock]\n%include {}%'.format(url)) |
180 code, err = run_script(str(webinc), str(dstfile)) | 201 code, err, _ = run_script(str(webinc), str(dstfile)) |
181 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8') | 202 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8') |
182 | 203 |
183 | 204 |
184 @pytest.mark.slowtest | 205 @pytest.mark.slowtest |
185 def test_failed_web_include(rootdir, dstfile, webserver_port): | 206 def test_failed_web_include(rootdir, dstfile, webserver_port): |
186 url = 'http://localhost:{}/missing.txt'.format(webserver_port) | 207 url = 'http://localhost:{}/missing.txt'.format(webserver_port) |
187 webinc = rootdir.join('webinc.txt') | 208 webinc = rootdir.join('webinc.txt') |
188 webinc.write('[Adblock]\n%include {}%'.format(url)) | 209 webinc.write('[Adblock]\n%include {}%'.format(url)) |
189 code, err = run_script(str(webinc), str(dstfile)) | 210 code, err, _ = run_script(str(webinc), str(dstfile)) |
190 assert code == 1 | 211 assert code == 1 |
191 assert err.startswith( | 212 assert err.startswith( |
192 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) | 213 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) |
OLD | NEW |