2022-07-15 18:12:40 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2022-11-22 19:04:29 +00:00
|
|
|
from tests_examples_app.public import _PATH_EXAMPLES
|
|
|
|
|
2022-07-15 18:12:40 +00:00
|
|
|
from lightning_app.testing.testing import application_testing
|
|
|
|
from lightning_app.utilities.load_app import _patch_sys_argv
|
|
|
|
|
|
|
|
|
|
|
|
def test_app_argparse_example():
|
|
|
|
original_argv = sys.argv
|
|
|
|
|
|
|
|
command_line = [
|
2022-11-22 19:04:29 +00:00
|
|
|
os.path.join(_PATH_EXAMPLES, "app_argparse", "app.py"),
|
2022-07-15 18:12:40 +00:00
|
|
|
"--app_args",
|
|
|
|
"--use_gpu",
|
|
|
|
"--without-server",
|
|
|
|
]
|
|
|
|
result = application_testing(command_line=command_line)
|
|
|
|
assert result.exit_code == 0, result.__dict__
|
|
|
|
assert sys.argv == original_argv
|
|
|
|
|
|
|
|
|
|
|
|
def test_patch_sys_argv():
|
|
|
|
original_argv = sys.argv
|
|
|
|
|
|
|
|
sys.argv = expected = ["lightning", "run", "app", "app.py"]
|
|
|
|
with _patch_sys_argv():
|
|
|
|
assert sys.argv == ["app.py"]
|
|
|
|
|
|
|
|
assert sys.argv == expected
|
|
|
|
|
|
|
|
sys.argv = expected = ["lightning", "run", "app", "app.py", "--without-server", "--env", "name=something"]
|
|
|
|
with _patch_sys_argv():
|
|
|
|
assert sys.argv == ["app.py"]
|
|
|
|
|
|
|
|
assert sys.argv == expected
|
|
|
|
|
|
|
|
sys.argv = expected = ["lightning", "run", "app", "app.py", "--app_args"]
|
|
|
|
with _patch_sys_argv():
|
|
|
|
assert sys.argv == ["app.py"]
|
|
|
|
|
|
|
|
assert sys.argv == expected
|
|
|
|
|
|
|
|
sys.argv = expected = ["lightning", "run", "app", "app.py", "--app_args", "--env", "name=something"]
|
|
|
|
with _patch_sys_argv():
|
|
|
|
assert sys.argv == ["app.py"]
|
|
|
|
|
|
|
|
assert sys.argv == expected
|
|
|
|
|
|
|
|
sys.argv = expected = [
|
|
|
|
"lightning",
|
|
|
|
"run",
|
|
|
|
"app",
|
|
|
|
"app.py",
|
|
|
|
"--without-server",
|
|
|
|
"--app_args",
|
|
|
|
"--use_gpu",
|
|
|
|
"--name=hello",
|
|
|
|
"--env",
|
|
|
|
"name=something",
|
|
|
|
]
|
|
|
|
with _patch_sys_argv():
|
|
|
|
assert sys.argv == ["app.py", "--use_gpu", "--name=hello"]
|
|
|
|
|
|
|
|
assert sys.argv == expected
|
|
|
|
|
|
|
|
sys.argv = original_argv
|