|
| 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