pytest-dev / pytest-dev/pytest

Permission Denied Error for root owned folder

Open
#9,089 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection type: bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.