-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add a pythonpath setting to allow paths to be added to sys.path.
#9134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa18d5e
Add a `pythonpath` setting to allow paths to be added to `sys.path`.
okken 028d56b
add comments to a couple tests
okken 6ead021
changes in response to code review
okken 33d89c2
clean up sys.path during unconfigure
okken e1a0437
Update testing/test_pythonpath.py
okken 02e8d93
retrieve list in unconfigure with getini
okken a6a24dc
Update changelog/9114.feature.rst
okken 3078407
make sure str version is removed, plus only remove if present
okken 9f72f93
Merge remote-tracking branch 'origin/9114_pythonpath_setting' into 91…
okken 13382a3
1st draft of doc mods + changelog mod
okken dd05547
Apply suggestions from code review
okken f8f7091
Test clean up, assert the exit code
bluetech 0808d93
Update src/_pytest/config/__init__.py
bluetech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| Added :ref:`pythonpath` setting that adds listed paths to `sys.path`. | ||
This file contains hidden or 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 hidden or 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,28 @@ | ||
| import sys | ||
| from typing import List | ||
|
|
||
| import pytest | ||
okken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from pytest import Config | ||
| from pytest import Parser | ||
|
|
||
|
|
||
| added_paths: List[str] | ||
|
|
||
|
|
||
| def pytest_addoption(parser: Parser) -> None: | ||
| parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[]) | ||
|
|
||
|
|
||
| @pytest.hookimpl(tryfirst=True) | ||
| def pytest_load_initial_conftests(early_config: Config) -> None: | ||
| global added_paths | ||
| added_paths = [str(p) for p in early_config.getini("pythonpath")] | ||
| # `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]` | ||
| for path in reversed(added_paths): | ||
| sys.path.insert(0, path) | ||
|
|
||
|
|
||
| def pytest_unconfigure() -> None: | ||
okken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| global added_paths | ||
okken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for path in added_paths: | ||
| sys.path.remove(path) | ||
okken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
okken marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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,72 @@ | ||
| from textwrap import dedent | ||
|
|
||
| import pytest | ||
| from _pytest.pytester import Pytester | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def file_structure(pytester: Pytester) -> None: | ||
| pytester.makepyfile( | ||
| test_foo=""" | ||
| from foo import foo | ||
|
|
||
| def test_foo(): | ||
| assert foo() == 1 | ||
| """ | ||
| ) | ||
|
|
||
| pytester.makepyfile( | ||
| test_bar=""" | ||
| from bar import bar | ||
|
|
||
| def test_bar(): | ||
| assert bar() == 2 | ||
| """ | ||
| ) | ||
|
|
||
| foo_py = pytester.mkdir("sub") / "foo.py" | ||
| content = dedent( | ||
| """ | ||
| def foo(): | ||
| return 1 | ||
| """ | ||
| ) | ||
| foo_py.write_text(content, encoding="utf-8") | ||
|
|
||
| bar_py = pytester.mkdir("sub2") / "bar.py" | ||
| content = dedent( | ||
| """ | ||
| def bar(): | ||
| return 2 | ||
| """ | ||
| ) | ||
| bar_py.write_text(content, encoding="utf-8") | ||
|
|
||
|
|
||
| def test_one_dir(pytester: Pytester, file_structure) -> None: | ||
| pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub\n") | ||
| result = pytester.runpytest("test_foo.py") | ||
| result.assert_outcomes(passed=1) | ||
|
|
||
|
|
||
| def test_two_dirs(pytester: Pytester, file_structure) -> None: | ||
| pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub sub2\n") | ||
| result = pytester.runpytest("test_foo.py", "test_bar.py") | ||
| result.assert_outcomes(passed=2) | ||
|
|
||
|
|
||
| def test_module_not_found(pytester: Pytester, file_structure) -> None: | ||
| """Without the pythonpath setting, the module should not be found.""" | ||
| pytester.makefile(".ini", pytest="[pytest]\n") | ||
| result = pytester.runpytest("test_foo.py") | ||
| result.assert_outcomes(errors=1) | ||
| expected_error = "E ModuleNotFoundError: No module named 'foo'" | ||
| result.stdout.fnmatch_lines([expected_error]) | ||
|
|
||
|
|
||
| def test_no_ini(pytester: Pytester, file_structure) -> None: | ||
| """If no ini file, test should error.""" | ||
| result = pytester.runpytest("test_foo.py") | ||
| result.assert_outcomes(errors=1) | ||
| expected_error = "E ModuleNotFoundError: No module named 'foo'" | ||
| result.stdout.fnmatch_lines([expected_error]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.