pytest-dev / pytest-dev/pytest
Permission Denied Error for root owned folder
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
Bug
I was adding tests as a user to a project that contains a symlinked subfolder owned by root (so I don't have access to it as my normal user). In the collections process pytest fails with a PermissionError: [Errno 13] Permission denied (stack trace below). sudo pytest works but I don't want to have to run my tests as root.
Attempted fixes
I have tried using a pytest.ini file that uses norecursedirs = privileged_dir or addopts --ignore=privileged_dir but it appears the issue occurs during collection when pytest tries to check if the directory is a file and that raises a PermissionError.
Possible Patch
I monkey-patched main.py to do the following and calling pytest is now working.
# main.py (line ~685)
for direntry in visit(str(argpath), self._recurse):
try:
if not direntry.is_file():
continue
except PermissionError:
warnings.warn(f"PyTest could not check folder {direntry} due to PermissionError")
continue
Stack Trace
../anaconda3/lib/python3.8/site-packages/_pytest/runner.py:310: in from_call
result = func() # type: Optional[TResult]
../anaconda3/lib/python3.8/site-packages/_pytest/runner.py:340: in <lambda>
call = CallInfo.from_call(lambda: list(collector.collect()), "collect")
../anaconda3/lib/python3.8/site-packages/_pytest/main.py:685: in collect
if not direntry.is_file():
E PermissionError: [Errno 13] Permission denied: '/home/project/privileged_dir'
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in _pytest/main.py around the collect method at line 685, where direntry.is_file() raises PermissionError for an inaccessible symlinked folder. Reproduce collection with a root-owned folder and compare the behavior with the proposed guard. Done means pytest can collect accessible tests without requiring root and handles the inaccessible entry without aborting collection.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100