1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
import contextlib
import os
import sys
import unittest
from waitress import runner
class Test_run(unittest.TestCase):
def match_output(self, argv, code, regex):
argv = ["waitress-serve"] + argv
with capture() as captured:
self.assertEqual(runner.run(argv=argv), code)
self.assertRegex(captured.getvalue(), regex)
captured.close()
def test_bad(self):
self.match_output(["--bad-opt"], 1, "^Error: option --bad-opt not recognized")
def test_help(self):
self.match_output(["--help"], 0, "^Usage:\n\n waitress-serve")
def test_no_app(self):
self.match_output([], 1, "^Error: Specify one application only")
def test_multiple_apps_app(self):
self.match_output(["a:a", "b:b"], 1, "^Error: Specify one application only")
def test_bad_apps_app(self):
self.match_output(["a"], 1, "^Error: No module named 'a'")
def test_bad_app_module(self):
self.match_output(["nonexistent:a"], 1, "^Error: No module named 'nonexistent'")
self.match_output(
["nonexistent:a"],
1,
(
r"There was an exception \((ImportError|ModuleNotFoundError)\) "
"importing your module.\n\nIt had these arguments: \n"
"1. No module named '?nonexistent'?"
),
)
def test_cwd_added_to_path(self):
def null_serve(app, **kw):
pass
sys_path = sys.path
current_dir = os.getcwd()
try:
os.chdir(os.path.dirname(__file__))
argv = [
"waitress-serve",
"fixtureapps.runner:app",
]
self.assertEqual(runner.run(argv=argv, _serve=null_serve), 0)
finally:
sys.path = sys_path
os.chdir(current_dir)
def test_bad_app_object(self):
self.match_output(
["tests.fixtureapps.runner:a"],
1,
"^Error: module 'tests.fixtureapps.runner' has no attribute 'a'",
)
def test_simple_call(self):
from tests.fixtureapps import runner as _apps
def check_server(app, **kw):
self.assertIs(app, _apps.app)
self.assertDictEqual(kw, {"port": "80"})
argv = [
"waitress-serve",
"--port=80",
"tests.fixtureapps.runner:app",
]
self.assertEqual(runner.run(argv=argv, _serve=check_server), 0)
def test_returned_app(self):
from tests.fixtureapps import runner as _apps
def check_server(app, **kw):
self.assertIs(app, _apps.app)
self.assertDictEqual(kw, {"port": "80"})
argv = [
"waitress-serve",
"--port=80",
"--call",
"tests.fixtureapps.runner:returns_app",
]
self.assertEqual(runner.run(argv=argv, _serve=check_server), 0)
class Test_helper(unittest.TestCase):
def test_exception_logging(self):
from waitress.runner import show_exception
regex = (
r"There was an exception \(ImportError\) importing your module."
r"\n\nIt had these arguments: \n1. My reason"
)
with capture() as captured:
try:
raise ImportError("My reason")
except ImportError:
self.assertIsNone(show_exception(sys.stderr))
self.assertRegex(captured.getvalue(), regex)
captured.close()
regex = (
r"There was an exception \(ImportError\) importing your module."
r"\n\nIt had no arguments."
)
with capture() as captured:
try:
raise ImportError
except ImportError:
self.assertIsNone(show_exception(sys.stderr))
self.assertRegex(captured.getvalue(), regex)
captured.close()
@contextlib.contextmanager
def capture():
from io import StringIO
fd = StringIO()
sys.stdout = fd
sys.stderr = fd
yield fd
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
|