| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import os |
| 6 |
import pathlib |
| 7 |
import pytest |
| 8 |
import shutil |
| 9 |
import stat |
| 10 |
import sys |
| 11 |
from scalene.redirect_python import redirect_python |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def python_alias_dir(tmp_path): |
| 16 |
|
| 17 |
dir = tmp_path / "python_alias" |
| 18 |
dir.mkdir() |
| 19 |
return dir |
| 20 |
|
| 21 |
|
| 22 |
def test_redirect_python(python_alias_dir): |
| 23 |
preface = "echo" |
| 24 |
cmdline = "--version" |
| 25 |
original_path = os.environ["PATH"] |
| 26 |
original_sys_executable = sys.executable |
| 27 |
original_sys_path = sys.path.copy() |
| 28 |
|
| 29 |
try: |
| 30 |
orig_executable = redirect_python(preface, cmdline, python_alias_dir) |
| 31 |
|
| 32 |
assert sys.executable != original_sys_executable |
| 33 |
|
| 34 |
assert str(python_alias_dir) in sys.path |
| 35 |
|
| 36 |
assert str(python_alias_dir) in os.environ["PATH"] |
| 37 |
|
| 38 |
base_python_extension = ".exe" if sys.platform == "win32" else "" |
| 39 |
all_python_names = [ |
| 40 |
"python" + base_python_extension, |
| 41 |
f"python{sys.version_info.major}{base_python_extension}", |
| 42 |
f"python{sys.version_info.major}.{sys.version_info.minor}{base_python_extension}", |
| 43 |
] |
| 44 |
for name in all_python_names: |
| 45 |
fname = python_alias_dir / name |
| 46 |
if sys.platform == "win32": |
| 47 |
fname = fname.with_suffix(".bat") |
| 48 |
assert fname.exists() |
| 49 |
finally: |
| 50 |
|
| 51 |
sys.executable = original_sys_executable |
| 52 |
sys.path = original_sys_path |
| 53 |
os.environ["PATH"] = original_path |
| 54 |
|
| 55 |
shutil.rmtree(python_alias_dir) |