Skip to content

Commit 4ba066e

Browse files
Add Tests For _pythoninfo.py (#147)
1 parent d28f8c7 commit 4ba066e

File tree

2 files changed

+140
-5
lines changed

2 files changed

+140
-5
lines changed

pyperformance/tests/__init__.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ def run_cmd(*argv):
4242
print("", flush=True)
4343

4444

45+
class Resources:
46+
"""A mixin that can create resources."""
47+
48+
def venv(self, python=sys.executable):
49+
tmpdir = tempfile.mkdtemp()
50+
venv = os.path.join(tmpdir, 'venv')
51+
run_cmd(python or sys.executable, '-u', '-m', 'venv', venv)
52+
self.addCleanup(lambda: shutil.rmtree(tmpdir))
53+
return venv, resolve_venv_python(venv)
54+
55+
56+
def resolve_venv_python(venv):
57+
if os.name == "nt":
58+
basename = os.path.basename(sys.executable)
59+
venv_python = os.path.join(venv, 'Scripts', basename)
60+
else:
61+
venv_python = os.path.join(venv, 'bin', 'python3')
62+
return venv_python
63+
64+
4565
#############################
4666
# functional tests
4767

@@ -94,11 +114,7 @@ def tearDownClass(cls):
94114

95115
@property
96116
def venv_python(self):
97-
if os.name == "nt":
98-
python = os.path.basename(sys.executable)
99-
return os.path.join(self._VENV, 'Scripts', python)
100-
else:
101-
return os.path.join(self._VENV, 'bin', 'python3')
117+
return resolve_venv_python(self._VENV)
102118

103119
def run_pyperformance(self, cmd, *args, invenv=True):
104120
if invenv:
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import importlib.util
2+
import os
3+
import sys
4+
import unittest
5+
6+
from pyperformance import tests, _pythoninfo
7+
8+
9+
INFO = {
10+
'executable': sys.executable,
11+
'version_str': sys.version,
12+
'version_info': tuple(sys.version_info),
13+
'hexversion': sys.hexversion,
14+
'api_version': sys.api_version,
15+
# importlib.util.MAGIC_NUMBER has been around since 3.5.
16+
'magic_number': importlib.util.MAGIC_NUMBER.hex(),
17+
'implementation_name': sys.implementation.name.lower(),
18+
'implementation_version': tuple(sys.implementation.version),
19+
'platform': sys.platform,
20+
'prefix': sys.prefix,
21+
'exec_prefix': sys.exec_prefix,
22+
'base_prefix': sys.base_prefix,
23+
'base_exec_prefix': sys.base_exec_prefix,
24+
'platlibdir': getattr(sys, 'platlibdir', 'lib'),
25+
'stdlib_dir': os.path.dirname(os.__file__)
26+
}
27+
28+
29+
class GetPythonInfoTests(tests.Resources, unittest.TestCase):
30+
31+
def test_no_args(self):
32+
info = _pythoninfo.get_python_info()
33+
34+
self.assertEqual(info, INFO)
35+
36+
def test_current_python(self):
37+
info = _pythoninfo.get_python_info(sys.executable)
38+
39+
self.assertEqual(info, INFO)
40+
41+
def test_venv(self):
42+
self.maxDiff = 80 * 100
43+
venv, python = self.venv()
44+
expected = dict(INFO)
45+
expected['executable'] = python
46+
expected['prefix'] = venv
47+
expected['exec_prefix'] = venv
48+
49+
info = _pythoninfo.get_python_info(python)
50+
51+
self.assertEqual(info, expected)
52+
53+
54+
class GetPythonIDTests(unittest.TestCase):
55+
56+
INFO = {
57+
'executable': '/a/b/c/bin/spam-python',
58+
'version_str': '3.8.10 (default, May 5 2021, 03:01:07) \n[GCC 7.5.0]',
59+
'version_info': (3, 8, 10, 'final', 0),
60+
'api_version': 1013,
61+
'magic_number': b'U\r\r\n'.hex(),
62+
'implementation_name': 'cpython',
63+
'implementation_version': (3, 8, 10, 'final', 0),
64+
}
65+
ID = 'b14d92fd0e6f'
66+
67+
def test_no_prefix(self):
68+
pyid = _pythoninfo.get_python_id(self.INFO)
69+
70+
self.assertEqual(pyid, self.ID)
71+
72+
def test_true_prefix(self):
73+
pyid = _pythoninfo.get_python_id(self.INFO, prefix=True)
74+
75+
self.assertEqual(pyid, f'cpython3.8-{self.ID}')
76+
77+
def test_given_prefix(self):
78+
pyid = _pythoninfo.get_python_id(self.INFO, prefix='spam-')
79+
80+
self.assertEqual(pyid, f'spam-{self.ID}')
81+
82+
83+
class InspectPythonInstallTests(tests.Resources, unittest.TestCase):
84+
85+
BASE = getattr(sys, '_base_executable', None)
86+
87+
def test_info(self):
88+
info = INFO
89+
(base, isdev, isvenv,
90+
) = _pythoninfo.inspect_python_install(info)
91+
92+
if self.BASE:
93+
self.assertEqual(base, self.BASE)
94+
self.assertEqual(base, info['executable'])
95+
self.assertFalse(isdev)
96+
self.assertFalse(isvenv)
97+
98+
def test_normal(self):
99+
(base, isdev, isvenv,
100+
) = _pythoninfo.inspect_python_install()
101+
102+
self.assertEqual(base, sys.executable)
103+
self.assertFalse(isdev)
104+
self.assertFalse(isvenv)
105+
106+
def test_venv(self):
107+
base_expected = sys.executable
108+
_, python = self.venv(base_expected)
109+
(base, isdev, isvenv,
110+
) = _pythoninfo.inspect_python_install(python)
111+
112+
self.assertEqual(base, base_expected)
113+
self.assertFalse(isdev)
114+
self.assertTrue(isvenv)
115+
116+
@unittest.skip('needs platform-specific testing')
117+
def test_dev(self):
118+
# XXX
119+
raise NotImplementedError

0 commit comments

Comments
 (0)