2024-04-14 04:27:55 +00:00
|
|
|
import json
|
2024-04-12 05:09:56 +00:00
|
|
|
import pathlib
|
2024-04-13 05:28:48 +00:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import uvicorn
|
2024-04-12 05:09:56 +00:00
|
|
|
|
2024-04-12 05:16:03 +00:00
|
|
|
|
2024-04-14 04:27:55 +00:00
|
|
|
def test_init_raises_if_config_file_exists_default_path(
|
|
|
|
invoke_cli, tmpdir, sample_config
|
|
|
|
):
|
2024-04-12 05:09:56 +00:00
|
|
|
expected_default_path = tmpdir / ".config" / "spud" / "config.json"
|
|
|
|
|
|
|
|
pathlib.Path(expected_default_path).parent.mkdir(parents=True)
|
2024-04-14 04:27:55 +00:00
|
|
|
expected_default_path.write(json.dumps(sample_config))
|
2024-04-12 05:09:56 +00:00
|
|
|
|
|
|
|
result = invoke_cli(["init"])
|
2024-04-12 05:16:03 +00:00
|
|
|
|
2024-04-12 05:09:56 +00:00
|
|
|
assert result.exit_code == 1
|
2024-04-12 05:16:03 +00:00
|
|
|
assert (
|
|
|
|
str(result.exception)
|
|
|
|
== f"File already exists ({str(expected_default_path)}), cannot initialize."
|
|
|
|
)
|
|
|
|
|
2024-04-12 05:09:56 +00:00
|
|
|
|
2024-04-14 04:27:55 +00:00
|
|
|
def test_init_raises_if_config_file_exists_custom_path(
|
|
|
|
invoke_cli, tmpdir, sample_config
|
|
|
|
):
|
2024-04-12 05:09:56 +00:00
|
|
|
expected_default_path = tmpdir / "config.json"
|
|
|
|
|
2024-04-14 04:27:55 +00:00
|
|
|
expected_default_path.write(json.dumps(sample_config))
|
2024-04-12 05:09:56 +00:00
|
|
|
|
|
|
|
result = invoke_cli(["--config", str(expected_default_path), "init"])
|
2024-04-12 05:16:03 +00:00
|
|
|
|
2024-04-12 05:09:56 +00:00
|
|
|
assert result.exit_code == 1
|
2024-04-12 05:16:03 +00:00
|
|
|
assert (
|
|
|
|
str(result.exception)
|
|
|
|
== f"File already exists ({str(expected_default_path)}), cannot initialize."
|
|
|
|
)
|
|
|
|
|
2024-04-12 05:09:56 +00:00
|
|
|
|
2024-04-12 05:16:03 +00:00
|
|
|
def test_print_config_raises_if_no_config_file_default_path(invoke_cli, tmpdir):
|
2024-04-12 05:09:56 +00:00
|
|
|
expected_default_path = tmpdir / ".config" / "spud" / "config.json"
|
|
|
|
result = invoke_cli(["print-config"])
|
|
|
|
|
|
|
|
assert result.exit_code == 1
|
2024-04-12 05:16:03 +00:00
|
|
|
assert (
|
|
|
|
str(result.exception)
|
|
|
|
== f"Configuration file not found at {str(expected_default_path)}."
|
|
|
|
)
|
|
|
|
|
2024-04-12 05:09:56 +00:00
|
|
|
|
2024-04-12 05:16:03 +00:00
|
|
|
def test_print_config_raises_if_no_config_file_custom_path(invoke_cli, tmpdir):
|
2024-04-12 05:09:56 +00:00
|
|
|
config_file = tmpdir / "config.json"
|
|
|
|
|
|
|
|
result = invoke_cli(["--config", str(config_file), "print-config"])
|
|
|
|
|
|
|
|
assert result.exit_code == 1
|
2024-04-12 05:16:03 +00:00
|
|
|
assert (
|
|
|
|
str(result.exception) == f"Configuration file not found at {str(config_file)}."
|
|
|
|
)
|
2024-04-13 05:28:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2024-04-14 04:27:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
# FIXME: Assert based on stdout.
|
|
|
|
def test_status_prints_service_statuses(tmpdir, invoke_cli, httpx_mock, sample_config):
|
|
|
|
httpx_mock.add_response("http://test.url/status", json=[])
|
|
|
|
config_file = tmpdir / "config.json"
|
|
|
|
|
|
|
|
config_file.write(json.dumps(sample_config))
|
|
|
|
|
|
|
|
result = invoke_cli(["--config", str(config_file), "status"])
|
|
|
|
|
|
|
|
assert result.exit_code == 0
|