Skip to content
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 22, 2024
commit c93aca70a411115a294b07ed06a7a1a10deb560b
14 changes: 9 additions & 5 deletions src/pyscript/plugins/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def end_headers(self):
self.send_header("Cross-Origin-Resource-Policy", "cross-origin")
self.send_header("Cache-Control", "no-cache, must-revalidate")
SimpleHTTPRequestHandler.end_headers(self)

def do_GET(self):
# intercept accesses to nonexistent files; replace them with the default file
# this is to service SPA use cases (see Github Issue #132)
if default_file:
path = Path(self.translate_path(self.path))
if not path.exists():
self.path = f'/{default_file}'
self.path = f"/{default_file}"

return super().do_GET()

return FolderBasedHTTPRequestHandler
Expand Down Expand Up @@ -87,7 +87,9 @@ def start_server(path: Path, show: bool, port: int, default_file: Path = None):
socketserver.TCPServer.allow_reuse_address = True

app_folder, filename = split_path_and_filename(path)
CustomHTTPRequestHandler = get_folder_based_http_request_handler(app_folder, default_file=default_file)
CustomHTTPRequestHandler = get_folder_based_http_request_handler(
app_folder, default_file=default_file
)

# Start the server within a context manager to make sure we clean up after
with socketserver.TCPServer(("", port), CustomHTTPRequestHandler) as httpd:
Expand Down Expand Up @@ -121,7 +123,9 @@ def run(
),
view: bool = typer.Option(True, help="Open the app in web browser."),
port: int = typer.Option(8000, help="The port that the app will run on."),
default_file: Path = typer.Option(None, help="A default file to serve when a nonexistent file is accessed."),
default_file: Path = typer.Option(
None, help="A default file to serve when a nonexistent file is accessed."
),
):
"""
Creates a local server to run the app on the path and port specified.
Expand Down
84 changes: 70 additions & 14 deletions tests/test_run_cli_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,80 @@ def test_run_server_with_no_view_flag(
(("--no-view",), (Path("."), False, 8000), {"default_file": None}),
((BASEPATH,), (Path(BASEPATH), True, 8000), {"default_file": None}),
(("--port=8001",), (Path("."), True, 8001), {"default_file": None}),
(("--no-view", "--port=8001"), (Path("."), False, 8001), {"default_file": None}),
((BASEPATH, "--no-view"), (Path(BASEPATH), False, 8000), {"default_file": None}),
((BASEPATH, "--port=8001"), (Path(BASEPATH), True, 8001), {"default_file": None}),
((BASEPATH, "--no-view", "--port=8001"), (Path(BASEPATH), False, 8001), {"default_file": None}),
((BASEPATH, "--port=8001"), (Path(BASEPATH), True, 8001), {"default_file": None}),
(("--no-view", "--default-file=index.html"), (Path("."), False, 8000), {"default_file": Path("index.html")}),
((BASEPATH, "--default-file=index.html"), (Path(BASEPATH), True, 8000), {"default_file": Path("index.html")}),
(("--port=8001", "--default-file=index.html"), (Path("."), True, 8001), {"default_file": Path("index.html")}),
(("--no-view", "--port=8001", "--default-file=index.html"), (Path("."), False, 8001), {"default_file": Path("index.html")}),
((BASEPATH, "--no-view", "--default-file=index.html"), (Path(BASEPATH), False, 8000), {"default_file": Path("index.html")}),
((BASEPATH, "--port=8001", "--default-file=index.html"), (Path(BASEPATH), True, 8001), {"default_file": Path("index.html")}),
((BASEPATH, "--no-view", "--port=8001", "--default-file=index.html"), (Path(BASEPATH), False, 8001), {"default_file": Path("index.html")}),
((BASEPATH, "--port=8001", "--default-file=index.html"), (Path(BASEPATH), True, 8001), {"default_file": Path("index.html")}),
(
("--no-view", "--port=8001"),
(Path("."), False, 8001),
{"default_file": None},
),
(
(BASEPATH, "--no-view"),
(Path(BASEPATH), False, 8000),
{"default_file": None},
),
(
(BASEPATH, "--port=8001"),
(Path(BASEPATH), True, 8001),
{"default_file": None},
),
(
(BASEPATH, "--no-view", "--port=8001"),
(Path(BASEPATH), False, 8001),
{"default_file": None},
),
(
(BASEPATH, "--port=8001"),
(Path(BASEPATH), True, 8001),
{"default_file": None},
),
(
("--no-view", "--default-file=index.html"),
(Path("."), False, 8000),
{"default_file": Path("index.html")},
),
(
(BASEPATH, "--default-file=index.html"),
(Path(BASEPATH), True, 8000),
{"default_file": Path("index.html")},
),
(
("--port=8001", "--default-file=index.html"),
(Path("."), True, 8001),
{"default_file": Path("index.html")},
),
(
("--no-view", "--port=8001", "--default-file=index.html"),
(Path("."), False, 8001),
{"default_file": Path("index.html")},
),
(
(BASEPATH, "--no-view", "--default-file=index.html"),
(Path(BASEPATH), False, 8000),
{"default_file": Path("index.html")},
),
(
(BASEPATH, "--port=8001", "--default-file=index.html"),
(Path(BASEPATH), True, 8001),
{"default_file": Path("index.html")},
),
(
(BASEPATH, "--no-view", "--port=8001", "--default-file=index.html"),
(Path(BASEPATH), False, 8001),
{"default_file": Path("index.html")},
),
(
(BASEPATH, "--port=8001", "--default-file=index.html"),
(Path(BASEPATH), True, 8001),
{"default_file": Path("index.html")},
),
],
)
@mock.patch("pyscript.plugins.run.start_server")
def test_run_server_with_valid_combinations(
start_server_mock, invoke_cli: CLIInvoker, run_args, expected_posargs, expected_kwargs # noqa: F811
start_server_mock,
invoke_cli: CLIInvoker,
run_args,
expected_posargs,
expected_kwargs, # noqa: F811
):
"""
Test that when run is called without arguments the command runs with the
Expand Down