Debian Codegraph @

Lib/test/test_tools/test_makefile.py

raw
1 """
2 Tests for `Makefile`.
3 """
4
5 import os
6 import unittest
7 from test import support
8 import sysconfig
9
10 MAKEFILE = sysconfig.get_makefile_filename()
11
12 if not support.check_impl_detail(cpython=True):
13 raise unittest.SkipTest('cpython only')
14 if not os.path.exists(MAKEFILE) or not os.path.isfile(MAKEFILE):
15 raise unittest.SkipTest('Makefile could not be found')
16
17
18 class TestMakefile(unittest.TestCase):
19 def list_test_dirs(self):
20 result = []
21 found_testsubdirs = False
22 with open(MAKEFILE, 'r', encoding='utf-8') as f:
23 for line in f:
24 if line.startswith('TESTSUBDIRS='):
25 found_testsubdirs = True
26 result.append(
27 line.removeprefix('TESTSUBDIRS=').replace(
28 '\\', '',
29 ).strip(),
30 )
31 continue
32 if found_testsubdirs:
33 if '\t' not in line:
34 break
35 result.append(line.replace('\\', '').strip())
36 return result
37
38 @unittest.skipUnless(support.TEST_MODULES_ENABLED, "requires test modules")
39 def test_makefile_test_folders(self):
40 test_dirs = self.list_test_dirs()
41 idle_test = 'idlelib/idle_test'
42 self.assertIn(idle_test, test_dirs)
43
44 used = set([idle_test])
45 for dirpath, dirs, files in os.walk(support.TEST_HOME_DIR):
46 dirname = os.path.basename(dirpath)
47 # Skip temporary dirs:
48 if dirname == '__pycache__' or dirname.startswith('.'):
49 dirs.clear() # do not process subfolders
50 continue
51
52 # Skip empty dirs (ignoring hidden files and __pycache__):
53 files = [
54 filename for filename in files
55 if not filename.startswith('.')
56 ]
57 dirs = [
58 dirname for dirname in dirs
59 if not dirname.startswith('.') and dirname != "__pycache__"
60 ]
61 if not dirs and not files:
62 continue
63
64 relpath = os.path.relpath(dirpath, support.STDLIB_DIR)
65 with self.subTest(relpath=relpath):
66 self.assertIn(
67 relpath,
68 test_dirs,
69 msg=(
70 f"{relpath!r} is not included in the Makefile's list "
71 "of test directories to install"
72 )
73 )
74 used.add(relpath)
75
76 # Don't check the wheel dir when Python is built --with-wheel-pkg-dir
77 if sysconfig.get_config_var('WHEEL_PKG_DIR'):
78 test_dirs.remove('test/wheeldata')
79 used.discard('test/wheeldata')
80
81 # Check that there are no extra entries:
82 unique_test_dirs = set(test_dirs)
83 self.assertSetEqual(unique_test_dirs, used)
84 self.assertEqual(len(test_dirs), len(unique_test_dirs))