Update customise_server to poll the debug flag in the LT server instead of re-reading the value from CLI arguments.

This commit is contained in:
Beth Probert 2026-06-17 12:22:12 +01:00
parent d3c4773884
commit b235de39f6
2 changed files with 15 additions and 9 deletions

View file

@ -72,7 +72,7 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None:
def customise_server( def customise_server(
server: lt.ThingServer, application_config: OFMApplicationData, debug: bool = False server: lt.ThingServer, application_config: OFMApplicationData
) -> None: ) -> None:
"""Customise the server with additional endpoints, debug mode etc.""" """Customise the server with additional endpoints, debug mode etc."""
if DEVELOPER_MODE: if DEVELOPER_MODE:
@ -88,8 +88,9 @@ def customise_server(
add_v2_endpoints(server) add_v2_endpoints(server)
add_static_files(server, application_config.data_folder) add_static_files(server, application_config.data_folder)
# Configure logging to DEBUG if requested in CLI args. # Configure logging to DEBUG if LT server is set up
if debug: # with debug = true
if server.debug:
lt.logs.configure_thing_logger(logging.DEBUG) lt.logs.configure_thing_logger(logging.DEBUG)
# Add an endpoint to get the logs - (directly calling the FastAPI decorator) # Add an endpoint to get the logs - (directly calling the FastAPI decorator)
@ -119,9 +120,8 @@ def serve_from_cli(argv: Optional[list[str]] = None) -> None:
application_config = OFMApplicationData(**lt_config.application_config) application_config = OFMApplicationData(**lt_config.application_config)
configure_logging(application_config.log_folder) configure_logging(application_config.log_folder)
server = lt.ThingServer.from_config(lt_config) server = lt.ThingServer.from_config(lt_config, args.debug)
debug = bool(args.debug) customise_server(server, application_config)
customise_server(server, application_config, debug)
def shutdown_call() -> None: def shutdown_call() -> None:
try: try:

View file

@ -109,14 +109,20 @@ def test_failed_customise(mocker):
# An that it has the error to display # An that it has the error to display
assert str(fallback_app._context.error) == "Can't touch this" assert str(fallback_app._context.error) == "Can't touch this"
def test_debug_mode(mocker): def test_debug_mode(mocker):
"""Test that --debug flag triggers lt.logs.configure_thing_logger.""" """Test that --debug flag triggers lt.logs.configure_thing_logger."""
# Mock the LabThings function that returns the server
# Use a side_effect to map the CLI debug arg to the mock server's debug attribute
def mock_from_config(_, debug_flag):
mock_server = mocker.Mock()
mock_server.debug = debug_flag # This will now be correctly True or False
return mock_server
mocker.patch( mocker.patch(
"openflexure_microscope_server.server.lt.ThingServer.from_config", "openflexure_microscope_server.server.lt.ThingServer.from_config",
return_value=mocker.Mock(), side_effect=mock_from_config,
) )
# Mock customisation dependencies to avoid side effects # Mock customisation dependencies to avoid side effects
mocker.patch.object(ofm_server, "add_v2_endpoints") mocker.patch.object(ofm_server, "add_v2_endpoints")
mocker.patch.object(ofm_server, "add_static_files") mocker.patch.object(ofm_server, "add_static_files")