Changes to CLI (#2401)
Co-authored-by: Callum Fleming <howzitcal@zohomail.com> Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
parent
2a8e91052f
commit
44b108b564
|
@ -113,6 +113,14 @@ Or, a path to a directory to run as a simple HTTP server:
|
||||||
delimiter = ":" if ":" in self.args.module else "."
|
delimiter = ":" if ":" in self.args.module else "."
|
||||||
module_name, app_name = self.args.module.rsplit(delimiter, 1)
|
module_name, app_name = self.args.module.rsplit(delimiter, 1)
|
||||||
|
|
||||||
|
if module_name == "" and os.path.isdir(self.args.module):
|
||||||
|
raise ValueError(
|
||||||
|
"App not found.\n"
|
||||||
|
" Please use --simple if you are passing a "
|
||||||
|
"directory to sanic.\n"
|
||||||
|
f" eg. sanic {self.args.module} --simple"
|
||||||
|
)
|
||||||
|
|
||||||
if app_name.endswith("()"):
|
if app_name.endswith("()"):
|
||||||
self.args.factory = True
|
self.args.factory = True
|
||||||
app_name = app_name[:-2]
|
app_name = app_name[:-2]
|
||||||
|
@ -125,9 +133,18 @@ Or, a path to a directory to run as a simple HTTP server:
|
||||||
app_type_name = type(app).__name__
|
app_type_name = type(app).__name__
|
||||||
|
|
||||||
if not isinstance(app, Sanic):
|
if not isinstance(app, Sanic):
|
||||||
|
if callable(app):
|
||||||
|
solution = f"sanic {self.args.module} --factory"
|
||||||
|
raise ValueError(
|
||||||
|
"Module is not a Sanic app, it is a"
|
||||||
|
f"{app_type_name}\n"
|
||||||
|
" If this callable returns a"
|
||||||
|
f"Sanic instance try: \n{solution}"
|
||||||
|
)
|
||||||
|
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Module is not a Sanic app, it is a {app_type_name}\n"
|
f"Module is not a Sanic app, it is a {app_type_name}\n"
|
||||||
f" Perhaps you meant {self.args.module}.app?"
|
f" Perhaps you meant {self.args.module}:app?"
|
||||||
)
|
)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
if module_name.startswith(e.name):
|
if module_name.startswith(e.name):
|
||||||
|
|
6
tests/fake/factory.py
Normal file
6
tests/fake/factory.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from sanic import Sanic
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
app = Sanic("FactoryTest")
|
||||||
|
return app
|
|
@ -57,6 +57,22 @@ 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():
|
||||||
|
command = ["sanic", "fake.factory.run"]
|
||||||
|
out, err, exitcode = capture(command)
|
||||||
|
assert b"try: \nsanic fake.factory.run --factory" in err
|
||||||
|
assert exitcode != 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_with_path_as_instance_without_simple_arg():
|
||||||
|
command = ["sanic", "./fake/"]
|
||||||
|
out, err, exitcode = capture(command)
|
||||||
|
assert (
|
||||||
|
b"Please use --simple if you are passing a directory to sanic." in err
|
||||||
|
)
|
||||||
|
assert exitcode != 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"cmd",
|
"cmd",
|
||||||
(
|
(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user