Skip to content

Commit 073b4d9

Browse files
committed
add tests
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 9a2d952 commit 073b4d9

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import json
2+
import os
3+
import sys
4+
import sysconfig
5+
import string
6+
import unittest
7+
8+
9+
class FormatTestsBase:
10+
@property
11+
def contents(self):
12+
"""Install details file contents. Should be overriden by subclasses."""
13+
raise NotImplementedError
14+
15+
@property
16+
def data(self):
17+
"""Parsed install details file data, as a Python object."""
18+
return json.loads(self.contents)
19+
20+
def key(self, name):
21+
"""Helper to fetch subsection entries.
22+
23+
It takes the entry name, allowing the usage of a dot to separate the
24+
different subsection names (eg. specifying 'a.b.c' as the key will
25+
return the value of self.data['a']['b']['c']).
26+
"""
27+
value = self.data
28+
for part in name.split('.'):
29+
value = value[part]
30+
return value
31+
32+
def test_parse(self):
33+
self.data
34+
35+
def test_top_level_container(self):
36+
self.assertIsInstance(self.data, dict)
37+
for key, value in self.data.items():
38+
with self.subTest(section=key):
39+
self.assertIsInstance(value, dict)
40+
41+
def test_python_version(self):
42+
allowed_characters = string.digits + string.ascii_letters + '.'
43+
value = self.key('python.version')
44+
45+
self.assertLessEqual(set(value), set(allowed_characters))
46+
self.assertTrue(sys.version.startswith(value))
47+
48+
def test_python_version_parts(self):
49+
value = self.key('python.version_parts')
50+
51+
self.assertEqual(len(value), sys.version_info.n_fields)
52+
for part_name, part_value in value.items():
53+
with self.subTest(part=part_name):
54+
self.assertEqual(part_value, getattr(sys.version_info, part_name))
55+
56+
def test_python_executable(self):
57+
"""Test the python.executable entry.
58+
59+
The generic test wants the key to be missing. If your implementation
60+
provides a value for it, you should override this test.
61+
"""
62+
with self.assertRaises(KeyError):
63+
self.key('python.executable')
64+
65+
def test_python_stdlib(self):
66+
"""Test the python.stdlib entry.
67+
68+
The generic test wants the key to be missing. If your implementation
69+
provides a value for it, you should override this test.
70+
"""
71+
with self.assertRaises(KeyError):
72+
self.key('python.stdlib')
73+
74+
75+
needs_installed_python = unittest.skipIf(
76+
sysconfig.is_python_build(),
77+
'This test can only run in an installed Python',
78+
)
79+
80+
81+
@unittest.skipIf(os.name == 'nt', 'Feature only implemented on POSIX right now')
82+
class CPythonInstallDetailsFileTests(unittest.TestCase, FormatTestsBase):
83+
"""Test CPython's install details file implementation."""
84+
85+
@property
86+
def location(self):
87+
if sysconfig.is_python_build():
88+
dirname = sysconfig.get_config_var('projectbase')
89+
else:
90+
dirname = sysconfig.get_path('stdlib')
91+
return os.path.join(dirname, 'install-details.json')
92+
93+
@property
94+
def contents(self):
95+
with open(self.location, 'r') as f:
96+
return f.read()
97+
98+
@needs_installed_python
99+
def test_location(self):
100+
self.assertTrue(os.path.isfile(self.location))
101+
102+
# Override generic format tests with tests for our specific implemenation.
103+
104+
@needs_installed_python
105+
def test_python_executable(self):
106+
value = self.key('python.executable')
107+
108+
self.assertEqual(os.path.realpath(value), os.path.realpath(sys.executable))
109+
110+
@needs_installed_python
111+
def test_python_stdlib(self):
112+
value = self.key('python.stdlib')
113+
114+
try:
115+
stdlib = os.path.dirname(unittest.__path__)
116+
except AttributeError as exc:
117+
self.skipTest(str(exc))
118+
119+
self.assertEqual(os.path.realpath(value), os.path.realpath(stdlib))
120+
121+
122+
if __name__ == "__main__":
123+
unittest.main()

Tools/build/generate_install_details_file.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def main() -> None:
3636

3737
config = generic_info()
3838

39-
# move this to tests afterwards
40-
assert len(config['python']['version_parts']) == sys.version_info.n_fields
41-
4239
with open(args.output_path, 'w') as f:
4340
json.dump(config, f)
4441

0 commit comments

Comments
 (0)