Skip to content

Commit

Permalink
Conditionally inject CLI arguments into factory (#2402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 23, 2022
1 parent c9dbc8e commit 0030425
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
12 changes: 11 additions & 1 deletion sanic/cli/app.py
Expand Up @@ -68,6 +68,13 @@ def run(self):
legacy_version = len(sys.argv) == 2 and sys.argv[-1] == "-v"
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._precheck()

Expand Down Expand Up @@ -128,7 +135,10 @@ def _get_app(self):
module = import_module(module_name)
app = getattr(module, app_name, None)
if self.args.factory:
app = app()
try:
app = app(self.args)
except TypeError:
app = app()

app_type_name = type(app).__name__

Expand Down
6 changes: 0 additions & 6 deletions tests/fake/factory.py

This file was deleted.

9 changes: 9 additions & 0 deletions tests/fake/server.py
Expand Up @@ -34,3 +34,12 @@ async def shutdown(app: Sanic, _):

def create_app():
return app


def create_app_with_args(args):
try:
print(f"foo={args.foo}")
except AttributeError:
print(f"module={args.module}")

return app
44 changes: 36 additions & 8 deletions tests/test_cli.py
Expand Up @@ -39,16 +39,17 @@ def read_app_info(lines):


@pytest.mark.parametrize(
"appname",
"appname,extra",
(
"fake.server.app",
"fake.server:app",
"fake.server:create_app()",
"fake.server.create_app()",
("fake.server.app", None),
("fake.server:create_app", "--factory"),
("fake.server.create_app()", None),
),
)
def test_server_run(appname):
def test_server_run(appname, extra):
command = ["sanic", appname]
if extra:
command.append(extra)
out, err, exitcode = capture(command)
lines = out.split(b"\n")
firstline = lines[starting_line(lines) + 1]
Expand All @@ -57,10 +58,37 @@ def test_server_run(appname):
assert firstline == b"Goin' Fast @ http://127.0.0.1:8000"


def test_server_run_factory_with_args():
command = [
"sanic",
"fake.server.create_app_with_args",
"--factory",
]
out, err, exitcode = capture(command)
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.factory.run"]
command = ["sanic", "fake.server.create_app"]
out, err, exitcode = capture(command)
assert b"try: \nsanic fake.factory.run --factory" in err
assert b"try: \nsanic fake.server.create_app --factory" in err
assert exitcode != 1


Expand Down

0 comments on commit 0030425

Please sign in to comment.