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

Side by Side Diff: tests/utils.py

Issue 29912588: Issue 7019 - [CMS] Refactor `test_server.py` (Closed)
Patch Set: Added timeout option when setting up server Created Oct. 24, 2018, 3:57 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
« no previous file with comments | « tests/test_page_outputs.py ('k') | tox.ini » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 19 matching lines...) Expand all
30 for dirpath, dirnames, filenames in os.walk(path): 30 for dirpath, dirnames, filenames in os.walk(path):
31 for output_file in filenames: 31 for output_file in filenames:
32 filepath = os.path.join(dirpath, output_file) 32 filepath = os.path.join(dirpath, output_file)
33 with open(filepath) as f: 33 with open(filepath) as f:
34 locale = os.path.split(os.path.split(filepath)[0])[1] 34 locale = os.path.split(os.path.split(filepath)[0])[1]
35 dirdata[os.path.join(locale, output_file)] = f.read().strip() 35 dirdata[os.path.join(locale, output_file)] = f.read().strip()
36 return dirdata 36 return dirdata
37 37
38 38
39 @contextlib.contextmanager 39 @contextlib.contextmanager
40 def run_test_server(site_path): 40 def run_test_server(site_path, new_env=None):
41 """Run test server, yield its URL. Terminate server on next iteration. 41 """Run test server, yield its URL. Terminate server on next iteration.
42 42
43 This function is intended be used in a pytest fixture. 43 This function is intended be used in a pytest fixture.
44
45 Parameters
46 ----------
47 site_path: str
48 The path to the website's source code.
49 new_env: dict
50 The environment under which the server will be run. If `None`, this
51 will be inherited from the main process.
52
53 Returns
54 -------
55 str
56 The url where the server runs.
57
44 """ 58 """
45 args = ['python', 'runserver.py', site_path] 59 args = ['python', 'runserver.py', site_path]
46 # Werkzeug is a dependency of flask which we are using for the mock api 60 # Werkzeug is a dependency of flask which we are using for the mock api
47 # however there is an issue with Werkzeug that prevents it from properly 61 # however there is an issue with Werkzeug that prevents it from properly
48 # handling the SIGTERM sent by p.kill() or terminate() 62 # handling the SIGTERM sent by p.kill() or terminate()
49 # Issue: https://github.com/pallets/werkzeug/issues/58 63 # Issue: https://github.com/pallets/werkzeug/issues/58
50 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid) 64 p = subprocess.Popen(args, stdout=subprocess.PIPE, preexec_fn=os.setsid,
65 env=new_env)
51 time.sleep(0.5) 66 time.sleep(0.5)
52 yield 'http://localhost:5000/' 67 yield 'http://localhost:5000/'
53 os.killpg(os.getpgid(p.pid), signal.SIGTERM) 68 os.killpg(os.getpgid(p.pid), signal.SIGINT)
54 69
55 70
56 def create_in_memory_zip(file_names, file_data): 71 def create_in_memory_zip(file_names, file_data):
57 """Create a BytesIO object with the contents of a zip file. 72 """Create a BytesIO object with the contents of a zip file.
58 73
59 Parameters 74 Parameters
60 ---------- 75 ----------
61 file_names: iterable 76 file_names: iterable
62 Of file names. Should be the full paths of the file inside the zip. 77 Of file names. Should be the full paths of the file inside the zip.
63 file_data: iterable 78 file_data: iterable
(...skipping 13 matching lines...) Expand all
77 92
78 memory_zip.seek(0) 93 memory_zip.seek(0)
79 return memory_zip 94 return memory_zip
80 95
81 96
82 def exception_test(func, exception, exp_msg, *args, **kw): 97 def exception_test(func, exception, exp_msg, *args, **kw):
83 with pytest.raises(exception) as err: 98 with pytest.raises(exception) as err:
84 func(*args, **kw) 99 func(*args, **kw)
85 100
86 assert exp_msg in str(err.value) 101 assert exp_msg in str(err.value)
OLDNEW
« no previous file with comments | « tests/test_page_outputs.py ('k') | tox.ini » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld