| 
 | 1 | +import json  | 
 | 2 | +import os  | 
 | 3 | +import sys  | 
 | 4 | +import sysconfig  | 
 | 5 | +import string  | 
 | 6 | +import unittest  | 
 | 7 | + | 
 | 8 | +from test.support import is_android, is_apple_mobile, is_emscripten, is_wasi  | 
 | 9 | + | 
 | 10 | + | 
 | 11 | +class FormatTestsBase:  | 
 | 12 | +    @property  | 
 | 13 | +    def contents(self):  | 
 | 14 | +        """Install details file contents. Should be overriden by subclasses."""  | 
 | 15 | +        raise NotImplementedError  | 
 | 16 | + | 
 | 17 | +    @property  | 
 | 18 | +    def data(self):  | 
 | 19 | +        """Parsed install details file data, as a Python object."""  | 
 | 20 | +        return json.loads(self.contents)  | 
 | 21 | + | 
 | 22 | +    def key(self, name):  | 
 | 23 | +        """Helper to fetch subsection entries.  | 
 | 24 | +
  | 
 | 25 | +        It takes the entry name, allowing the usage of a dot to separate the  | 
 | 26 | +        different subsection names (eg. specifying 'a.b.c' as the key will  | 
 | 27 | +        return the value of self.data['a']['b']['c']).  | 
 | 28 | +        """  | 
 | 29 | +        value = self.data  | 
 | 30 | +        for part in name.split('.'):  | 
 | 31 | +            value = value[part]  | 
 | 32 | +        return value  | 
 | 33 | + | 
 | 34 | +    def test_parse(self):  | 
 | 35 | +        self.data  | 
 | 36 | + | 
 | 37 | +    def test_top_level_container(self):  | 
 | 38 | +        self.assertIsInstance(self.data, dict)  | 
 | 39 | +        for key, value in self.data.items():  | 
 | 40 | +            with self.subTest(key=key):  | 
 | 41 | +                if key in ('schema_version', 'base_prefix', 'base_interpreter', 'platform'):  | 
 | 42 | +                    self.assertIsInstance(value, str)  | 
 | 43 | +                elif key in ('language', 'implementation', 'abi', 'suffixes', 'libpython', 'c_api', 'arbitrary_data'):  | 
 | 44 | +                    self.assertIsInstance(value, dict)  | 
 | 45 | + | 
 | 46 | +    def test_base_prefix(self):  | 
 | 47 | +        self.assertIsInstance(self.key('base_prefix'), str)  | 
 | 48 | + | 
 | 49 | +    def test_base_interpreter(self):  | 
 | 50 | +        """Test the base_interpreter entry.  | 
 | 51 | +
  | 
 | 52 | +        The generic test wants the key to be missing. If your implementation  | 
 | 53 | +        provides a value for it, you should override this test.  | 
 | 54 | +        """  | 
 | 55 | +        with self.assertRaises(KeyError):  | 
 | 56 | +            self.key('base_interpreter')  | 
 | 57 | + | 
 | 58 | +    def test_platform(self):  | 
 | 59 | +        self.assertEqual(self.key('platform'), sysconfig.get_platform())  | 
 | 60 | + | 
 | 61 | +    def test_language_version(self):  | 
 | 62 | +        allowed_characters = string.digits + string.ascii_letters + '.'  | 
 | 63 | +        value = self.key('language.version')  | 
 | 64 | + | 
 | 65 | +        self.assertLessEqual(set(value), set(allowed_characters))  | 
 | 66 | +        self.assertTrue(sys.version.startswith(value))  | 
 | 67 | + | 
 | 68 | +    def test_language_version_info(self):  | 
 | 69 | +        value = self.key('language.version_info')  | 
 | 70 | + | 
 | 71 | +        self.assertEqual(len(value), sys.version_info.n_fields)  | 
 | 72 | +        for part_name, part_value in value.items():  | 
 | 73 | +            with self.subTest(part=part_name):  | 
 | 74 | +                self.assertEqual(part_value, getattr(sys.version_info, part_name))  | 
 | 75 | + | 
 | 76 | +    def test_implementation(self):  | 
 | 77 | +        for key, value in self.key('implementation').items():  | 
 | 78 | +            with self.subTest(part=key):  | 
 | 79 | +                if key == 'version':  | 
 | 80 | +                    self.assertEqual(len(value), len(sys.implementation.version))  | 
 | 81 | +                    for part_name, part_value in value.items():  | 
 | 82 | +                        self.assertEqual(getattr(sys.implementation.version, part_name), part_value)  | 
 | 83 | +                else:  | 
 | 84 | +                    self.assertEqual(getattr(sys.implementation, key), value)  | 
 | 85 | + | 
 | 86 | + | 
 | 87 | +needs_installed_python = unittest.skipIf(  | 
 | 88 | +    sysconfig.is_python_build(),  | 
 | 89 | +    'This test can only run in an installed Python',  | 
 | 90 | +)  | 
 | 91 | + | 
 | 92 | + | 
 | 93 | +@unittest.skipIf(os.name != 'posix', 'Feature only implemented on POSIX right now')  | 
 | 94 | +@unittest.skipIf(is_wasi or is_emscripten, 'Feature not available on WebAssembly builds')  | 
 | 95 | +class CPythonBuildDetailsTests(unittest.TestCase, FormatTestsBase):  | 
 | 96 | +    """Test CPython's install details file implementation."""  | 
 | 97 | + | 
 | 98 | +    @property  | 
 | 99 | +    def location(self):  | 
 | 100 | +        if sysconfig.is_python_build():  | 
 | 101 | +            projectdir = sysconfig.get_config_var('projectbase')  | 
 | 102 | +            with open(os.path.join(projectdir, 'pybuilddir.txt')) as f:  | 
 | 103 | +                dirname = os.path.join(projectdir, f.read())  | 
 | 104 | +        else:  | 
 | 105 | +            dirname = sysconfig.get_path('stdlib')  | 
 | 106 | +        return os.path.join(dirname, 'build-details.json')  | 
 | 107 | + | 
 | 108 | +    @property  | 
 | 109 | +    def contents(self):  | 
 | 110 | +        with open(self.location, 'r') as f:  | 
 | 111 | +            return f.read()  | 
 | 112 | + | 
 | 113 | +    @needs_installed_python  | 
 | 114 | +    def test_location(self):  | 
 | 115 | +        self.assertTrue(os.path.isfile(self.location))  | 
 | 116 | + | 
 | 117 | +    # Override generic format tests with tests for our specific implemenation.  | 
 | 118 | + | 
 | 119 | +    @needs_installed_python  | 
 | 120 | +    @unittest.skipIf(is_android or is_apple_mobile, 'Android and iOS run tests via a custom testbed method that changes sys.executable')  | 
 | 121 | +    def test_base_interpreter(self):  | 
 | 122 | +        value = self.key('base_interpreter')  | 
 | 123 | + | 
 | 124 | +        self.assertEqual(os.path.realpath(value), os.path.realpath(sys.executable))  | 
 | 125 | + | 
 | 126 | + | 
 | 127 | +if __name__ == '__main__':  | 
 | 128 | +    unittest.main()  | 
0 commit comments