|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Tests for the mcpgateway CLI module (cli.py). |
| 3 | +
|
| 4 | +Copyright 2025 |
| 5 | +SPDX-License-Identifier: Apache-2.0 |
| 6 | +Authors: Mihai Criveti |
| 7 | +
|
| 8 | +This module contains tests for the tiny "Uvicorn wrapper" found in |
| 9 | +mcpgateway.cli. It exercises **every** decision point: |
| 10 | +
|
| 11 | +* `_needs_app` - missing vs. present app path |
| 12 | +* `_insert_defaults` - all permutations of host/port injection |
| 13 | +* `main()` - early-return on --version / -V **and** the happy path that |
| 14 | + actually calls Uvicorn with a patched ``sys.argv``. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import importlib |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | +from typing import List, Dict, Any |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +import mcpgateway.cli as cli |
| 27 | + |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# helpers / fixtures |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(autouse=True) |
| 35 | +def _restore_sys_argv() -> None: |
| 36 | + """Keep the global *sys.argv* pristine between tests.""" |
| 37 | + original = sys.argv.copy() |
| 38 | + yield |
| 39 | + sys.argv[:] = original |
| 40 | + |
| 41 | + |
| 42 | +def _capture_uvicorn_main(monkeypatch) -> Dict[str, Any]: |
| 43 | + """Monkey-patch *uvicorn.main* and record the argv it sees.""" |
| 44 | + captured: Dict[str, Any] = {} |
| 45 | + |
| 46 | + def _fake_main() -> None: |
| 47 | + # Copy because tests mutate sys.argv afterwards. |
| 48 | + captured["argv"] = sys.argv.copy() |
| 49 | + |
| 50 | + monkeypatch.setattr(cli.uvicorn, "main", _fake_main) |
| 51 | + return captured |
| 52 | + |
| 53 | + |
| 54 | +# --------------------------------------------------------------------------- |
| 55 | +# _needs_app |
| 56 | +# --------------------------------------------------------------------------- |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + ("argv", "missing"), |
| 61 | + [ |
| 62 | + ([], True), # no positional args at all |
| 63 | + (["--reload"], True), # first token is an option |
| 64 | + (["somepkg.app:app"], False), # explicit app path present |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_needs_app_detection(argv: List[str], missing: bool) -> None: |
| 68 | + assert cli._needs_app(argv) is missing |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# _insert_defaults |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | +def test_insert_defaults_injects_everything() -> None: |
| 77 | + """No app/host/port supplied ⇒ inject all three.""" |
| 78 | + raw = ["--reload"] |
| 79 | + out = cli._insert_defaults(raw) |
| 80 | + |
| 81 | + # original list must remain untouched (function copies) |
| 82 | + assert raw == ["--reload"] |
| 83 | + |
| 84 | + assert out[0] == cli.DEFAULT_APP |
| 85 | + assert "--host" in out and cli.DEFAULT_HOST in out |
| 86 | + assert "--port" in out and str(cli.DEFAULT_PORT) in out |
| 87 | + |
| 88 | + |
| 89 | +def test_insert_defaults_respects_explicit_host(monkeypatch) -> None: |
| 90 | + """Host given, port missing ⇒ only port default injected.""" |
| 91 | + raw = ["myapp:app", "--host", "0.0.0.0"] |
| 92 | + out = cli._insert_defaults(raw) |
| 93 | + |
| 94 | + # our app path must stay first |
| 95 | + assert out[0] == "myapp:app" |
| 96 | + # host left untouched, port injected |
| 97 | + assert out.count("--host") == 1 |
| 98 | + assert "--port" in out and str(cli.DEFAULT_PORT) in out |
| 99 | + |
| 100 | + |
| 101 | +def test_insert_defaults_skips_for_uds() -> None: |
| 102 | + """When --uds is present no host/port defaults are added.""" |
| 103 | + raw = ["--uds", "/tmp/app.sock"] |
| 104 | + out = cli._insert_defaults(raw) |
| 105 | + |
| 106 | + assert "--host" not in out |
| 107 | + assert "--port" not in out |
| 108 | + |
| 109 | + |
| 110 | +# --------------------------------------------------------------------------- |
| 111 | +# main() - early *--version* short-circuit |
| 112 | +# --------------------------------------------------------------------------- |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize("flag", ["--version", "-V"]) |
| 116 | +def test_main_prints_version_and_exits(flag: str, capsys, monkeypatch) -> None: |
| 117 | + monkeypatch.setattr(sys, "argv", ["mcpgateway", flag]) |
| 118 | + # If Uvicorn accidentally ran we'd hang the tests - make sure it can't. |
| 119 | + monkeypatch.setattr(cli.uvicorn, "main", lambda: (_ for _ in ()).throw(RuntimeError("should not be called"))) |
| 120 | + cli.main() |
| 121 | + |
| 122 | + out, err = capsys.readouterr() |
| 123 | + assert out.strip() == f"mcpgateway {cli.__version__}" |
| 124 | + assert err == "" |
| 125 | + |
| 126 | + |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | +# main() - normal execution path (calls Uvicorn) |
| 129 | +# --------------------------------------------------------------------------- |
| 130 | + |
| 131 | + |
| 132 | +def test_main_invokes_uvicorn_with_patched_argv(monkeypatch) -> None: |
| 133 | + """Ensure *main()* rewrites argv then delegates to Uvicorn.""" |
| 134 | + captured = _capture_uvicorn_main(monkeypatch) |
| 135 | + monkeypatch.setattr(sys, "argv", ["mcpgateway", "--reload"]) |
| 136 | + |
| 137 | + cli.main() |
| 138 | + |
| 139 | + # The fake Uvicorn ran exactly once |
| 140 | + assert "argv" in captured |
| 141 | + patched = captured["argv"] |
| 142 | + |
| 143 | + # Position 0 must be the console-script name |
| 144 | + assert patched[0] == "mcpgateway" |
| 145 | + # The injected app path must follow |
| 146 | + assert patched[1] == cli.DEFAULT_APP |
| 147 | + # Original flag preserved |
| 148 | + assert "--reload" in patched |
| 149 | + # Defaults present |
| 150 | + assert "--host" in patched and cli.DEFAULT_HOST in patched |
| 151 | + assert "--port" in patched and str(cli.DEFAULT_PORT) in patched |
0 commit comments