Skip to content

Commit 8fa76a3

Browse files
committed
refactor[core]: improve code formatting for health check imports and assertions in tests
1 parent a504861 commit 8fa76a3

File tree

6 files changed

+167
-159
lines changed

6 files changed

+167
-159
lines changed

src/golf/core/builder.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,12 @@ def _generate_server(self) -> None:
572572

573573
# Add health check imports if enabled
574574
if self.settings.health_check_enabled:
575-
imports.extend([
576-
"from starlette.requests import Request",
577-
"from starlette.responses import PlainTextResponse"
578-
])
575+
imports.extend(
576+
[
577+
"from starlette.requests import Request",
578+
"from starlette.responses import PlainTextResponse",
579+
]
580+
)
579581

580582
# Get transport-specific configuration
581583
transport_config = self._get_transport_config(self.settings.transport)
@@ -904,7 +906,9 @@ def _generate_server(self) -> None:
904906
if self.settings.health_check_enabled:
905907
health_check_code = [
906908
"# Add health check route",
907-
"@mcp.custom_route('" + self.settings.health_check_path + "', methods=[\"GET\"])",
909+
"@mcp.custom_route('"
910+
+ self.settings.health_check_path
911+
+ '\', methods=["GET"])',
908912
"async def health_check(request: Request) -> PlainTextResponse:",
909913
' """Health check endpoint for Kubernetes and load balancers."""',
910914
f' return PlainTextResponse("{self.settings.health_check_response}")',

src/golf/core/config.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ class Settings(BaseSettings):
106106
health_check_enabled: bool = Field(
107107
False, description="Enable health check endpoint"
108108
)
109-
health_check_path: str = Field(
110-
"/health", description="Health check endpoint path"
111-
)
112-
health_check_response: str = Field(
113-
"OK", description="Health check response text"
114-
)
109+
health_check_path: str = Field("/health", description="Health check endpoint path")
110+
health_check_response: str = Field("OK", description="Health check response text")
115111

116112

117113
def find_config_path(start_path: Path | None = None) -> Path | None:

tests/commands/test_init.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,19 @@ def test_api_key_template_compatibility_with_health_check(
9191
# Check that we can add health check configuration
9292
config_file = project_dir / "golf.json"
9393
config = json.loads(config_file.read_text())
94-
94+
9595
# Add health check configuration
96-
config.update({
97-
"health_check_enabled": True,
98-
"health_check_path": "/status",
99-
"health_check_response": "API Ready"
100-
})
101-
96+
config.update(
97+
{
98+
"health_check_enabled": True,
99+
"health_check_path": "/status",
100+
"health_check_response": "API Ready",
101+
}
102+
)
103+
102104
# Should be able to write back without issues
103105
config_file.write_text(json.dumps(config, indent=2))
104-
106+
105107
# Verify it can be read back
106108
updated_config = json.loads(config_file.read_text())
107109
assert updated_config["health_check_enabled"] is True

tests/core/test_builder.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test_generates_health_check_when_enabled(
280280
"name": "HealthProject",
281281
"health_check_enabled": True,
282282
"health_check_path": "/health",
283-
"health_check_response": "OK"
283+
"health_check_response": "OK",
284284
}
285285
config_file.write_text(json.dumps(config))
286286

@@ -321,7 +321,7 @@ def simple_tool() -> Output:
321321
assert "from starlette.responses import PlainTextResponse" in server_code
322322

323323
# Should contain health check route definition
324-
assert "@mcp.custom_route(\"/health\", methods=[\"GET\"])" in server_code
324+
assert '@mcp.custom_route("/health", methods=["GET"])' in server_code
325325
assert (
326326
"async def health_check(request: Request) -> PlainTextResponse:"
327327
in server_code
@@ -338,7 +338,7 @@ def test_health_check_with_custom_config(
338338
"name": "CustomHealthProject",
339339
"health_check_enabled": True,
340340
"health_check_path": "/status",
341-
"health_check_response": "Service is running"
341+
"health_check_response": "Service is running",
342342
}
343343
config_file.write_text(json.dumps(config))
344344

@@ -371,7 +371,7 @@ def simple_tool() -> Output:
371371
server_code = server_file.read_text()
372372

373373
# Should use custom path and response
374-
assert "@mcp.custom_route(\"/status\", methods=[\"GET\"])" in server_code
374+
assert '@mcp.custom_route("/status", methods=["GET"])' in server_code
375375
assert 'return PlainTextResponse("Service is running")' in server_code
376376

377377
def test_no_health_check_when_disabled(
@@ -380,10 +380,7 @@ def test_no_health_check_when_disabled(
380380
"""Test that health check route is not generated when disabled."""
381381
# Ensure health check is disabled (default)
382382
config_file = sample_project / "golf.json"
383-
config = {
384-
"name": "NoHealthProject",
385-
"health_check_enabled": False
386-
}
383+
config = {"name": "NoHealthProject", "health_check_enabled": False}
387384
config_file.write_text(json.dumps(config))
388385

389386
# Create a simple tool
@@ -426,18 +423,15 @@ def test_health_check_without_starlette_imports_when_disabled(
426423
# Create a minimal project without any auth or other features
427424
project_dir = temp_dir / "minimal_project"
428425
project_dir.mkdir()
429-
426+
430427
# Create minimal golf.json with only health check disabled
431428
config_file = project_dir / "golf.json"
432-
config = {
433-
"name": "MinimalProject",
434-
"health_check_enabled": False
435-
}
429+
config = {"name": "MinimalProject", "health_check_enabled": False}
436430
config_file.write_text(json.dumps(config))
437-
431+
438432
# Create minimal tool structure
439433
(project_dir / "tools").mkdir()
440-
(project_dir / "resources").mkdir()
434+
(project_dir / "resources").mkdir()
441435
(project_dir / "prompts").mkdir()
442436

443437
# Create a simple tool
@@ -470,8 +464,7 @@ def simple_tool() -> Output:
470464

471465
# Most importantly, no health check route should be generated
472466
assert (
473-
"@mcp.custom_route" not in server_code
474-
or "health_check" not in server_code
467+
"@mcp.custom_route" not in server_code or "health_check" not in server_code
475468
)
476469
assert "async def health_check" not in server_code
477470

@@ -480,10 +473,7 @@ def test_health_check_docstring_generation(
480473
) -> None:
481474
"""Test that health check function has proper docstring."""
482475
config_file = sample_project / "golf.json"
483-
config = {
484-
"name": "DocstringProject",
485-
"health_check_enabled": True
486-
}
476+
config = {"name": "DocstringProject", "health_check_enabled": True}
487477
config_file.write_text(json.dumps(config))
488478

489479
# Create a simple tool
@@ -515,20 +505,25 @@ def simple_tool() -> Output:
515505
server_code = server_file.read_text()
516506

517507
# Should include descriptive docstring
518-
assert '"""Health check endpoint for Kubernetes and load balancers."""' in server_code
508+
assert (
509+
'"""Health check endpoint for Kubernetes and load balancers."""'
510+
in server_code
511+
)
519512

520513

521514
class TestHealthCheckEdgeCases:
522515
"""Test edge cases and error conditions for health check generation."""
523516

524-
def test_health_check_with_empty_response(self, sample_project: Path, temp_dir: Path) -> None:
517+
def test_health_check_with_empty_response(
518+
self, sample_project: Path, temp_dir: Path
519+
) -> None:
525520
"""Test health check with empty response string."""
526521
config_file = sample_project / "golf.json"
527522
config = {
528523
"name": "EmptyResponseProject",
529524
"health_check_enabled": True,
530525
"health_check_path": "/health",
531-
"health_check_response": ""
526+
"health_check_response": "",
532527
}
533528
config_file.write_text(json.dumps(config))
534529

@@ -563,14 +558,16 @@ def simple_tool() -> Output:
563558
# Should handle empty string gracefully
564559
assert 'return PlainTextResponse("")' in server_code
565560

566-
def test_health_check_path_sanitization(self, sample_project: Path, temp_dir: Path) -> None:
561+
def test_health_check_path_sanitization(
562+
self, sample_project: Path, temp_dir: Path
563+
) -> None:
567564
"""Test that health check paths are properly handled in generated code."""
568565
config_file = sample_project / "golf.json"
569566
config = {
570567
"name": "PathSanitizationProject",
571568
"health_check_enabled": True,
572569
"health_check_path": "/api/v1/health-check",
573-
"health_check_response": "All systems operational"
570+
"health_check_response": "All systems operational",
574571
}
575572
config_file.write_text(json.dumps(config))
576573

@@ -603,23 +600,22 @@ def simple_tool() -> Output:
603600
server_code = server_file.read_text()
604601

605602
# Should properly handle complex paths
606-
assert "@mcp.custom_route(\"/api/v1/health-check\", methods=[\"GET\"])" in server_code
603+
assert (
604+
'@mcp.custom_route("/api/v1/health-check", methods=["GET"])' in server_code
605+
)
607606
assert 'return PlainTextResponse("All systems operational")' in server_code
608607

609608
def test_health_check_imports_only_when_needed(self, temp_dir: Path) -> None:
610609
"""Test that health check route is only generated when explicitly enabled."""
611610
# Create a minimal project without any auth or other features
612611
project_dir = temp_dir / "minimal_project"
613612
project_dir.mkdir()
614-
613+
615614
# Create minimal golf.json with health check disabled
616615
config_file = project_dir / "golf.json"
617-
config = {
618-
"name": "MinimalProject",
619-
"health_check_enabled": False
620-
}
616+
config = {"name": "MinimalProject", "health_check_enabled": False}
621617
config_file.write_text(json.dumps(config))
622-
618+
623619
# Create minimal tool structure
624620
(project_dir / "tools").mkdir()
625621
(project_dir / "resources").mkdir()
@@ -655,7 +651,7 @@ def simple_tool() -> Output:
655651

656652
# Should not have health check route when disabled
657653
assert "async def health_check" not in server_code
658-
654+
659655
# Now test with health check enabled
660656
config["health_check_enabled"] = True
661657
config["health_check_path"] = "/health"
@@ -664,6 +660,7 @@ def simple_tool() -> Output:
664660

665661
# Clean and regenerate
666662
import shutil
663+
667664
shutil.rmtree(output_dir)
668665

669666
settings = load_settings(project_dir)

tests/core/test_config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_health_check_configuration_from_json(self, temp_dir: Path) -> None:
111111
"name": "HealthProject",
112112
"health_check_enabled": True,
113113
"health_check_path": "/status",
114-
"health_check_response": "Service is healthy"
114+
"health_check_response": "Service is healthy",
115115
}
116116

117117
config_file = temp_dir / "golf.json"
@@ -124,10 +124,7 @@ def test_health_check_configuration_from_json(self, temp_dir: Path) -> None:
124124

125125
def test_health_check_partial_configuration(self, temp_dir: Path) -> None:
126126
"""Test that partial health check configuration uses defaults for missing values."""
127-
config = {
128-
"name": "PartialHealthProject",
129-
"health_check_enabled": True
130-
}
127+
config = {"name": "PartialHealthProject", "health_check_enabled": True}
131128

132129
config_file = temp_dir / "golf.json"
133130
config_file.write_text(json.dumps(config))

0 commit comments

Comments
 (0)