lint-tests.py raw
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2018-2022 The Limenka developers
4 # Distributed under the MIT software license, see the accompanying
5 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7 """
8 Check the test suite naming conventions
9 """
10
11 import re
12 import subprocess
13 import sys
14
15
16 def grep_boost_fixture_test_suite():
17 command = [
18 "git",
19 "grep",
20 "-E",
21 r"^BOOST_FIXTURE_TEST_SUITE\(",
22 "--",
23 "src/test/**.cpp",
24 "src/wallet/test/**.cpp",
25 ]
26 return subprocess.check_output(command, text=True, encoding="utf8")
27
28
29 def check_matching_test_names(test_suite_list):
30 not_matching = [
31 x
32 for x in test_suite_list
33 if re.search(r"/(.*?)\.cpp:BOOST_FIXTURE_TEST_SUITE\(\1, .*\)", x) is None
34 ]
35 if len(not_matching) > 0:
36 not_matching = "\n".join(not_matching)
37 error_msg = (
38 "The test suite in file src/test/foo_tests.cpp should be named\n"
39 '"foo_tests". Please make sure the following test suites follow\n'
40 "that convention:\n\n"
41 f"{not_matching}\n"
42 )
43 print(error_msg)
44 return 1
45 return 0
46
47
48 def get_duplicates(input_list):
49 """
50 From https://stackoverflow.com/a/9835819
51 """
52 seen = set()
53 dupes = set()
54 for x in input_list:
55 if x in seen:
56 dupes.add(x)
57 else:
58 seen.add(x)
59 return dupes
60
61
62 def check_unique_test_names(test_suite_list):
63 output = [re.search(r"\((.*?),", x) for x in test_suite_list]
64 output = [x.group(1) for x in output if x is not None]
65 output = get_duplicates(output)
66 output = sorted(list(output))
67
68 if len(output) > 0:
69 output = "\n".join(output)
70 error_msg = (
71 "Test suite names must be unique. The following test suite names\n"
72 f"appear to be used more than once:\n\n{output}"
73 )
74 print(error_msg)
75 return 1
76 return 0
77
78
79 def main():
80 test_suite_list = grep_boost_fixture_test_suite().splitlines()
81 exit_code = check_matching_test_names(test_suite_list)
82 exit_code |= check_unique_test_names(test_suite_list)
83 sys.exit(exit_code)
84
85
86 if __name__ == "__main__":
87 main()
88