diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index 3735f312..01028715 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -72,7 +72,7 @@ def set_shutdown_function(shutdown_function: Callable[[], None]) -> None: def customise_server( - server: lt.ThingServer, application_config: OFMApplicationData, debug: bool = False + server: lt.ThingServer, application_config: OFMApplicationData ) -> None: """Customise the server with additional endpoints, debug mode etc.""" if DEVELOPER_MODE: @@ -88,8 +88,9 @@ def customise_server( add_v2_endpoints(server) add_static_files(server, application_config.data_folder) - # Configure logging to DEBUG if requested in CLI args. - if debug: + # Configure logging to DEBUG if LT server is set up + # with debug = true + if server.debug: lt.logs.configure_thing_logger(logging.DEBUG) # 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) configure_logging(application_config.log_folder) - server = lt.ThingServer.from_config(lt_config) - debug = bool(args.debug) - customise_server(server, application_config, debug) + server = lt.ThingServer.from_config(lt_config, args.debug) + customise_server(server, application_config) def shutdown_call() -> None: try: diff --git a/tests/unit_tests/test_server_cli.py b/tests/unit_tests/test_server_cli.py index 4c71a1c3..b7837781 100644 --- a/tests/unit_tests/test_server_cli.py +++ b/tests/unit_tests/test_server_cli.py @@ -109,14 +109,20 @@ def test_failed_customise(mocker): # An that it has the error to display assert str(fallback_app._context.error) == "Can't touch this" - def test_debug_mode(mocker): """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( "openflexure_microscope_server.server.lt.ThingServer.from_config", - return_value=mocker.Mock(), + side_effect=mock_from_config, ) + # Mock customisation dependencies to avoid side effects mocker.patch.object(ofm_server, "add_v2_endpoints") mocker.patch.object(ofm_server, "add_static_files")