Skip to content

Conversation

@jayalath-jknr
Copy link

Fixes #13822

This PR addresses a bug where pytest incorrectly identifies the system temp directory as the project rootdir when a setup.py file exists in /tmp (or the system temp directory).

Problem

When a setup.py file exists in the temp directory (e.g., /tmp/setup.py), pytest's rootdir detection logic would walk up the directory tree and find this file, incorrectly setting /tmp as the rootdir. This caused test nodeids to include the full temporary path instead of just the filename, breaking the test_warning_recorded_hook test and potentially affecting other functionality.

Solution

The fix modifies the determine_setup function in src/_pytest/config/findpaths.py to skip setup.py files specifically located in the directory returned by tempfile.gettempdir(). This prevents the temp directory from being mistakenly identified as a project root while preserving normal setup.py detection for legitimate project directories.

Changes

  • src/_pytest/config/findpaths.py: Added logic to skip setup.py files in the temp directory when determining rootdir
  • testing/test_findpaths.py: Added comprehensive tests to ensure the fix works and normal setup.py detection still functions

Testing

The fix has been validated with:

  1. Reproduction of the original issue with /tmp/setup.py present
  2. Verification that the fix resolves the failing test_warning_recorded_hook test
  3. New unit tests ensuring temp directory setup.py files are ignored
  4. Regression tests confirming normal setup.py detection still works
  5. Full test suite validation for rootdir-related functionality

This is a targeted fix that specifically addresses the temp directory edge case without affecting other pytest functionality.

- Skip setup.py in tempfile.gettempdir() when determining rootdir
- This prevents pytest from incorrectly using /tmp as rootdir when
  /tmp/setup.py exists, which was causing test failures
- Resolves issue pytest-dev#13822

The issue occurred because pytest walks up the directory tree looking
for setup.py files to determine the project root. When a setup.py
exists in the system temp directory (e.g., /tmp/setup.py), pytest
would incorrectly identify /tmp as the rootdir, causing test nodeids
to include the full temporary path instead of just the filename.

This fix adds a check to skip setup.py files specifically in the
temp directory returned by tempfile.gettempdir(), while preserving
normal setup.py detection for legitimate project directories.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure when /tmp/setup.py exists

1 participant