Debian Codegraph @

Lib/test/test_build_details.py

raw ◀ ▶
1 Debian patch touch this file
1 import importlib
2 import json
3 import os
4 import os.path
5 import sys
6 import sysconfig
7 import string
8 import unittest
9 from pathlib import Path
10
11 from test.support import is_android, is_apple_mobile, is_wasm32
12
13 BASE_PATH = Path(
14 __file__, # Lib/test/test_build_details.py
15 '..', # Lib/test
16 '..', # Lib
17 '..', # <src/install dir>
18 ).resolve()
19 MODULE_PATH = BASE_PATH / 'Tools' / 'build' / 'generate-build-details.py'
20
21 try:
22 # Import "generate-build-details.py" as "generate_build_details"
23 spec = importlib.util.spec_from_file_location(
24 "generate_build_details", MODULE_PATH
25 )
26 generate_build_details = importlib.util.module_from_spec(spec)
27 sys.modules["generate_build_details"] = generate_build_details
28 spec.loader.exec_module(generate_build_details)
29 except (FileNotFoundError, ImportError):
30 generate_build_details = None
31
32
33 class FormatTestsBase:
34 @property
35 def contents(self):
36 """Install details file contents. Should be overriden by subclasses."""
37 raise NotImplementedError
38
39 @property
40 def data(self):
41 """Parsed install details file data, as a Python object."""
42 return json.loads(self.contents)
43
44 def key(self, name):
45 """Helper to fetch subsection entries.
46
47 It takes the entry name, allowing the usage of a dot to separate the
48 different subsection names (eg. specifying 'a.b.c' as the key will
49 return the value of self.data['a']['b']['c']).
50 """
51 value = self.data
52 for part in name.split('.'):
53 value = value[part]
54 return value
55
56 def test_top_level_container(self):
57 self.assertIsInstance(self.data, dict)
58 for key, value in self.data.items():
59 with self.subTest(key=key):
60 if key in ('schema_version', 'base_prefix', 'base_interpreter',
61 'platform'):
62 self.assertIsInstance(value, str)
63 elif key in ('language', 'implementation', 'abi', 'suffixes',
64 'libpython', 'c_api', 'arbitrary_data'):
65 self.assertIsInstance(value, dict)
66
67 def test_base_prefix(self):
68 self.assertIsInstance(self.key('base_prefix'), str)
69
70 def test_base_interpreter(self):
71 """Test the base_interpreter entry.
72
73 The generic test wants the key to be missing. If your implementation
74 provides a value for it, you should override this test.
75 """
76 with self.assertRaises(KeyError):
77 self.key('base_interpreter')
78
79 def test_platform(self):
80 self.assertEqual(self.key('platform'), sysconfig.get_platform())
81
82 def test_language_version(self):
83 allowed_characters = string.digits + string.ascii_letters + '.'
84 value = self.key('language.version')
85
86 self.assertLessEqual(set(value), set(allowed_characters))
87 self.assertTrue(sys.version.startswith(value))
88
89 def test_language_version_info(self):
90 value = self.key('language.version_info')
91
92 self.assertEqual(len(value), sys.version_info.n_fields)
93 for part_name, part_value in value.items():
94 with self.subTest(part=part_name):
95 sys_version_value = getattr(sys.version_info, part_name)
96 self.assertEqual(part_value, sys_version_value)
97
98 def test_implementation(self):
99 impl_ver = sys.implementation.version
100 for key, value in self.key('implementation').items():
101 with self.subTest(part=key):
102 if key == 'version':
103 self.assertEqual(len(value), len(impl_ver))
104 for part_name, part_value in value.items():
105 self.assertFalse(isinstance(sys.implementation.version, dict))
106 getattr(sys.implementation.version, part_name)
107 sys_implementation_value = getattr(impl_ver, part_name)
108 self.assertEqual(sys_implementation_value, part_value)
109 else:
110 self.assertEqual(getattr(sys.implementation, key), value)
111
112
113 needs_installed_python = unittest.skipIf(
114 sysconfig.is_python_build(),
115 'This test can only run in an installed Python',
116 )
117
118
119 @unittest.skipIf(os.name != 'posix', 'Feature only implemented on POSIX right now')
120 @unittest.skipIf(is_wasm32, 'Feature not available on WebAssembly builds')
121 class CPythonBuildDetailsTests(unittest.TestCase, FormatTestsBase):
122 """Test CPython's install details file implementation."""
123
124 @property
125 def location(self):
126 if sysconfig.is_python_build():
127 projectdir = sysconfig.get_config_var('projectbase')
128 pybuilddir = os.path.join(projectdir, 'pybuilddir.txt')
129 with open(pybuilddir, encoding='utf-8') as f:
130 dirname = os.path.join(projectdir, f.read())
131 else:
132 dirname = sysconfig.get_path('stdlib')
•133 return os.path.join(dirname, f'build-details.{sys.abiflags}.{sys.implementation._multiarch}.json')
134
135 @property
136 def contents(self):
137 with open(self.location, 'r', encoding='utf-8') as f:
138 return f.read()
139
140 @needs_installed_python
141 def test_location(self):
142 self.assertTrue(os.path.isfile(self.location))
143
144 # Override generic format tests with tests for our specific implemenation.
145
146 @needs_installed_python
147 @unittest.skipIf(
148 is_android or is_apple_mobile,
149 'Android and iOS run tests via a custom testbed method that changes sys.executable'
150 )
151 def test_base_interpreter(self):
152 value = self.key('base_interpreter')
153
154 # Skip check if installation is relocated
155 if sysconfig._installation_is_relocated():
156 self.skipTest("Installation is relocated")
157
158 self.assertEqual(os.path.realpath(value), os.path.realpath(sys.executable))
159
160 @needs_installed_python
161 @unittest.skipIf(
162 is_android or is_apple_mobile,
163 "Android and iOS run tests via a custom testbed method that doesn't ship headers"
164 )
165 def test_c_api(self):
166 value = self.key('c_api')
167
168 # Skip check if installation is relocated
169 if sysconfig._installation_is_relocated():
170 self.skipTest("Installation is relocated")
171
172 self.assertTrue(os.path.exists(os.path.join(value['headers'], 'Python.h')))
173 version = sysconfig.get_config_var('VERSION')
174 self.assertTrue(os.path.exists(os.path.join(value['pkgconfig_path'], f'python-{version}.pc')))
175
176
177 @unittest.skipIf(
178 generate_build_details is None,
179 "Failed to import generate-build-details"
180 )
181 @unittest.skipIf(os.name != 'posix', 'Feature only implemented on POSIX right now')
182 @unittest.skipIf(is_wasm32, 'Feature not available on WebAssembly builds')
183 class BuildDetailsRelativePathsTests(unittest.TestCase):
184 @property
185 def build_details_absolute_paths(self):
186 data = generate_build_details.generate_data(schema_version='1.0')
187 return json.loads(json.dumps(data))
188
189 @property
190 def build_details_relative_paths(self):
191 data = self.build_details_absolute_paths
192 generate_build_details.make_paths_relative(data, config_path=None)
193 return data
194
195 def test_round_trip(self):
196 data_abs_path = self.build_details_absolute_paths
197 data_rel_path = self.build_details_relative_paths
198
199 self.assertEqual(data_abs_path['base_prefix'],
200 data_rel_path['base_prefix'])
201
202 base_prefix = data_abs_path['base_prefix']
203
204 top_level_keys = ('base_interpreter',)
205 for key in top_level_keys:
206 self.assertEqual(key in data_abs_path, key in data_rel_path)
207 if key not in data_abs_path:
208 continue
209
210 abs_rel_path = os.path.join(base_prefix, data_rel_path[key])
211 abs_rel_path = os.path.normpath(abs_rel_path)
212 self.assertEqual(data_abs_path[key], abs_rel_path)
213
214 second_level_keys = (
215 ('libpython', 'dynamic'),
216 ('libpython', 'dynamic_stableabi'),
217 ('libpython', 'static'),
218 ('c_api', 'headers'),
219 ('c_api', 'pkgconfig_path'),
220
221 )
222 for part, key in second_level_keys:
223 self.assertEqual(part in data_abs_path, part in data_rel_path)
224 if part not in data_abs_path:
225 continue
226 self.assertEqual(key in data_abs_path[part],
227 key in data_rel_path[part])
228 if key not in data_abs_path[part]:
229 continue
230
231 abs_rel_path = os.path.join(base_prefix, data_rel_path[part][key])
232 abs_rel_path = os.path.normpath(abs_rel_path)
233 self.assertEqual(data_abs_path[part][key], abs_rel_path)
234
235
236 if __name__ == '__main__':
237 unittest.main()