Skip to content

Commit d6ec7ed

Browse files
committed
add testing
1 parent 9621bd3 commit d6ec7ed

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/tests_pytorch/profilers/test_profiler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,33 @@ def test_advanced_profiler_dump_states(tmp_path):
322322
assert len(data) > 0
323323

324324

325+
@pytest.mark.parametrize("char", ["/", "\\", ":", "*", "?", '"', "<", ">", "|", "\n", "\r", "\t"])
326+
def test_advanced_profiler_dump_states_sanitizes_filename(tmp_path, char):
327+
"""Profiler should sanitize action names to produce filesystem-safe .prof filenames.
328+
329+
This guards against errors when callbacks or actions include path-unsafe characters (e.g., metric names with '/').
330+
331+
"""
332+
profiler = AdvancedProfiler(dirpath=tmp_path, dump_stats=True)
333+
action_name = f"before{char}after"
334+
with profiler.profile(action_name):
335+
pass
336+
337+
profiler.describe()
338+
339+
prof_files = [f for f in os.listdir(tmp_path) if f.endswith(".prof")]
340+
assert len(prof_files) == 1
341+
prof_name = prof_files[0]
342+
343+
# Ensure none of the path-unsafe characters are present in the produced filename
344+
forbidden = ["/", "\\", ":", "*", "?", '"', "<", ">", "|", "\n", "\r", "\t"]
345+
for bad in forbidden:
346+
assert bad not in prof_name
347+
348+
# File should be non-empty
349+
assert (tmp_path / prof_name).read_bytes()
350+
351+
325352
def test_advanced_profiler_value_errors(advanced_profiler):
326353
"""Ensure errors are raised where expected."""
327354
action = "test"

0 commit comments

Comments
 (0)