forked from pypa/virtualenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_activator.py
More file actions
94 lines (74 loc) · 3.43 KB
/
test_python_activator.py
File metadata and controls
94 lines (74 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import annotations
import os
import sys
from ast import literal_eval
from textwrap import dedent
from virtualenv.activation import PythonActivator
from virtualenv.info import IS_WIN
def test_python(raise_on_non_source_class, activation_tester):
class Python(raise_on_non_source_class):
def __init__(self, session) -> None:
super().__init__(
PythonActivator,
session,
sys.executable,
activate_script="activate_this.py",
extension="py",
non_source_fail_message="You must use exec(open(this_file).read(), {'__file__': this_file}))",
)
self.unix_line_ending = not IS_WIN
def env(self, tmp_path):
env = os.environ.copy()
env["PYTHONIOENCODING"] = "utf-8"
for key in ("VIRTUAL_ENV", "PYTHONPATH"):
env.pop(str(key), None)
env["PATH"] = os.pathsep.join([str(tmp_path), str(tmp_path / "other")])
return env
@staticmethod
def _get_test_lines(activate_script):
raw = f"""
import os
import sys
import platform
def print_r(value):
print(repr(value))
print_r(os.environ.get("VIRTUAL_ENV"))
print_r(os.environ.get("VIRTUAL_ENV_PROMPT"))
print_r(os.environ.get("PATH").split(os.pathsep))
print_r(sys.path)
file_at = {str(activate_script)!r}
# CPython 2 requires non-ascii path open to be unicode
with open(file_at, "r", encoding='utf-8') as file_handler:
content = file_handler.read()
exec(content, {{"__file__": file_at}})
print_r(os.environ.get("VIRTUAL_ENV"))
print_r(os.environ.get("VIRTUAL_ENV_PROMPT"))
print_r(os.environ.get("PATH").split(os.pathsep))
print_r(sys.path)
import pydoc_test
print_r(pydoc_test.__file__)
"""
return dedent(raw).splitlines()
def assert_output(self, out, raw, tmp_path): # noqa: ARG002
out = [literal_eval(i) for i in out]
assert out[0] is None # start with VIRTUAL_ENV None
assert out[1] is None # likewise for VIRTUAL_ENV_PROMPT
prev_path = out[2]
prev_sys_path = out[3]
assert out[4] == str(self._creator.dest) # VIRTUAL_ENV now points to the virtual env folder
assert out[5] == str(self._creator.env_name) # VIRTUAL_ENV_PROMPT now has the env name
new_path = out[6] # PATH now starts with bin path of current
assert ([str(self._creator.bin_dir), *prev_path]) == new_path
# sys path contains the site package at its start
new_sys_path = out[7]
new_lib_paths = {str(i) for i in self._creator.libs}
assert prev_sys_path == new_sys_path[len(new_lib_paths) :]
assert new_lib_paths == set(new_sys_path[: len(new_lib_paths)])
# manage to import from activate site package
dest = self.norm_path(self._creator.purelib / "pydoc_test.py")
found = self.norm_path(out[8])
assert found.startswith(dest)
def non_source_activate(self, activate_script):
act = str(activate_script)
return [*self._invoke_script, "-c", f"exec(open({act!r}).read())"]
activation_tester(Python)