summaryrefslogtreecommitdiff
path: root/tests/test_runner.py
blob: 2ac302ce3ef2ef121bd8b35fed934e015bf2b253 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import contextlib
import os
import sys

if sys.version_info[:2] == (2, 6):  # pragma: no cover
    import unittest2 as unittest
else:  # pragma: no cover
    import unittest

from waitress import runner


class Test_match(unittest.TestCase):
    def test_empty(self):
        self.assertRaisesRegexp(
            ValueError, "^Malformed application ''$", runner.match, ""
        )

    def test_module_only(self):
        self.assertRaisesRegexp(
            ValueError, r"^Malformed application 'foo\.bar'$", runner.match, "foo.bar"
        )

    def test_bad_module(self):
        self.assertRaisesRegexp(
            ValueError,
            r"^Malformed application 'foo#bar:barney'$",
            runner.match,
            "foo#bar:barney",
        )

    def test_module_obj(self):
        self.assertTupleEqual(
            runner.match("foo.bar:fred.barney"), ("foo.bar", "fred.barney")
        )


class Test_resolve(unittest.TestCase):
    def test_bad_module(self):
        self.assertRaises(
            ImportError, runner.resolve, "nonexistent", "nonexistent_function"
        )

    def test_nonexistent_function(self):
        self.assertRaisesRegexp(
            AttributeError,
            r"has no attribute 'nonexistent_function'",
            runner.resolve,
            "os.path",
            "nonexistent_function",
        )

    def test_simple_happy_path(self):
        from os.path import exists

        self.assertIs(runner.resolve("os.path", "exists"), exists)

    def test_complex_happy_path(self):
        # Ensure we can recursively resolve object attributes if necessary.
        self.assertEquals(runner.resolve("os.path", "exists.__name__"), "exists")


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.assertRegexpMatches(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: Malformed application 'a'")

    def test_bad_app_module(self):
        self.match_output(["nonexistent:a"], 1, "^Error: Bad module '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: Bad object name '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.assertEqual(show_exception(sys.stderr), None)
            self.assertRegexpMatches(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.assertEqual(show_exception(sys.stderr), None)
            self.assertRegexpMatches(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__