Debian Codegraph @

testing/test_python_path.py

raw
1 # mypy: allow-untyped-defs
2 from __future__ import annotations
3
4 import sys
5 from textwrap import dedent
6
7 from _pytest.pytester import Pytester
8 import pytest
9
10
11 @pytest.fixture()
12 def file_structure(pytester: Pytester) -> None:
13 pytester.makepyfile(
14 test_foo="""
15 from foo import foo
16
17 def test_foo():
18 assert foo() == 1
19 """
20 )
21
22 pytester.makepyfile(
23 test_bar="""
24 from bar import bar
25
26 def test_bar():
27 assert bar() == 2
28 """
29 )
30
31 foo_py = pytester.mkdir("sub") / "foo.py"
32 content = dedent(
33 """
34 def foo():
35 return 1
36 """
37 )
38 foo_py.write_text(content, encoding="utf-8")
39
40 bar_py = pytester.mkdir("sub2") / "bar.py"
41 content = dedent(
42 """
43 def bar():
44 return 2
45 """
46 )
47 bar_py.write_text(content, encoding="utf-8")
48
49
50 def test_one_dir(pytester: Pytester, file_structure) -> None:
51 pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub\n")
52 result = pytester.runpytest("test_foo.py")
53 assert result.ret == 0
54 result.assert_outcomes(passed=1)
55
56
57 def test_two_dirs(pytester: Pytester, file_structure) -> None:
58 pytester.makefile(".ini", pytest="[pytest]\npythonpath=sub sub2\n")
59 result = pytester.runpytest("test_foo.py", "test_bar.py")
60 assert result.ret == 0
61 result.assert_outcomes(passed=2)
62
63
64 def test_local_plugin(pytester: Pytester, file_structure) -> None:
65 """`pythonpath` kicks early enough to load plugins via -p (#11118)."""
66 localplugin_py = pytester.path / "sub" / "localplugin.py"
67 content = dedent(
68 """
69 def pytest_load_initial_conftests():
70 print("local plugin load")
71
72 def pytest_unconfigure():
73 print("local plugin unconfig")
74 """
75 )
76 localplugin_py.write_text(content, encoding="utf-8")
77
78 pytester.makeini("[pytest]\npythonpath=sub\n")
79 result = pytester.runpytest("-plocalplugin", "-s", "test_foo.py")
80 result.stdout.fnmatch_lines(["local plugin load", "local plugin unconfig"])
81 assert result.ret == 0
82 result.assert_outcomes(passed=1)
83
84
85 def test_module_not_found(pytester: Pytester, file_structure) -> None:
86 """Without the pythonpath setting, the module should not be found."""
87 pytester.makefile(".ini", pytest="[pytest]\n")
88 result = pytester.runpytest("test_foo.py")
89 assert result.ret == pytest.ExitCode.INTERRUPTED
90 result.assert_outcomes(errors=1)
91 expected_error = "E ModuleNotFoundError: No module named 'foo'"
92 result.stdout.fnmatch_lines([expected_error])
93
94
95 def test_no_config_file(pytester: Pytester, file_structure) -> None:
96 """If no configuration file, test should error."""
97 result = pytester.runpytest("test_foo.py")
98 assert result.ret == pytest.ExitCode.INTERRUPTED
99 result.assert_outcomes(errors=1)
100 expected_error = "E ModuleNotFoundError: No module named 'foo'"
101 result.stdout.fnmatch_lines([expected_error])
102
103
104 def test_clean_up(pytester: Pytester) -> None:
105 """Test that the plugin cleans up after itself."""
106 # This is tough to test behaviorally because the cleanup really runs last.
107 # So the test make several implementation assumptions:
108 # - Cleanup is done in pytest_unconfigure().
109 # - Not a hook wrapper.
110 # So we can add a hook wrapper ourselves to test what it does.
111 pytester.makefile(".ini", pytest="[pytest]\npythonpath=I_SHALL_BE_REMOVED\n")
112 pytester.makepyfile(test_foo="""def test_foo(): pass""")
113
114 before: list[str] | None = None
115 after: list[str] | None = None
116
117 class Plugin:
118 @pytest.hookimpl(tryfirst=True)
119 def pytest_unconfigure(self) -> None:
120 nonlocal before
121 before = sys.path.copy()
122
123 result = pytester.runpytest_inprocess(plugins=[Plugin()])
124 after = sys.path.copy()
125 assert result.ret == 0
126
127 assert before is not None
128 assert after is not None
129 assert any("I_SHALL_BE_REMOVED" in entry for entry in before)
130 assert not any("I_SHALL_BE_REMOVED" in entry for entry in after)