This repository has been archived on 2024-06-02. You can view files and clone it, but cannot push or open issues or pull requests.
spud-py/tests/test_cli.py

90 lines
2.5 KiB
Python
Raw Normal View History

import json
import pathlib
from unittest.mock import Mock
import pytest
import uvicorn
def test_init_raises_if_config_file_exists_default_path(
invoke_cli, tmpdir, sample_config
):
expected_default_path = tmpdir / ".config" / "spud" / "config.json"
pathlib.Path(expected_default_path).parent.mkdir(parents=True)
expected_default_path.write(json.dumps(sample_config))
result = invoke_cli(["init"])
assert result.exit_code == 1
assert (
str(result.exception)
== f"File already exists ({str(expected_default_path)}), cannot initialize."
)
def test_init_raises_if_config_file_exists_custom_path(
invoke_cli, tmpdir, sample_config
):
expected_default_path = tmpdir / "config.json"
expected_default_path.write(json.dumps(sample_config))
result = invoke_cli(["--config", str(expected_default_path), "init"])
assert result.exit_code == 1
assert (
str(result.exception)
== f"File already exists ({str(expected_default_path)}), cannot initialize."
)
def test_print_config_raises_if_no_config_file_default_path(invoke_cli, tmpdir):
expected_default_path = tmpdir / ".config" / "spud" / "config.json"
result = invoke_cli(["print-config"])
assert result.exit_code == 1
assert (
str(result.exception)
== f"Configuration file not found at {str(expected_default_path)}."
)
def test_print_config_raises_if_no_config_file_custom_path(invoke_cli, tmpdir):
config_file = tmpdir / "config.json"
result = invoke_cli(["--config", str(config_file), "print-config"])
assert result.exit_code == 1
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)
# 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