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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
from argparse import Namespace
import io
import sys
import pytest
from falcon import App
from falcon import inspect
import falcon.asgi
from falcon.cmd import inspect_app
from falcon.testing import redirected
_WIN32 = sys.platform.startswith('win')
# NOTE(vytas): This is not the cleanest way to import as we lack __init__.py,
# but it works as pytest (when operating in the default "prepend" import mode)
# inserts the directory of every test file into sys.path.
_MODULE = 'test_cmd_inspect_app'
class DummyResource:
def on_get(self, req, resp):
resp.text = 'Test\n'
resp.status = '200 OK'
class DummyResourceAsync:
async def on_get(self, req, resp):
resp.text = 'Test\n'
resp.status = '200 OK'
def create_app(asgi):
app_cls = falcon.asgi.App if asgi else App
return app_cls()
def make_app(asgi=False):
app = create_app(asgi)
app.add_route('/test', DummyResourceAsync() if asgi else DummyResource())
return app
_APP = make_app()
@pytest.fixture
def app(asgi):
return make_app(asgi)
class TestMakeParser:
@pytest.mark.parametrize(
'args, exp',
(
(
['foo'],
Namespace(
app_module='foo', route_only=False, verbose=False, internal=False
),
),
(
['foo', '-r'],
Namespace(
app_module='foo', route_only=True, verbose=False, internal=False
),
),
(
['foo', '--route_only'],
Namespace(
app_module='foo', route_only=True, verbose=False, internal=False
),
),
(
['foo', '-v'],
Namespace(
app_module='foo', route_only=False, verbose=True, internal=False
),
),
(
['foo', '--verbose'],
Namespace(
app_module='foo', route_only=False, verbose=True, internal=False
),
),
(
['foo', '-i'],
Namespace(
app_module='foo', route_only=False, verbose=False, internal=True
),
),
(
['foo', '--internal'],
Namespace(
app_module='foo', route_only=False, verbose=False, internal=True
),
),
(
['foo', '-r', '-v', '-i'],
Namespace(
app_module='foo', route_only=True, verbose=True, internal=True
),
),
),
)
def test_make_parser(self, args, exp):
parser = inspect_app.make_parser()
actual = parser.parse_args(args)
assert actual == exp
def test_make_parser_error(self):
parser = inspect_app.make_parser()
with pytest.raises(SystemExit):
parser.parse_args([])
class TestLoadApp:
@pytest.mark.parametrize('name', ('_APP', 'make_app'))
def test_load_app(self, name):
parser = inspect_app.make_parser()
args = Namespace(
app_module='{}:{}'.format(_MODULE, name), route_only=False, verbose=False
)
app = inspect_app.load_app(parser, args)
assert isinstance(app, App)
assert app._router.find('/test') is not None
@pytest.mark.parametrize(
'name',
(
'foo', # not exists
'_MODULE', # not callable and not app
'DummyResource', # callable and not app
),
)
def test_load_app_error(self, name):
parser = inspect_app.make_parser()
args = Namespace(
app_module='{}:{}'.format(_MODULE, name), route_only=False, verbose=False
)
with pytest.raises(SystemExit):
inspect_app.load_app(parser, args)
def test_load_app_module_error(self):
parser = inspect_app.make_parser()
args = Namespace(app_module='foo', route_only=False, verbose=False)
with pytest.raises(SystemExit):
inspect_app.load_app(parser, args)
@pytest.mark.skipif(sys.version_info < (3, 6), reason='dict order is not stable')
@pytest.mark.parametrize('verbose', (True, False), ids=['verbose', 'not-verbose'])
@pytest.mark.parametrize('internal', (True, False), ids=['internal', 'not-internal'])
class TestMain:
def check(self, actual, expect):
if _WIN32:
# windows randomly returns the driver name as lowercase
assert actual.casefold() == expect.casefold()
else:
assert actual == expect
def test_routes_only(self, verbose, internal, monkeypatch):
args = ['some-file.py', '{}:{}'.format(_MODULE, '_APP'), '-r']
if verbose:
args.append('-v')
if internal:
args.append('-i')
monkeypatch.setattr('sys.argv', args)
output = io.StringIO()
with redirected(stdout=output):
inspect_app.main()
routes = inspect.inspect_routes(_APP)
sv = inspect.StringVisitor(verbose, internal)
expect = '\n'.join([sv.process(r) for r in routes])
self.check(output.getvalue().strip(), expect)
def test_inspect(self, verbose, internal, monkeypatch):
args = ['some-file.py', '{}:{}'.format(_MODULE, '_APP')]
if verbose:
args.append('-v')
if internal:
args.append('-i')
monkeypatch.setattr('sys.argv', args)
output = io.StringIO()
with redirected(stdout=output):
inspect_app.main()
ins = inspect.inspect_app(_APP)
self.check(output.getvalue().strip(), ins.to_string(verbose, internal))
def test_route_main(monkeypatch):
called = False
def mock():
nonlocal called
called = True
monkeypatch.setattr(inspect_app, 'main', mock)
output = io.StringIO()
with redirected(stderr=output):
with pytest.raises(SystemExit):
inspect_app.route_main()
assert 'no longer supported' in output.getvalue()
assert not called
|