1""" 2Python interpreter and environment tests. 3 4These need to be executed with the standard library unittest. 5Third party test runners such as pytest cannot be used because 6that would interfere with the tests. 7""" 8 9import platform 10import sys 11import unittest 12import site 13 14 15ENV = "@environment@" 16INTERPRETER = "@interpreter@" 17PYTHON_VERSION = "@pythonVersion@" 18 19IS_VIRTUALENV = @is_virtualenv@ 20IS_VENV = @is_venv@ 21IS_NIXENV = @is_nixenv@ 22IS_PYPY = platform.python_implementation() == "PyPy" 23 24 25class TestCasePython(unittest.TestCase): 26 27 @unittest.skipIf(IS_PYPY, "Executable is incorrect and needs to be fixed.") 28 def test_interpreter(self): 29 self.assertEqual(sys.executable, INTERPRETER) 30 31 @unittest.skipIf(IS_PYPY, "Prefix is incorrect and needs to be fixed.") 32 def test_prefix(self): 33 self.assertEqual(sys.prefix, ENV) 34 self.assertEqual(sys.prefix, sys.exec_prefix) 35 36 def test_site_prefix(self): 37 self.assertTrue(sys.prefix in site.PREFIXES) 38 39 @unittest.skipIf(IS_PYPY or sys.version_info.major==2, "Python 2 does not have base_prefix") 40 def test_base_prefix(self): 41 if IS_VENV or IS_NIXENV or IS_VIRTUALENV: 42 self.assertNotEqual(sys.prefix, sys.base_prefix) 43 else: 44 self.assertEqual(sys.prefix, sys.base_prefix) 45 46 @unittest.skipIf(sys.version_info.major==3, "sys.real_prefix is only set by virtualenv in case of Python 2.") 47 def test_real_prefix(self): 48 self.assertTrue(hasattr(sys, "real_prefix") == IS_VIRTUALENV) 49 50 def test_python_version(self): 51 self.assertTrue(platform.python_version().startswith(PYTHON_VERSION)) 52 53 54if __name__ == "__main__": 55 unittest.main()