forked from pytest-dev/pytest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythonpath: make it compatible with pytest-pythonpath until pytest 8
Fix pytest-dev#9636.
- Loading branch information
Showing
5 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Restore compatiblity to users of the pytest-pythonpath plugins by adding two ini options ``python_paths`` and ``site_dirs``. | ||
These options are immediately marked as deprecated. See :ref:`python_paths-site_dir-ini` for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import sys | ||
|
||
from distutils.core import setup | ||
|
||
if __name__ == "__main__": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
import sys | ||
|
||
import pytest | ||
from _pytest.deprecated import PYTHON_PATHS_INI | ||
from _pytest.deprecated import SITE_DIRS_INI | ||
from pytest import Config | ||
from pytest import Parser | ||
|
||
|
||
def pytest_addoption(parser: Parser) -> None: | ||
parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[]) | ||
parser.addini( | ||
"python_paths", type="paths", help="Deprecated alias for pythonpath", default=[] | ||
) | ||
parser.addini( | ||
"site_dirs", | ||
type="paths", | ||
help="Deprecated: directory paths to add to via site.addsitedir(path)", | ||
default=[], | ||
) | ||
|
||
|
||
@pytest.hookimpl(tryfirst=True) | ||
def pytest_load_initial_conftests(early_config: Config) -> None: | ||
pythonpath = early_config.getini("pythonpath") | ||
python_paths = early_config.getini("python_paths") | ||
if python_paths: | ||
early_config.issue_config_time_warning(PYTHON_PATHS_INI, 2) | ||
if not pythonpath: | ||
pythonpath = python_paths | ||
# `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]` | ||
for path in reversed(early_config.getini("pythonpath")): | ||
for path in reversed(pythonpath): | ||
sys.path.insert(0, str(path)) | ||
|
||
site_dirs = early_config.getini("site_dirs") | ||
if site_dirs: | ||
early_config.issue_config_time_warning(SITE_DIRS_INI, 2) | ||
|
||
import site | ||
|
||
for path in site_dirs: | ||
site.addsitedir(str(path)) | ||
|
||
|
||
@pytest.hookimpl(trylast=True) | ||
def pytest_unconfigure(config: Config) -> None: | ||
for path in config.getini("pythonpath"): | ||
pythonpath = config.getini("pythonpath") | ||
python_paths = config.getini("python_paths") | ||
if python_paths and not pythonpath: | ||
pythonpath = python_paths | ||
for path in pythonpath: | ||
path_str = str(path) | ||
if path_str in sys.path: | ||
sys.path.remove(path_str) |