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 = kw.pop('test_in', None) |
98 return proc.returncode, stderr.decode('utf-8') | 98 if test_in is not None: |
| 99 test_in = test_in.encode('utf-8') |
| 100 |
| 101 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 102 stdout=subprocess.PIPE, stdin=subprocess.PIPE, |
| 103 **kw) |
| 104 stdout, stderr = proc.communicate(input=test_in) |
| 105 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') |
99 | 106 |
100 | 107 |
101 def test_render_no_includes(rootdir, dstfile): | 108 @pytest.mark.parametrize('test_input, args', [ |
102 run_script(str(rootdir.join('simple.txt')), str(dstfile)) | 109 ('None', ["'simple.txt'", 'str(dstfile)']), |
103 result = dstfile.read() | 110 ('None', ["'simple.txt'"]), |
104 assert 'Ok' in result | 111 ("rootdir.join('simple.txt').read()", []), |
105 assert '! Checksum:' in result | 112 ]) |
| 113 def test_render_no_includes(test_input, args, rootdir, dstfile): |
| 114 _locals = locals() |
| 115 test_input = eval(test_input) |
| 116 args = list(map(eval, args)) |
| 117 _, _, stdout = run_script(*args, cwd=str(rootdir), test_in=test_input) |
| 118 |
| 119 if len(args) > 1: |
| 120 output = dstfile.read() |
| 121 else: |
| 122 output = stdout |
| 123 |
| 124 assert 'Ok' in output |
| 125 assert '! Checksum:' in output |
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 |