Skip to content

Commit 96909cc

Browse files
authored
fix: trace file name too long (#562)
1 parent 1236892 commit 96909cc

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pykiso"
3-
version = "1.6.0"
3+
version = "1.6.1"
44
description = "Embedded integration testing framework."
55
authors = ["Sebastian Fischer <sebastian.fischer@de.bosch.com>"]
66
license = "Eclipse Public License - v 2.0"

src/pykiso/lib/connectors/cc_pcan_can/cc_pcan_can.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,18 @@ def _rename_trc(self):
537537
list_of_traces = sorted(trace_path.glob("*.trc"), key=os.path.getmtime)[-len(trace_file_names) :]
538538
for index, file_name in enumerate(trace_file_names):
539539
if file_name is not None:
540-
list_of_traces[index].rename(trace_path / file_name)
540+
try:
541+
list_of_traces[index].rename(trace_path / file_name)
542+
except OSError as e:
543+
if len(file_name) > 255: # Most filesystems have 255 char limit
544+
# Truncate filename while preserving extension
545+
name, ext = os.path.splitext(file_name)
546+
truncated_name = name[len(ext) - 255 :] + ext
547+
log.warning("Filename too long, truncating '%s' to '%s'", file_name, truncated_name)
548+
list_of_traces[index].rename(trace_path / truncated_name)
549+
else:
550+
log.error("Failed to rename trace file: %s", e)
551+
raise
541552

542553
def shutdown(self) -> None:
543554
"""Destructor method."""

tests/test_cc_pcan_can.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)