From 72200752d60dd966c56487ee51aa769d5d9e83ce Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sat, 13 Apr 2024 01:28:48 -0400 Subject: [PATCH] feat: add daemon command + dummy server setup --- spud/cli.py | 15 +++++++++++++++ spud/daemon.py | 9 +++++++++ tests/test_cli.py | 20 ++++++++++++++++++++ tests/test_daemon.py | 15 +++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 spud/daemon.py create mode 100644 tests/test_daemon.py diff --git a/spud/cli.py b/spud/cli.py index c08e70b..4cdd78c 100644 --- a/spud/cli.py +++ b/spud/cli.py @@ -8,6 +8,7 @@ import json import pathlib import click +import uvicorn from spud.base import Configuration @@ -65,8 +66,22 @@ def print_config(context): click.echo(config.model_dump_json(indent=2)) +@click.command() +@click.option( + "--reload", + type=bool, + default=False, + is_flag=True, + help="Hot reloads on code updates.", +) +def daemon(reload): + """Starts the daemon process.""" + uvicorn.run("spud.daemon:app", reload=reload) + + cli.add_command(init) cli.add_command(print_config) +cli.add_command(daemon) if __name__ == "__main__": cli(None, None) diff --git a/spud/daemon.py b/spud/daemon.py new file mode 100644 index 0000000..00f0b36 --- /dev/null +++ b/spud/daemon.py @@ -0,0 +1,9 @@ +import fastapi + +app = fastapi.FastAPI() + + +@app.get("/") +def alive(): + """Live check.""" + return 200 diff --git a/tests/test_cli.py b/tests/test_cli.py index cf525a9..5fe368a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,8 @@ import pathlib +from unittest.mock import Mock + +import pytest +import uvicorn def test_init_raises_if_config_file_exists_default_path(invoke_cli, tmpdir): @@ -50,3 +54,19 @@ def test_print_config_raises_if_no_config_file_custom_path(invoke_cli, tmpdir): assert ( str(result.exception) == f"Configuration file not found at {str(config_file)}." ) + + +@pytest.mark.parametrize("reload_flag", [True, False]) +def test_daemon_starts_server_with_reload_option(invoke_cli, monkeypatch, reload_flag): + run_mock = Mock() + monkeypatch.setattr(uvicorn, "run", run_mock) + + args = ["daemon"] + + if reload_flag: + args += ["--reload"] + result = invoke_cli(args) + + assert result.exit_code == 0 + + run_mock.assert_called_once_with("spud.daemon:app", reload=reload_flag) diff --git a/tests/test_daemon.py b/tests/test_daemon.py new file mode 100644 index 0000000..5dd8f60 --- /dev/null +++ b/tests/test_daemon.py @@ -0,0 +1,15 @@ +import pytest +from fastapi.testclient import TestClient + +import spud.daemon + + +@pytest.fixture(name="client") +def client_fixture(): + return TestClient(spud.daemon.app) + + +def test_alive_returns_200(client): + response = client.get("/") + + assert response.status_code == 200