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 test_input = eval(test_input) |
| 115 args = list(map(eval, args)) |
| 116 _, _, stdout = run_script(*args, cwd=str(rootdir), test_in=test_input) |
| 117 |
| 118 if len(args) > 1: |
| 119 output = dstfile.read() |
| 120 else: |
| 121 output = stdout |
| 122 |
| 123 assert 'Ok' in output |
| 124 assert '! Checksum:' in output |
106 | 125 |
107 | 126 |
108 def test_render_unicode(rootdir, dstfile): | 127 def test_render_unicode(rootdir, dstfile): |
109 code, err = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) | 128 code, err, _ = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) |
110 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') | 129 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') |
111 | 130 |
112 | 131 |
113 def test_render_with_includes(rootdir, dstfile): | 132 def test_render_with_includes(rootdir, dstfile): |
114 run_script(str(rootdir.join('includer.txt')), str(dstfile), | 133 run_script(str(rootdir.join('includer.txt')), str(dstfile), |
115 '-i', 'inc=' + str(rootdir.join('inc'))) | 134 '-i', 'inc=' + str(rootdir.join('inc'))) |
116 assert 'I am included!' in dstfile.read() | 135 assert 'I am included!' in dstfile.read() |
117 | 136 |
118 | 137 |
119 def test_render_with_includes_relative(rootdir, dstfile): | 138 def test_render_with_includes_relative(rootdir, dstfile): |
120 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir)) | 139 run_script('includer.txt', str(dstfile), '-i', 'inc=inc', cwd=str(rootdir)) |
121 assert 'I am included!' in dstfile.read() | 140 assert 'I am included!' in dstfile.read() |
122 | 141 |
123 | 142 |
124 def test_render_verbose(rootdir, dstfile): | 143 def test_render_verbose(rootdir, dstfile): |
125 code, err = run_script('includer.txt', str(dstfile), | 144 code, err, _ = run_script('includer.txt', str(dstfile), |
126 '-i', 'inc=inc', '-v', cwd=str(rootdir)) | 145 '-i', 'inc=inc', '-v', cwd=str(rootdir)) |
127 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' | 146 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' |
128 | 147 |
129 | 148 |
130 def test_no_header(rootdir, dstfile): | 149 def test_no_header(rootdir, dstfile): |
131 code, err = run_script('inc/includee.txt', str(dstfile), cwd=str(rootdir)) | 150 code, err, _ = run_script('inc/includee.txt', str(dstfile), |
| 151 cwd=str(rootdir)) |
132 assert code == 1 | 152 assert code == 1 |
133 assert err == 'No header found at the beginning of the input.\n' | 153 assert err == 'No header found at the beginning of the input.\n' |
134 | 154 |
135 | 155 |
136 def test_wrong_file(dstfile): | 156 def test_wrong_file(dstfile): |
137 code, err = run_script('wrong.txt', str(dstfile)) | 157 code, err, _ = run_script('wrong.txt', str(dstfile)) |
138 assert code == 1 | 158 assert code == 1 |
139 assert err == "File not found: 'wrong.txt'\n" | 159 assert err == "File not found: 'wrong.txt'\n" |
140 | 160 |
141 | 161 |
142 def test_wrong_include_source(rootdir, dstfile): | 162 def test_wrong_include_source(rootdir, dstfile): |
143 code, err = run_script('brk.txt', str(dstfile), cwd=str(rootdir)) | 163 code, err, _ = run_script('brk.txt', str(dstfile), cwd=str(rootdir)) |
144 assert code == 1 | 164 assert code == 1 |
145 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' " | 165 assert err == ("Unknown source: 'inc' when including 'inc:broken.txt' " |
146 "from 'brk.txt'\n") | 166 "from 'brk.txt'\n") |
147 | 167 |
148 | 168 |
149 def test_wrong_include(rootdir, dstfile): | 169 def test_wrong_include(rootdir, dstfile): |
150 code, err = run_script('brk.txt', str(dstfile), | 170 code, err, _ = run_script('brk.txt', str(dstfile), |
151 '-i', 'inc=inc', cwd=str(rootdir)) | 171 '-i', 'inc=inc', cwd=str(rootdir)) |
152 missing_path = str(rootdir.join('inc', 'missing.txt')) | 172 missing_path = str(rootdir.join('inc', 'missing.txt')) |
153 expect = ("File not found: '{}' when including 'missing.txt' " | 173 expect = ("File not found: '{}' when including 'missing.txt' " |
154 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path) | 174 "from 'inc:broken.txt' from 'brk.txt'\n").format(missing_path) |
155 assert code == 1 | 175 assert code == 1 |
156 assert err == expect | 176 assert err == expect |
157 | 177 |
158 | 178 |
159 def test_circular_includes(rootdir, dstfile): | 179 def test_circular_includes(rootdir, dstfile): |
160 code, err = run_script('circ.txt', str(dstfile), | 180 code, err, _ = run_script('circ.txt', str(dstfile), |
161 '-i', 'inc=inc', cwd=str(rootdir)) | 181 '-i', 'inc=inc', cwd=str(rootdir)) |
162 expect = ("Include loop encountered when including 'circular.txt' " | 182 expect = ("Include loop encountered when including 'circular.txt' " |
163 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n") | 183 "from 'circular.txt' from 'inc:circular.txt' from 'circ.txt'\n") |
164 assert code == 1 | 184 assert code == 1 |
165 assert err == expect | 185 assert err == expect |
166 | 186 |
167 | 187 |
168 def test_wrong_source(rootdir, dstfile): | 188 def test_wrong_source(rootdir, dstfile): |
169 code, err = run_script('foo:bar.txt', str(dstfile)) | 189 code, err, _ = run_script('foo:bar.txt', str(dstfile)) |
170 assert code == 1 | 190 assert code == 1 |
171 assert err == "Unknown source: 'foo'\n" | 191 assert err == "Unknown source: 'foo'\n" |
172 | 192 |
173 | 193 |
174 @pytest.mark.tryfirst | 194 @pytest.mark.tryfirst |
175 @pytest.mark.slowtest | 195 @pytest.mark.slowtest |
176 def test_web_include(rootdir, dstfile, webserver_port): | 196 def test_web_include(rootdir, dstfile, webserver_port): |
177 url = 'http://localhost:{}/metainc.txt'.format(webserver_port) | 197 url = 'http://localhost:{}/metainc.txt'.format(webserver_port) |
178 webinc = rootdir.join('webinc.txt') | 198 webinc = rootdir.join('webinc.txt') |
179 webinc.write('[Adblock]\n%include {}%'.format(url)) | 199 webinc.write('[Adblock]\n%include {}%'.format(url)) |
180 code, err = run_script(str(webinc), str(dstfile)) | 200 code, err, _ = run_script(str(webinc), str(dstfile)) |
181 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8') | 201 assert 'Web \u1234' in dstfile.read(mode='rb').decode('utf-8') |
182 | 202 |
183 | 203 |
184 @pytest.mark.slowtest | 204 @pytest.mark.slowtest |
185 def test_failed_web_include(rootdir, dstfile, webserver_port): | 205 def test_failed_web_include(rootdir, dstfile, webserver_port): |
186 url = 'http://localhost:{}/missing.txt'.format(webserver_port) | 206 url = 'http://localhost:{}/missing.txt'.format(webserver_port) |
187 webinc = rootdir.join('webinc.txt') | 207 webinc = rootdir.join('webinc.txt') |
188 webinc.write('[Adblock]\n%include {}%'.format(url)) | 208 webinc.write('[Adblock]\n%include {}%'.format(url)) |
189 code, err = run_script(str(webinc), str(dstfile)) | 209 code, err, _ = run_script(str(webinc), str(dstfile)) |
190 assert code == 1 | 210 assert code == 1 |
191 assert err.startswith( | 211 assert err.startswith( |
192 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) | 212 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) |
OLD | NEW |