Debian Codegraph @

3rdparty/catch2/tests/TestScripts/DiscoverTests/VerifyRegistration.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright Catch2 Authors
4 # Distributed under the Boost Software License, Version 1.0.
5 # (See accompanying file LICENSE.txt or copy at
6 # https://www.boost.org/LICENSE_1_0.txt)
7
8 # SPDX-License-Identifier: BSL-1.0
9
10 import os
11 import subprocess
12 import sys
13
14
15 def build_project(sources_dir, output_base_path, catch2_path):
16 build_dir = os.path.join(output_base_path, 'ctest-registration-test')
17 config_cmd = ['cmake',
18 '-B', build_dir,
19 '-S', sources_dir,
20 f'-DCATCH2_PATH={catch2_path}',
21 '-DCMAKE_BUILD_TYPE=Debug']
22
23 build_cmd = ['cmake',
24 '--build', build_dir,
25 '--config', 'Debug']
26
27 try:
28 subprocess.run(config_cmd,
29 capture_output = True,
30 check = True,
31 text = True)
32 subprocess.run(build_cmd,
33 capture_output = True,
34 check = True,
35 text = True)
36 except subprocess.CalledProcessError as err:
37 print('Error when building the test project')
38 print(f'cmd: {err.cmd}')
39 print(f'stderr: {err.stderr}')
40 print(f'stdout: {err.stdout}')
41 exit(3)
42
43 return build_dir
44
45
46
47 def get_test_names(build_path):
48 # For now we assume that Windows builds are done using MSBuild under
49 # Debug configuration. This means that we need to add "Debug" folder
50 # to the path when constructing it. On Linux, we don't add anything.
51 config_path = "Debug" if os.name == 'nt' else ""
52 full_path = os.path.join(build_path, config_path, 'tests')
53
54
55 cmd = [full_path, '--reporter', 'xml', '--list-tests']
56 result = subprocess.run(cmd,
57 capture_output = True,
58 check = True,
59 text = True)
60
61 import xml.etree.ElementTree as ET
62 root = ET.fromstring(result.stdout)
63 return [tc.text for tc in root.findall('TestCase/Name')]
64
65
66 def list_ctest_tests(build_path):
67 old_path = os.getcwd()
68 os.chdir(build_path)
69
70 cmd = ['ctest', '-C', 'debug', '--show-only=json-v1']
71 result = subprocess.run(cmd,
72 capture_output = True,
73 check = True,
74 text = True)
75 os.chdir(old_path)
76
77 import json
78
79 ctest_response = json.loads(result.stdout)
80 tests = ctest_response['tests']
81 test_names = []
82 for test in tests:
83 test_command = test['command']
84 # First part of the command is the binary, second is the filter.
85 # If there are less, registration has failed. If there are more,
86 # registration has changed and the script needs updating.
87 assert len(test_command) == 2
88 test_names.append(test_command[1])
89 test_name = test_command[1]
90
91 return test_names
92
93 def escape_catch2_test_name(name):
94 for char in ('\\', ',', '[', ']'):
95 name = name.replace(char, f"\\{char}")
96 return name
97
98 if __name__ == '__main__':
99 if len(sys.argv) != 3:
100 print(f'Usage: {sys.argv[0]} path-to-catch2-cml output-path')
101 exit(2)
102 catch2_path = sys.argv[1]
103 output_base_path = sys.argv[2]
104 sources_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
105
106 build_path = build_project(sources_dir, output_base_path, catch2_path)
107
108 catch_test_names = [escape_catch2_test_name(name) for name in get_test_names(build_path)]
109 ctest_test_names = list_ctest_tests(build_path)
110
111 mismatched = 0
112 for catch_test in catch_test_names:
113 if catch_test not in ctest_test_names:
114 print(f"Catch2 test '{catch_test}' not found in CTest")
115 mismatched += 1
116 for ctest_test in ctest_test_names:
117 if ctest_test not in catch_test_names:
118 print(f"CTest test '{ctest_test}' not found in Catch2")
119 mismatched += 1
120
121 if mismatched:
122 print(f"Found {mismatched} mismatched tests catch test names and ctest test commands!")
123 exit(1)