Debian Codegraph @

tests/test_coverup_15.py

raw symbols SCIP
1 # file scalene/redirect_python.py:8-50
2 # lines [8, 17, 18, 19, 20, 21, 24, 25, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 44, 46, 47, 48, 50]
3 # branches ['29->30', '29->41', '31->32', '31->33', '36->29', '36->37', '47->48', '47->50']
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 # Create a temporary directory for the test
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 # Check if the sys.executable has been changed
32 assert sys.executable != original_sys_executable
33 # Check if the sys.path has been updated
34 assert str(python_alias_dir) in sys.path
35 # Check if the PATH environment variable has been updated
36 assert str(python_alias_dir) in os.environ["PATH"]
37 # Check if the files have been created
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 # Clean up: Restore the original sys.executable, sys.path, and PATH
51 sys.executable = original_sys_executable
52 sys.path = original_sys_path
53 os.environ["PATH"] = original_path
54 # Remove the temporary directory
55 shutil.rmtree(python_alias_dir)