Conditionally inject CLI arguments into factory (#2402)
This commit is contained in:
parent
c9dbc8ed26
commit
0030425c8c
|
@ -68,6 +68,13 @@ Or, a path to a directory to run as a simple HTTP server:
|
||||||
legacy_version = len(sys.argv) == 2 and sys.argv[-1] == "-v"
|
legacy_version = len(sys.argv) == 2 and sys.argv[-1] == "-v"
|
||||||
parse_args = ["--version"] if legacy_version else None
|
parse_args = ["--version"] if legacy_version else None
|
||||||
|
|
||||||
|
if not parse_args:
|
||||||
|
parsed, unknown = self.parser.parse_known_args()
|
||||||
|
if unknown and parsed.factory:
|
||||||
|
for arg in unknown:
|
||||||
|
if arg.startswith("--"):
|
||||||
|
self.parser.add_argument(arg.split("=")[0])
|
||||||
|
|
||||||
self.args = self.parser.parse_args(args=parse_args)
|
self.args = self.parser.parse_args(args=parse_args)
|
||||||
self._precheck()
|
self._precheck()
|
||||||
|
|
||||||
|
@ -128,6 +135,9 @@ Or, a path to a directory to run as a simple HTTP server:
|
||||||
module = import_module(module_name)
|
module = import_module(module_name)
|
||||||
app = getattr(module, app_name, None)
|
app = getattr(module, app_name, None)
|
||||||
if self.args.factory:
|
if self.args.factory:
|
||||||
|
try:
|
||||||
|
app = app(self.args)
|
||||||
|
except TypeError:
|
||||||
app = app()
|
app = app()
|
||||||
|
|
||||||
app_type_name = type(app).__name__
|
app_type_name = type(app).__name__
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
app = Sanic("FactoryTest")
|
|
||||||
return app
|
|
|
@ -34,3 +34,12 @@ async def shutdown(app: Sanic, _):
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
def create_app_with_args(args):
|
||||||
|
try:
|
||||||
|
print(f"foo={args.foo}")
|
||||||
|
except AttributeError:
|
||||||
|
print(f"module={args.module}")
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
|
@ -39,16 +39,17 @@ def read_app_info(lines):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"appname",
|
"appname,extra",
|
||||||
(
|
(
|
||||||
"fake.server.app",
|
("fake.server.app", None),
|
||||||
"fake.server:app",
|
("fake.server:create_app", "--factory"),
|
||||||
"fake.server:create_app()",
|
("fake.server.create_app()", None),
|
||||||
"fake.server.create_app()",
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_server_run(appname):
|
def test_server_run(appname, extra):
|
||||||
command = ["sanic", appname]
|
command = ["sanic", appname]
|
||||||
|
if extra:
|
||||||
|
command.append(extra)
|
||||||
out, err, exitcode = capture(command)
|
out, err, exitcode = capture(command)
|
||||||
lines = out.split(b"\n")
|
lines = out.split(b"\n")
|
||||||
firstline = lines[starting_line(lines) + 1]
|
firstline = lines[starting_line(lines) + 1]
|
||||||
|
@ -57,10 +58,37 @@ def test_server_run(appname):
|
||||||
assert firstline == b"Goin' Fast @ http://127.0.0.1:8000"
|
assert firstline == b"Goin' Fast @ http://127.0.0.1:8000"
|
||||||
|
|
||||||
|
|
||||||
def test_error_with_function_as_instance_without_factory_arg():
|
def test_server_run_factory_with_args():
|
||||||
command = ["sanic", "fake.factory.run"]
|
command = [
|
||||||
|
"sanic",
|
||||||
|
"fake.server.create_app_with_args",
|
||||||
|
"--factory",
|
||||||
|
]
|
||||||
out, err, exitcode = capture(command)
|
out, err, exitcode = capture(command)
|
||||||
assert b"try: \nsanic fake.factory.run --factory" in err
|
lines = out.split(b"\n")
|
||||||
|
|
||||||
|
assert exitcode != 1, lines
|
||||||
|
assert b"module=fake.server.create_app_with_args" in lines
|
||||||
|
|
||||||
|
|
||||||
|
def test_server_run_factory_with_args_arbitrary():
|
||||||
|
command = [
|
||||||
|
"sanic",
|
||||||
|
"fake.server.create_app_with_args",
|
||||||
|
"--factory",
|
||||||
|
"--foo=bar",
|
||||||
|
]
|
||||||
|
out, err, exitcode = capture(command)
|
||||||
|
lines = out.split(b"\n")
|
||||||
|
|
||||||
|
assert exitcode != 1, lines
|
||||||
|
assert b"foo=bar" in lines
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_with_function_as_instance_without_factory_arg():
|
||||||
|
command = ["sanic", "fake.server.create_app"]
|
||||||
|
out, err, exitcode = capture(command)
|
||||||
|
assert b"try: \nsanic fake.server.create_app --factory" in err
|
||||||
assert exitcode != 1
|
assert exitcode != 1
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user