| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 test_in = None | 96 |
| 97 if 'test_in'in kw.keys(): | 97 test_in = kw.pop('test_in', None) |
|
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 test_in = kw.pop('test_in').encode('utf-8') | 98 if test_in is not None: |
| 99 test_in = test_in.encode('utf-8') | |
| 100 | |
| 99 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, | 101 proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 100 stdout=subprocess.PIPE, stdin=subprocess.PIPE, | 102 stdout=subprocess.PIPE, stdin=subprocess.PIPE, |
| 101 **kw) | 103 **kw) |
| 102 stdout, stderr = proc.communicate(input=test_in) | 104 stdout, stderr = proc.communicate(input=test_in) |
| 103 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') | 105 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') |
| 104 | 106 |
| 105 | 107 |
| 106 def test_render_no_includes(rootdir, dstfile): | 108 @pytest.mark.parametrize('test_input, args', [ |
| 107 run_script(str(rootdir.join('simple.txt')), str(dstfile)) | 109 ('None', ["'simple.txt'", 'str(dstfile)']), |
| 108 result = dstfile.read() | 110 ('None', ["'simple.txt'"]), |
| 109 assert 'Ok' in result | 111 ("rootdir.join('simple.txt').read()", []), |
| 110 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 | |
| 111 | 125 |
| 112 | 126 |
| 113 def test_render_unicode(rootdir, dstfile): | 127 def test_render_unicode(rootdir, dstfile): |
| 114 code, err, _ = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) | 128 code, err, _ = run_script(str(rootdir.join('unicode.txt')), str(dstfile)) |
| 115 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') | 129 assert '\u1234' in dstfile.read(mode='rb').decode('utf-8') |
| 116 | 130 |
| 117 | 131 |
| 118 def test_render_with_includes(rootdir, dstfile): | 132 def test_render_with_includes(rootdir, dstfile): |
| 119 run_script(str(rootdir.join('includer.txt')), str(dstfile), | 133 run_script(str(rootdir.join('includer.txt')), str(dstfile), |
| 120 '-i', 'inc=' + str(rootdir.join('inc'))) | 134 '-i', 'inc=' + str(rootdir.join('inc'))) |
| 121 assert 'I am included!' in dstfile.read() | 135 assert 'I am included!' in dstfile.read() |
| 122 | 136 |
| 123 | 137 |
| 124 def test_render_with_includes_relative(rootdir, dstfile): | 138 def test_render_with_includes_relative(rootdir, dstfile): |
| 125 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)) |
| 126 assert 'I am included!' in dstfile.read() | 140 assert 'I am included!' in dstfile.read() |
| 127 | 141 |
| 128 | 142 |
| 129 def test_render_verbose(rootdir, dstfile): | 143 def test_render_verbose(rootdir, dstfile): |
| 130 code, err, _ = run_script('includer.txt', str(dstfile), | 144 code, err, _ = run_script('includer.txt', str(dstfile), |
| 131 '-i', 'inc=inc', '-v', cwd=str(rootdir)) | 145 '-i', 'inc=inc', '-v', cwd=str(rootdir)) |
| 132 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' | 146 assert err == 'Rendering: includer.txt\n- including: inc:includee.txt\n' |
| 133 | |
| 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 | 147 |
| 154 | 148 |
| 155 def test_no_header(rootdir, dstfile): | 149 def test_no_header(rootdir, dstfile): |
| 156 code, err, _ = run_script('inc/includee.txt', str(dstfile), | 150 code, err, _ = run_script('inc/includee.txt', str(dstfile), |
| 157 cwd=str(rootdir)) | 151 cwd=str(rootdir)) |
| 158 assert code == 1 | 152 assert code == 1 |
| 159 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' |
| 160 | 154 |
| 161 | 155 |
| 162 def test_wrong_file(dstfile): | 156 def test_wrong_file(dstfile): |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 | 203 |
| 210 @pytest.mark.slowtest | 204 @pytest.mark.slowtest |
| 211 def test_failed_web_include(rootdir, dstfile, webserver_port): | 205 def test_failed_web_include(rootdir, dstfile, webserver_port): |
| 212 url = 'http://localhost:{}/missing.txt'.format(webserver_port) | 206 url = 'http://localhost:{}/missing.txt'.format(webserver_port) |
| 213 webinc = rootdir.join('webinc.txt') | 207 webinc = rootdir.join('webinc.txt') |
| 214 webinc.write('[Adblock]\n%include {}%'.format(url)) | 208 webinc.write('[Adblock]\n%include {}%'.format(url)) |
| 215 code, err, _ = run_script(str(webinc), str(dstfile)) | 209 code, err, _ = run_script(str(webinc), str(dstfile)) |
| 216 assert code == 1 | 210 assert code == 1 |
| 217 assert err.startswith( | 211 assert err.startswith( |
| 218 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) | 212 "HTTP 404 Not found: '{0}' when including '{0}'".format(url)) |
| LEFT | RIGHT |