@@ -23,11 +23,14 @@ def test_custom_interpreter(
23
23
capfd : pytest .CaptureFixture [str ],
24
24
args_joined : bool ,
25
25
) -> None :
26
- result = virtualenv .cli_run ([str (tmp_path / "venv" ), "--activators" , "" ])
27
- cmd = [sys .executable ]
26
+ # Delete $PYTHONPATH so that it cannot be passed to the custom interpreter process (since we don't know what
27
+ # distribution metadata to expect when it's used).
28
+ monkeypatch .delenv ("PYTHONPATH" , False )
29
+
28
30
monkeypatch .chdir (tmp_path )
31
+ result = virtualenv .cli_run ([str (tmp_path / "venv" ), "--activators" , "" ])
29
32
py = str (result .creator .exe .relative_to (tmp_path ))
30
- cmd + = [f"--python={ result .creator .exe } " ] if args_joined else ["--python" , py ]
33
+ cmd = ["" , f"--python={ result .creator .exe } " ] if args_joined else ["" , "--python" , py ]
31
34
mocker .patch ("pipdeptree._discovery.sys.argv" , cmd )
32
35
main ()
33
36
out , _ = capfd .readouterr ()
@@ -69,3 +72,37 @@ def test_custom_interpreter_with_local_only(
69
72
if sys .version_info >= (3 , 12 ):
70
73
expected -= {"setuptools" , "wheel" } # pragma: no cover
71
74
assert found == expected , out
75
+
76
+
77
+ def test_custom_interpreter_ensure_pythonpath_envar_is_honored (
78
+ tmp_path : Path ,
79
+ mocker : MockerFixture ,
80
+ monkeypatch : pytest .MonkeyPatch ,
81
+ capfd : pytest .CaptureFixture [str ],
82
+ ) -> None :
83
+ # ensures that we honor $PYTHONPATH when passing it to the custom interpreter process
84
+ venv_path = str (tmp_path / "venv" )
85
+ result = virtualenv .cli_run ([venv_path , "--activators" , "" ])
86
+
87
+ another_path = tmp_path / "another-path"
88
+ fake_dist = another_path / "foo-1.2.3.dist-info"
89
+ fake_dist .mkdir (parents = True )
90
+ fake_metadata = fake_dist / "METADATA"
91
+ with fake_metadata .open ("w" ) as f :
92
+ f .write ("Metadata-Version: 2.3\n " "Name: foo\n " "Version: 1.2.3\n " )
93
+ cmd = ["" , f"--python={ result .creator .exe } " ]
94
+ mocker .patch ("pipdeptree._discovery.sys.argv" , cmd )
95
+ monkeypatch .setenv ("PYTHONPATH" , str (another_path ))
96
+ main ()
97
+ out , _ = capfd .readouterr ()
98
+ found = {i .split ("==" )[0 ] for i in out .splitlines ()}
99
+ implementation = python_implementation ()
100
+ if implementation == "CPython" :
101
+ expected = {"foo" , "pip" , "setuptools" , "wheel" }
102
+ elif implementation == "PyPy" : # pragma: cpython no cover
103
+ expected = {"foo" , "cffi" , "greenlet" , "pip" , "readline" , "setuptools" , "wheel" }
104
+ else : # pragma: no cover
105
+ raise ValueError (implementation )
106
+ if sys .version_info >= (3 , 12 ): # pragma: >=3.12 cover
107
+ expected -= {"setuptools" , "wheel" }
108
+ assert found == expected , out
0 commit comments