Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/utils.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Addressed comments on patch set #2 Created Oct. 3, 2018, 2:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
24 import pytest
21 25
22 26
23 def get_dir_contents(path): 27 def get_dir_contents(path):
24 # TODO: This function is duplicated in test_page_outputs.py. 28 # TODO: This function is duplicated in test_page_outputs.py.
25 dirdata = {} 29 dirdata = {}
26 for dirpath, dirnames, filenames in os.walk(path): 30 for dirpath, dirnames, filenames in os.walk(path):
27 for output_file in filenames: 31 for output_file in filenames:
28 filepath = os.path.join(dirpath, output_file) 32 filepath = os.path.join(dirpath, output_file)
29 with open(filepath) as f: 33 with open(filepath) as f:
30 locale = os.path.split(os.path.split(filepath)[0])[1] 34 locale = os.path.split(os.path.split(filepath)[0])[1]
31 dirdata[os.path.join(locale, output_file)] = f.read().strip() 35 dirdata[os.path.join(locale, output_file)] = f.read().strip()
32 return dirdata 36 return dirdata
33 37
34 38
35 @contextlib.contextmanager 39 @contextlib.contextmanager
36 def run_test_server(site_path): 40 def run_test_server(site_path):
37 """Run test server, yield its URL. Terminate server on next iteration. 41 """Run test server, yield its URL. Terminate server on next iteration.
38 42
39 This function is intended be used in a pytest fixture. 43 This function is intended be used in a pytest fixture.
40 """ 44 """
41 args = ['python', 'runserver.py', site_path] 45 args = ['python', 'runserver.py', site_path]
42 # Werkzeug is a dependency of flask which we are using for the mock api 46 # 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 47 # however there is an issue with Werkzeug that prevents it from properly
44 # handling the SIGTERM sent by p.kill() or terminate() 48 # handling the SIGTERM sent by p.kill() or terminate()
45 # Issue: https://github.com/pallets/werkzeug/issues/58 49 # Issue: https://github.com/pallets/werkzeug/issues/58
46 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) 50 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid)
47 time.sleep(0.5) 51 time.sleep(0.5)
48 yield 'http://localhost:5000/' 52 yield 'http://localhost:5000/'
49 os.killpg(os.getpgid(p.pid), signal.SIGTERM) 53 os.killpg(os.getpgid(p.pid), signal.SIGTERM)
54
55
56 def create_in_memory_zip(file_names, file_data):
57 """Create a BytesIO object with the contents of a zip file.
58
59 Parameters
60 ----------
61 file_names: iterable
62 Of file names. Should be the full paths of the file inside the zip.
63 file_data: iterable
64 The data to be contained of the files.
65
66 Returns
67 -------
68 BytesIO
69 The resulting in-memory zip file.
70
71 """
72 memory_zip = BytesIO()
73
74 with zipfile.ZipFile(memory_zip, 'w') as zf:
75 for idx in range(len(file_names)):
76 zf.writestr(file_names[idx], file_data[idx], zipfile.ZIP_DEFLATED)
77
78 memory_zip.seek(0)
79 return memory_zip
80
81
82 def exception_test(func, exception, exp_msg, *args, **kw):
83 with pytest.raises(exception) as err:
84 func(*args, **kw)
85
86 assert exp_msg in str(err.value)
OLDNEW

Powered by Google App Engine
This is Rietveld