Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
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 |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import contextlib | 16 import contextlib |
17 import os | 17 import os |
18 import signal | 18 import signal |
19 import subprocess | 19 import subprocess |
20 import time | 20 import time |
21 import zipfile | |
22 from io import BytesIO | |
23 import pytest | |
Vasily Kuznetsov
2018/09/26 15:45:27
Nit: there should be an empty line before pytest,
Tudor Avram
2018/10/04 06:48:15
Done.
| |
21 | 24 |
22 | 25 |
23 def get_dir_contents(path): | 26 def get_dir_contents(path): |
24 # TODO: This function is duplicated in test_page_outputs.py. | 27 # TODO: This function is duplicated in test_page_outputs.py. |
25 dirdata = {} | 28 dirdata = {} |
26 for dirpath, dirnames, filenames in os.walk(path): | 29 for dirpath, dirnames, filenames in os.walk(path): |
27 for output_file in filenames: | 30 for output_file in filenames: |
28 filepath = os.path.join(dirpath, output_file) | 31 filepath = os.path.join(dirpath, output_file) |
29 with open(filepath) as f: | 32 with open(filepath) as f: |
30 locale = os.path.split(os.path.split(filepath)[0])[1] | 33 locale = os.path.split(os.path.split(filepath)[0])[1] |
31 dirdata[os.path.join(locale, output_file)] = f.read().strip() | 34 dirdata[os.path.join(locale, output_file)] = f.read().strip() |
32 return dirdata | 35 return dirdata |
33 | 36 |
34 | 37 |
35 @contextlib.contextmanager | 38 @contextlib.contextmanager |
36 def run_test_server(site_path): | 39 def run_test_server(site_path): |
37 """Run test server, yield its URL. Terminate server on next iteration. | 40 """Run test server, yield its URL. Terminate server on next iteration. |
38 | 41 |
39 This function is intended be used in a pytest fixture. | 42 This function is intended be used in a pytest fixture. |
40 """ | 43 """ |
41 args = ['python', 'runserver.py', site_path] | 44 args = ['python', 'runserver.py', site_path] |
42 # Werkzeug is a dependency of flask which we are using for the mock api | 45 # Werkzeug is a dependency of flask which we are using for the mock api |
43 # however there is an issue with Werkzeug that prevents it from properly | 46 # however there is an issue with Werkzeug that prevents it from properly |
44 # handling the SIGTERM sent by p.kill() or terminate() | 47 # handling the SIGTERM sent by p.kill() or terminate() |
45 # Issue: https://github.com/pallets/werkzeug/issues/58 | 48 # Issue: https://github.com/pallets/werkzeug/issues/58 |
46 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) | 49 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) |
47 time.sleep(0.5) | 50 time.sleep(0.5) |
48 yield 'http://localhost:5000/' | 51 yield 'http://localhost:5000/' |
49 os.killpg(os.getpgid(p.pid), signal.SIGTERM) | 52 os.killpg(os.getpgid(p.pid), signal.SIGTERM) |
53 | |
54 | |
55 def create_in_memory_zip(file_names, file_data): | |
56 """Create a BytesIO object with the contents of a zip file. | |
57 | |
58 Parameters | |
59 ---------- | |
60 file_names: iterable | |
61 Of file names. Should be the full paths of the file inside the zip. | |
62 file_data: iterable | |
63 The data to be contained of the files. | |
64 | |
65 Returns | |
66 ------- | |
67 BytesIO | |
68 The resulting in-memory zip file. | |
69 | |
70 """ | |
71 memory_zip = BytesIO() | |
72 | |
73 with zipfile.ZipFile(memory_zip, 'w') as zf: | |
74 for idx in range(len(file_names)): | |
75 zf.writestr(file_names[idx], file_data[idx], zipfile.ZIP_DEFLATED) | |
76 | |
77 memory_zip.seek(0) | |
78 return memory_zip | |
79 | |
80 | |
81 def exception_test(func, exception, exp_msg, *args, **kw): | |
82 with pytest.raises(exception) as err: | |
83 func(*args, **kw) | |
84 | |
85 assert exp_msg in str(err.value) | |
OLD | NEW |