Repository navigation
Include directory path in pytest log file names - #14939
Conversation
There was a problem hiding this comment.
Pull request overview
Updates pytest per-test log naming to include directory components (relative to unit-tests/) so log filenames match the legacy run-unit-tests.py convention and remain stable for CI artifact linking.
Changes:
- Update
test_log_name()to prepend directory components (joined with-) to the log filename. - Update infra tests to match the new naming and add edge-case coverage (root-level tests, single dir, deep nesting, absolute paths).
- Update internal documentation describing the naming scheme.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
unit-tests/py/rspy/pytest/logging_setup.py |
Implements directory-aware pytest log filename derivation. |
unit-tests/infra-tests/test_log_naming.py |
Updates expectations and adds edge-case tests for the new naming. |
.github/skills/pytest-infra.md |
Documents the updated log naming convention and examples. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Resolve relative path within the unit-tests tree | ||
| normalized = file_path.replace(os.sep, '/') | ||
| marker = 'unit-tests/' | ||
| idx = normalized.rfind(marker) | ||
| if idx >= 0: | ||
| rel_path = normalized[idx + len(marker):] | ||
| else: | ||
| rel_path = normalized |
There was a problem hiding this comment.
When unit-tests/ isn't found in file_path, the code falls back to using the entire normalized path. For absolute paths outside the unit-tests tree this will embed large portions of the filesystem path into the log filename (after /→-), which can leak host paths and may exceed filename length limits. Consider falling back to just the file basename (no directory prefix) when the marker isn't found, or compute a safe relative path against _unit_tests_dir/repo root instead of using the full absolute path.
| # Resolve relative path within the unit-tests tree | |
| normalized = file_path.replace(os.sep, '/') | |
| marker = 'unit-tests/' | |
| idx = normalized.rfind(marker) | |
| if idx >= 0: | |
| rel_path = normalized[idx + len(marker):] | |
| else: | |
| rel_path = normalized | |
| # Resolve a safe relative path within the unit-tests tree. If the file is | |
| # outside that tree, fall back to just the basename to avoid embedding host | |
| # filesystem paths in the log filename. | |
| abs_file_path = os.path.normpath(os.path.abspath(file_path)) | |
| abs_unit_tests_dir = os.path.normpath(os.path.abspath(_unit_tests_dir)) | |
| try: | |
| rel_to_unit_tests = os.path.relpath(abs_file_path, abs_unit_tests_dir) | |
| if rel_to_unit_tests == os.curdir or rel_to_unit_tests.startswith('..' + os.sep): | |
| rel_path = os.path.basename(abs_file_path) | |
| else: | |
| rel_path = rel_to_unit_tests | |
| except ValueError: | |
| # Different drive on Windows, or another path normalization failure. | |
| rel_path = os.path.basename(abs_file_path) |
| - With device param: pytest-depth.py::test[D455-111] → pytest-live-frames-depth_D455-111.log | ||
| - Without device param: pytest-depth.py::test_basic → pytest-live-frames-depth.log |
There was a problem hiding this comment.
The docstring examples mix inputs with and without directory prefixes (e.g., “With device param: pytest-depth.py::...” but output includes “pytest-live-frames-...”). Updating the examples to consistently include the same input path form (e.g., live/frames/pytest-depth.py::...) would make the expectations clearer and match the behavior being tested.
| - With device param: pytest-depth.py::test[D455-111] → pytest-live-frames-depth_D455-111.log | |
| - Without device param: pytest-depth.py::test_basic → pytest-live-frames-depth.log | |
| - With device param: live/frames/pytest-depth.py::test[D455-111] → pytest-live-frames-depth_D455-111.log | |
| - Without device param: live/frames/pytest-depth.py::test_basic → pytest-live-frames-depth.log |
|
|
||
| if dirname: | ||
| # Strip 'pytest-' prefix, prepend 'pytest-' + dir components joined by '-' | ||
| stripped = basename[7:] if basename.startswith('pytest-') else basename |
There was a problem hiding this comment.
can basename.startswith('pytest-') ever not happen?
There was a problem hiding this comment.
Yes, legacy run-unit-tests also produce logs with test-*.log, right?
There was a problem hiding this comment.
Good catch — in production it can't. conftest sets python_files = pytest-*.py, so every collected file starts with pytest-. Dropped the conditional and added a comment explaining it.
| # Absolute path outside the unit-tests tree — use basename only to avoid | ||
| # embedding host filesystem paths in the log filename. | ||
| rel_path = os.path.basename(normalized) | ||
| else: |
There was a problem hiding this comment.
this branch only happens on the infra tests - please add a comment mentioning this
There was a problem hiding this comment.
Added a comment on that else branch clarifying it's only reached by infra test mocks that pass relative paths directly.
Summary
run-unit-tests.pynaming conventionlive/frames/pytest-depth.pyproducespytest-live-frames-depth.loginstead ofpytest-depth.logTest plan