Skip to content

Commit

Permalink
Allow arbitrary args in factory mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 23, 2022
1 parent 79d3cb4 commit 5726732
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
7 changes: 7 additions & 0 deletions sanic/cli/app.py
Original file line number Diff line number Diff line change
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
6 changes: 0 additions & 6 deletions tests/fake/factory.py

This file was deleted.

9 changes: 9 additions & 0 deletions tests/fake/server.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 5726732

Please sign in to comment.