Repository navigation
test(time): characterize serve() through the protocol, per-file coverage #4972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cliffhall
merged 4 commits into
v2/chore/4855-git-coverage
from
v2/chore/4855-time-coverage
Oct 4, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86042c1
test(time): characterize serve() through the protocol, per-file coverage
cliffhall 003618a
docs(testing): describe time's in-process harness and coverage command
cliffhall d8938ed
test(time): mark the tests that pin known bugs
cliffhall d051608
test(time): cite the newly filed issues in KNOWN BUG markers
cliffhall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Coverage output from the per-server coverage command (see README, | ||
| # "Development"); generated locally and never committed. | ||
| coverage.json | ||
| .coverage | ||
| .coverage.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Tests for mcp-server-time's entry points (#4855): `main()` in `__init__.py`, | ||
| # `python -m mcp_server_time` (`__main__.py`), and the `mcp-server-time` | ||
| # console script. | ||
| # | ||
| # `main()` and both `__main__` paths run in-process with `serve` patched out, | ||
| # so coverage measures them and nothing reads real stdin. The console script | ||
| # gets the one thin subprocess smoke test: it is the only way to prove the | ||
| # installed entry point boots and speaks MCP over real stdio. | ||
|
|
||
| import os | ||
| import runpy | ||
| import shutil | ||
| import sys | ||
| from pathlib import Path | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
| from mcp import ClientSession, StdioServerParameters | ||
| from mcp.client.stdio import stdio_client | ||
|
|
||
| import mcp_server_time | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "argv,expected_local_timezone", | ||
| [ | ||
| ([], None), | ||
| (["--local-timezone", "Europe/Paris"], "Europe/Paris"), | ||
| (["--local-timezone=America/Chicago"], "America/Chicago"), | ||
| ], | ||
| ) | ||
| def test_main_passes_local_timezone_to_serve( | ||
| argv: list[str], expected_local_timezone: str | None | ||
| ) -> None: | ||
| serve = AsyncMock() | ||
| with ( | ||
| patch.object(mcp_server_time, "serve", serve), | ||
| patch.object(sys, "argv", ["mcp-server-time", *argv]), | ||
| ): | ||
| mcp_server_time.main() | ||
| serve.assert_awaited_once_with(expected_local_timezone) | ||
|
|
||
|
|
||
| def test_main_help_describes_the_server(capsys: pytest.CaptureFixture[str]) -> None: | ||
| serve = AsyncMock() | ||
| with ( | ||
| patch.object(mcp_server_time, "serve", serve), | ||
| patch.object(sys, "argv", ["mcp-server-time", "--help"]), | ||
| pytest.raises(SystemExit) as exit_info, | ||
| ): | ||
| mcp_server_time.main() | ||
| assert exit_info.value.code == 0 | ||
| out = capsys.readouterr().out | ||
| assert ( | ||
| "give a model the ability to handle time queries and timezone conversions" | ||
| in out | ||
| ) | ||
| assert "--local-timezone" in out | ||
| serve.assert_not_awaited() | ||
|
|
||
|
|
||
| def test_main_rejects_unknown_arguments(capsys: pytest.CaptureFixture[str]) -> None: | ||
| serve = AsyncMock() | ||
| with ( | ||
| patch.object(mcp_server_time, "serve", serve), | ||
| patch.object(sys, "argv", ["mcp-server-time", "--bogus"]), | ||
| pytest.raises(SystemExit) as exit_info, | ||
| ): | ||
| mcp_server_time.main() | ||
| assert exit_info.value.code == 2 | ||
| assert "unrecognized arguments: --bogus" in capsys.readouterr().err | ||
| serve.assert_not_awaited() | ||
|
|
||
|
|
||
| def test_python_dash_m_calls_main() -> None: | ||
| # What `python -m mcp_server_time` executes. | ||
| main = MagicMock() | ||
| with patch.object(mcp_server_time, "main", main): | ||
| runpy.run_module("mcp_server_time", run_name="__main__") | ||
| main.assert_called_once_with() | ||
|
|
||
|
|
||
| def test_init_module_run_as_main_calls_main() -> None: | ||
| # `__init__.py` carries its own `if __name__ == "__main__"` guard. Running | ||
| # it as a module re-executes it in a fresh namespace, which imports | ||
| # `serve` from `.server` again, so the patch goes there. | ||
| serve = AsyncMock() | ||
| with ( | ||
| patch("mcp_server_time.server.serve", serve), | ||
| patch.object(sys, "argv", ["mcp-server-time", "--local-timezone", "UTC"]), | ||
| ): | ||
| runpy.run_module("mcp_server_time.__init__", run_name="__main__") | ||
| serve.assert_awaited_once_with("UTC") | ||
|
|
||
|
|
||
| async def test_console_script_boots_over_stdio() -> None: | ||
| script = shutil.which("mcp-server-time", path=str(Path(sys.executable).parent)) | ||
| assert script is not None, "the console script is not installed in this venv" | ||
| params = StdioServerParameters( | ||
| command=script, | ||
| args=["--local-timezone", "UTC"], | ||
| env={**os.environ}, | ||
| ) | ||
| with open(os.devnull, "w") as errlog: | ||
| async with stdio_client(params, errlog=errlog) as (read, write): | ||
| async with ClientSession(read, write) as session: | ||
| init = await session.initialize() | ||
| tools = await session.list_tools() | ||
| result = await session.call_tool( | ||
| "get_current_time", {"timezone": "UTC"} | ||
| ) | ||
| assert init.model_dump(by_alias=True, mode="json")["serverInfo"]["name"] == ( | ||
| "mcp-time" | ||
| ) | ||
| assert [tool.name for tool in tools.tools] == ["get_current_time", "convert_time"] | ||
| assert result.model_dump(by_alias=True, mode="json")["isError"] is False |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.