@@ -826,7 +826,7 @@ def test_merge_trc(trc_files, mock_can_bus, mock_PCANBasic):
826826
827827 cc_pcan ._merge_trc ()
828828
829- with open (result_path , "r" , encoding = ' utf-8' ) as trc :
829+ with open (result_path , "r" , encoding = " utf-8" ) as trc :
830830 result = trc .read ()
831831
832832 assert trc_merge_data == result
@@ -842,7 +842,7 @@ def test_merge_trc_with_file_name(trc_files, mock_can_bus, mock_PCANBasic):
842842 result_path = trc_files [0 ]
843843 cc_pcan ._merge_trc ()
844844
845- with open (result_path , "r" , encoding = ' utf-8' ) as trc :
845+ with open (result_path , "r" , encoding = " utf-8" ) as trc :
846846 result = trc .read ()
847847
848848 assert trc_merge_data == result
@@ -859,7 +859,7 @@ def test_merge_trc_with_old_file_version(trc_files_v1_1, mock_can_bus, mock_PCAN
859859 result_path = trc_files_v1_1 [0 ]
860860 cc_pcan ._merge_trc ()
861861
862- with open (result_path , "r" , encoding = ' utf-8' ) as trc :
862+ with open (result_path , "r" , encoding = " utf-8" ) as trc :
863863 result = trc .read ()
864864
865865 assert trc_data_start_4 + trc_data_end == result
@@ -880,7 +880,7 @@ def test_merge_trc_with_multiple_dir(trc_files_different_directory, mock_can_bus
880880 cc_pcan ._merge_trc ()
881881
882882 # Check if all the trace from the different repo has been merged in one file
883- with open (result_path , "r" , encoding = ' utf-8' ) as trc :
883+ with open (result_path , "r" , encoding = " utf-8" ) as trc :
884884 result = trc .read ()
885885 assert trc_merge_data == result
886886
@@ -1069,3 +1069,52 @@ def test_start_pcan_already_started(caplog):
10691069 with caplog .at_level (logging .WARNING ):
10701070 cc_pcan ._cc_open ()
10711071 assert "Pcan is already opened" in caplog .text
1072+
1073+
1074+ def test_rename_trc_truncate_filename (tmp_path , mock_can_bus , mock_PCANBasic , caplog , mocker ):
1075+ """Test that _rename_trc truncates filenames that are too long"""
1076+ long_name = "a" * 260 + ".trc"
1077+ expected_truncated = "a" * 251 + ".trc"
1078+
1079+ trace_file = tmp_path / "temp_trace.trc"
1080+ trace_file .touch ()
1081+
1082+ cc_pcan = CCPCanCan (trace_path = tmp_path )
1083+ cc_pcan ._trc_file_names [tmp_path ] = [long_name ]
1084+
1085+ mock_rename = mocker .patch .object (Path , "rename" )
1086+ mock_rename .side_effect = [OSError ("Filename too long" ), None ]
1087+
1088+ with caplog .at_level (logging .WARNING ):
1089+ cc_pcan ._rename_trc ()
1090+
1091+ assert "Filename too long, truncating" in caplog .text
1092+ assert long_name in caplog .text
1093+ assert expected_truncated in caplog .text
1094+
1095+ assert mock_rename .call_count == 2
1096+ mock_rename .assert_any_call (tmp_path / long_name )
1097+ mock_rename .assert_any_call (tmp_path / expected_truncated )
1098+
1099+
1100+ def test_rename_trc_oserror_short_filename (tmp_path , mock_can_bus , mock_PCANBasic , caplog , mocker ):
1101+ """Test that _rename_trc re-raises OSError when filename is not too long"""
1102+ short_name = "short_filename.trc"
1103+
1104+ trace_file = tmp_path / "temp_trace.trc"
1105+ trace_file .touch ()
1106+
1107+ cc_pcan = CCPCanCan (trace_path = tmp_path )
1108+ cc_pcan ._trc_file_names [tmp_path ] = [short_name ]
1109+
1110+ mock_rename = mocker .patch .object (Path , "rename" )
1111+ mock_rename .side_effect = OSError ("Permission denied" )
1112+
1113+ with caplog .at_level (logging .ERROR ):
1114+ with pytest .raises (OSError , match = "Permission denied" ):
1115+ cc_pcan ._rename_trc ()
1116+
1117+ assert "Failed to rename trace file: Permission denied" in caplog .text
1118+
1119+ assert mock_rename .call_count == 1
1120+ mock_rename .assert_called_once_with (tmp_path / short_name )
0 commit comments