@@ -432,7 +432,9 @@ def test_user_can_save_valid_data_successfully(self, json_manager_with_schema) -
432432 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
433433 return_value = "/output/directory/output.json" ,
434434 ),
435- patch ("builtins.open" , mock_open ()) as mock_file ,
435+ patch (
436+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
437+ ) as mock_safe_write ,
436438 patch (
437439 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.json_dumps" ,
438440 return_value = '{"name": "Test Output", "count": 42}' ,
@@ -444,7 +446,7 @@ def test_user_can_save_valid_data_successfully(self, json_manager_with_schema) -
444446 # Assert: Data saved successfully
445447 assert error_occurred is False
446448 assert error_message == ""
447- mock_file . assert_called_once_with ( "/output/directory/output.json" , "w" , encoding = "utf-8" , newline = " \n " )
449+ mock_safe_write . assert_called_once ( )
448450 mock_dumps .assert_called_once_with (valid_data , indent = 4 )
449451 # Assert: Internal cache updated immediately (regression test for commit ccc53bb)
450452 assert json_manager_with_schema .data == valid_data
@@ -501,7 +503,10 @@ def test_user_handles_missing_directory_error(self, json_manager_with_schema) ->
501503 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
502504 return_value = "/nonexistent/directory/output.json" ,
503505 ),
504- patch ("builtins.open" , side_effect = FileNotFoundError ),
506+ patch (
507+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
508+ side_effect = FileNotFoundError ,
509+ ),
505510 patch ("ardupilot_methodic_configurator.backend_filesystem_json_with_schema.logging_error" ) as mock_error ,
506511 ):
507512 # Act: User attempts to save to missing directory
@@ -530,7 +535,10 @@ def test_user_handles_permission_denied_error(self, json_manager_with_schema) ->
530535 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
531536 return_value = "/protected/directory/output.json" ,
532537 ),
533- patch ("builtins.open" , side_effect = PermissionError ),
538+ patch (
539+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
540+ side_effect = PermissionError ,
541+ ),
534542 patch ("ardupilot_methodic_configurator.backend_filesystem_json_with_schema.logging_error" ) as mock_error ,
535543 ):
536544 # Act: User attempts to save without permissions
@@ -559,7 +567,10 @@ def test_user_handles_directory_instead_of_file_error(self, json_manager_with_sc
559567 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
560568 return_value = "/test/directory/output.json" ,
561569 ),
562- patch ("builtins.open" , side_effect = IsADirectoryError ),
570+ patch (
571+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
572+ side_effect = IsADirectoryError ,
573+ ),
563574 patch ("ardupilot_methodic_configurator.backend_filesystem_json_with_schema.logging_error" ) as mock_error ,
564575 ):
565576 # Act: User attempts to save to directory path
@@ -589,7 +600,10 @@ def test_user_handles_os_error_gracefully(self, json_manager_with_schema) -> Non
589600 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
590601 return_value = "/test/directory/output.json" ,
591602 ),
592- patch ("builtins.open" , side_effect = os_error ),
603+ patch (
604+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
605+ side_effect = os_error ,
606+ ),
593607 patch ("ardupilot_methodic_configurator.backend_filesystem_json_with_schema.logging_error" ) as mock_error ,
594608 ):
595609 # Act: User encounters OS error
@@ -619,7 +633,6 @@ def test_user_handles_json_serialization_errors(self, json_manager_with_schema)
619633 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
620634 return_value = "/test/directory/output.json" ,
621635 ),
622- patch ("builtins.open" , mock_open ()),
623636 patch (
624637 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.json_dumps" ,
625638 side_effect = TypeError ("Object not serializable" ),
@@ -652,7 +665,6 @@ def test_user_handles_json_value_errors(self, json_manager_with_schema) -> None:
652665 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
653666 return_value = "/test/directory/output.json" ,
654667 ),
655- patch ("builtins.open" , mock_open ()),
656668 patch (
657669 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.json_dumps" ,
658670 side_effect = ValueError ("Circular reference in data" ),
@@ -687,7 +699,10 @@ def test_user_handles_unexpected_errors_gracefully(self, json_manager_with_schem
687699 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.os_path.join" ,
688700 return_value = "/test/directory/output.json" ,
689701 ),
690- patch ("builtins.open" , side_effect = unexpected_error ),
702+ patch (
703+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
704+ side_effect = unexpected_error ,
705+ ),
691706 patch ("ardupilot_methodic_configurator.backend_filesystem_json_with_schema.logging_error" ) as mock_error ,
692707 ):
693708 # Act: User encounters unexpected error
@@ -731,10 +746,10 @@ def test_user_completes_full_workflow_successfully(self) -> None:
731746 ),
732747 patch (
733748 "builtins.open" ,
734- side_effect = [
735- mock_open ( read_data = json . dumps ( schema_data )). return_value , # Schema file
736- mock_open (). return_value , # Data file for saving
737- ] ,
749+ return_value = mock_open ( read_data = json . dumps ( schema_data )). return_value , # Schema file
750+ ),
751+ patch (
752+ "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.safe_write" ,
738753 ),
739754 patch (
740755 "ardupilot_methodic_configurator.backend_filesystem_json_with_schema.json_dumps" ,
0 commit comments