feat: add daemon command + dummy server setup
All checks were successful
/ Static Analysis (push) Successful in 56s
/ Tests (push) Successful in 45s

This commit is contained in:
Marc 2024-04-13 01:28:48 -04:00
parent be56f287ea
commit 72200752d6
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 59 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import json
import pathlib import pathlib
import click import click
import uvicorn
from spud.base import Configuration from spud.base import Configuration
@ -65,8 +66,22 @@ def print_config(context):
click.echo(config.model_dump_json(indent=2)) 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(init)
cli.add_command(print_config) cli.add_command(print_config)
cli.add_command(daemon)
if __name__ == "__main__": if __name__ == "__main__":
cli(None, None) cli(None, None)

9
spud/daemon.py Normal file
View file

@ -0,0 +1,9 @@
import fastapi
app = fastapi.FastAPI()
@app.get("/")
def alive():
"""Live check."""
return 200

View file

@ -1,4 +1,8 @@
import pathlib import pathlib
from unittest.mock import Mock
import pytest
import uvicorn
def test_init_raises_if_config_file_exists_default_path(invoke_cli, tmpdir): 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 ( assert (
str(result.exception) == f"Configuration file not found at {str(config_file)}." 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)

15
tests/test_daemon.py Normal file
View file

@ -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