|
| 1 | +"""Activate this does not mangles with the shell itself to provision the python, but instead mangles |
| 2 | +with the caller interpreter, effectively making so that the virtualenv activation constraints are met once |
| 3 | +it's loaded. |
| 4 | +
|
| 5 | +While initially may feel like a import is all we need, import is executed only once not at every activation. To |
| 6 | +work around this we'll use Python 2 execfile, and Python 3 exec(read()). |
| 7 | +
|
| 8 | +Virtual env activation constraints that we guarantee: |
| 9 | +- the virtualenv site-package will be visible from the activator Python |
| 10 | +- virtualenv packages take priority over python 2 |
| 11 | +- virtualenv bin PATH pre-pended |
| 12 | +- VIRTUAL_ENV env var will be set. |
| 13 | +- if the user tries to import we'll raise |
| 14 | +
|
| 15 | +""" |
| 16 | +from __future__ import absolute_import, unicode_literals |
| 17 | + |
1 | 18 | import os |
| 19 | +import re |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import textwrap |
| 23 | + |
2 | 24 |
|
| 25 | +def test_activate_this(activation_env, tmp_path, monkeypatch): |
| 26 | + # to test this, we'll try to use the activation env from this Python |
| 27 | + monkeypatch.delenv(str("VIRTUAL_ENV"), raising=False) |
| 28 | + monkeypatch.delenv(str("PYTHONPATH"), raising=False) |
| 29 | + start_path = os.pathsep.join([str(tmp_path), str(tmp_path / "other")]) |
| 30 | + monkeypatch.setenv(str("PATH"), start_path) |
| 31 | + activator = tmp_path.__class__(activation_env[1]) / "activate_this.py" |
| 32 | + assert activator.exists() |
3 | 33 |
|
4 | | -def get_base_dir(): |
5 | | - return os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) |
| 34 | + activator_at = str(activator) |
| 35 | + script = textwrap.dedent( |
| 36 | + """ |
| 37 | + import os |
| 38 | + import sys |
| 39 | + print(os.environ.get("VIRTUAL_ENV")) |
| 40 | + print(os.environ.get("PATH")) |
| 41 | + try: |
| 42 | + import pydoc_test |
| 43 | + raise RuntimeError("this should not happen") |
| 44 | + except ImportError: |
| 45 | + pass |
| 46 | + print(os.pathsep.join(sys.path)) |
| 47 | + file_at = {!r} |
| 48 | + exec(open(file_at).read(), {{'__file__': file_at}}) |
| 49 | + print(os.environ.get("VIRTUAL_ENV")) |
| 50 | + print(os.environ.get("PATH")) |
| 51 | + print(os.pathsep.join(sys.path)) |
| 52 | + import pydoc_test |
| 53 | + print(pydoc_test.__file__) |
| 54 | + """.format( |
| 55 | + str(activator_at) |
| 56 | + ) |
| 57 | + ) |
| 58 | + script_path = tmp_path / "test.py" |
| 59 | + script_path.write_text(script) |
| 60 | + try: |
| 61 | + raw = subprocess.check_output( |
| 62 | + [sys.executable, str(script_path)], stderr=subprocess.STDOUT, universal_newlines=True |
| 63 | + ) |
6 | 64 |
|
| 65 | + out = re.sub(r"pydev debugger: process \d+ is connecting\n\n", "", raw, re.M).strip().split("\n") |
7 | 66 |
|
8 | | -def get_activate_this_path(): |
9 | | - return os.path.abspath(os.path.join(get_base_dir(), "virtualenv_embedded", "activate_this.py")) |
| 67 | + assert out[0] == "None" |
| 68 | + assert out[1] == start_path |
| 69 | + prev_sys_path = out[2].split(os.path.pathsep) |
10 | 70 |
|
| 71 | + assert out[3] == activation_env[0] # virtualenv set as the activated env |
11 | 72 |
|
12 | | -def activate(): |
13 | | - activate_this = get_activate_this_path() |
14 | | - exec(compile(open(activate_this).read(), activate_this, "exec"), dict(__file__=activate_this)) |
| 73 | + # PATH updated with activated |
| 74 | + assert out[4].endswith(start_path) |
| 75 | + assert out[4][: -len(start_path)].split(os.pathsep) == [activation_env[1], ""] |
15 | 76 |
|
| 77 | + # sys path contains the site package at its start |
| 78 | + new_sys_path = out[5].split(os.path.pathsep) |
| 79 | + assert new_sys_path[-len(prev_sys_path) :] == prev_sys_path |
| 80 | + extra_start = new_sys_path[0 : -len(prev_sys_path)] |
| 81 | + assert len(extra_start) == 1 |
| 82 | + assert extra_start[0].startswith(activation_env[0]) |
| 83 | + assert tmp_path.__class__(extra_start[0]).exists() |
16 | 84 |
|
17 | | -def test_activate_this(): |
18 | | - base_dir = get_base_dir() |
19 | | - old_path = os.environ["PATH"] |
| 85 | + # manage to import from activate site package |
| 86 | + assert out[6] == str(activation_env[2]) |
| 87 | + except subprocess.CalledProcessError as exception: |
| 88 | + assert not exception.returncode, exception.output |
20 | 89 |
|
21 | | - activate() |
22 | 90 |
|
23 | | - assert os.environ["PATH"] == os.path.dirname(get_activate_this_path()) + os.pathsep + old_path |
24 | | - assert os.environ["VIRTUAL_ENV"] == base_dir |
| 91 | +def test_activate_this_no_file(activation_env, tmp_path, monkeypatch): |
| 92 | + activator = tmp_path.__class__(activation_env[1]) / "activate_this.py" |
| 93 | + assert activator.exists() |
| 94 | + try: |
| 95 | + subprocess.check_output( |
| 96 | + [sys.executable, "-c", "exec(open({!r}).read())".format(str(activator))], |
| 97 | + stderr=subprocess.STDOUT, |
| 98 | + universal_newlines=True, |
| 99 | + ) |
| 100 | + raise RuntimeError("this should not happen") |
| 101 | + except subprocess.CalledProcessError as exception: |
| 102 | + out = re.sub(r"pydev debugger: process \d+ is connecting\n\n", "", exception.output, re.M).strip() |
| 103 | + assert "You must use exec(open(this_file).read(), {'__file__': this_file}))" in out, out |
0 commit comments