| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Checks that all of the "catch_foo_all.hpp" headers include all subheaders. |
| 5 |
|
| 6 |
The logic is simple: given a folder, e.g. `catch2/matchers`, then the |
| 7 |
ccorresponding header is called `catch_matchers_all.hpp` and contains |
| 8 |
* all headers in `catch2/matchers`, |
| 9 |
* all headers in `catch2/matchers/{internal, detail}`, |
| 10 |
* all convenience catch_matchers_*_all.hpp headers from any non-internal subfolders |
| 11 |
|
| 12 |
The top level header is called `catch_all.hpp`. |
| 13 |
""" |
| 14 |
|
| 15 |
internal_dirs = ['detail', 'internal'] |
| 16 |
|
| 17 |
from scriptCommon import catchPath |
| 18 |
from glob import glob |
| 19 |
from pprint import pprint |
| 20 |
import os |
| 21 |
import re |
| 22 |
|
| 23 |
def normalized_path(path): |
| 24 |
"""Replaces \ in paths on Windows with /""" |
| 25 |
return path.replace('\\', '/') |
| 26 |
|
| 27 |
def normalized_paths(paths): |
| 28 |
"""Replaces \ with / in every path""" |
| 29 |
return [normalized_path(path) for path in paths] |
| 30 |
|
| 31 |
source_path = catchPath + '/src/catch2' |
| 32 |
source_path = normalized_path(source_path) |
| 33 |
include_parser = re.compile(r'#include <(catch2/.+\.hpp)>') |
| 34 |
|
| 35 |
errors_found = False |
| 36 |
|
| 37 |
def headers_in_folder(folder): |
| 38 |
return glob(folder + '/*.hpp') |
| 39 |
|
| 40 |
def folders_in_folder(folder): |
| 41 |
return [x for x in os.scandir(folder) if x.is_dir()] |
| 42 |
|
| 43 |
def collated_includes(folder): |
| 44 |
base = headers_in_folder(folder) |
| 45 |
for subfolder in folders_in_folder(folder): |
| 46 |
if subfolder.name in internal_dirs: |
| 47 |
base.extend(headers_in_folder(subfolder.path)) |
| 48 |
else: |
| 49 |
base.append(subfolder.path + '/catch_{}_all.hpp'.format(subfolder.name)) |
| 50 |
return normalized_paths(sorted(base)) |
| 51 |
|
| 52 |
def includes_from_file(header): |
| 53 |
includes = [] |
| 54 |
with open(header, 'r', encoding = 'utf-8') as file: |
| 55 |
for line in file: |
| 56 |
if not line.startswith('#include'): |
| 57 |
continue |
| 58 |
match = include_parser.match(line) |
| 59 |
if match: |
| 60 |
includes.append(match.group(1)) |
| 61 |
return normalized_paths(includes) |
| 62 |
|
| 63 |
def normalize_includes(includes): |
| 64 |
"""Returns """ |
| 65 |
return [include[len(catchPath)+5:] for include in includes] |
| 66 |
|
| 67 |
def get_duplicates(xs): |
| 68 |
seen = set() |
| 69 |
duplicated = [] |
| 70 |
for x in xs: |
| 71 |
if x in seen: |
| 72 |
duplicated.append(x) |
| 73 |
seen.add(x) |
| 74 |
return duplicated |
| 75 |
|
| 76 |
def verify_convenience_header(folder): |
| 77 |
""" |
| 78 |
Performs the actual checking of convenience header for specific folder. |
| 79 |
Checks that |
| 80 |
1) The header even exists |
| 81 |
2) That all includes in the header are sorted |
| 82 |
3) That there are no duplicated includes |
| 83 |
4) That all includes that should be in the header are actually present in the header |
| 84 |
5) That there are no superfluous includes that should not be in the header |
| 85 |
""" |
| 86 |
global errors_found |
| 87 |
|
| 88 |
path = normalized_path(folder.path) |
| 89 |
|
| 90 |
assert path.startswith(source_path), '{} does not start with {}'.format(path, source_path) |
| 91 |
stripped_path = path[len(source_path) + 1:] |
| 92 |
path_pieces = stripped_path.split('/') |
| 93 |
|
| 94 |
if path == source_path: |
| 95 |
header_name = 'catch_all.hpp' |
| 96 |
else: |
| 97 |
header_name = 'catch_{}_all.hpp'.format('_'.join(path_pieces)) |
| 98 |
|
| 99 |
|
| 100 |
full_path = path + '/' + header_name |
| 101 |
if not os.path.isfile(full_path): |
| 102 |
errors_found = True |
| 103 |
print('Missing convenience header: {}'.format(full_path)) |
| 104 |
return |
| 105 |
file_incs = includes_from_file(path + '/' + header_name) |
| 106 |
|
| 107 |
if sorted(file_incs) != file_incs: |
| 108 |
errors_found = True |
| 109 |
print("'{}': Includes are not in sorted order!".format(header_name)) |
| 110 |
|
| 111 |
|
| 112 |
duplicated = get_duplicates(file_incs) |
| 113 |
for duplicate in duplicated: |
| 114 |
errors_found = True |
| 115 |
print("'{}': Duplicated include: '{}'".format(header_name, duplicate)) |
| 116 |
|
| 117 |
target_includes = normalize_includes(collated_includes(path)) |
| 118 |
|
| 119 |
target_includes = [x for x in target_includes if header_name not in x] |
| 120 |
|
| 121 |
file_incs_set = set(file_incs) |
| 122 |
for include in target_includes: |
| 123 |
if (include not in file_incs_set and |
| 124 |
include != 'catch2/internal/catch_windows_h_proxy.hpp'): |
| 125 |
errors_found = True |
| 126 |
print("'{}': missing include '{}'".format(header_name, include)) |
| 127 |
|
| 128 |
|
| 129 |
desired_set = set(target_includes) |
| 130 |
for include in file_incs: |
| 131 |
if include not in desired_set: |
| 132 |
errors_found = True |
| 133 |
print("'{}': superfluous include '{}'".format(header_name, include)) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
def walk_source_folders(current): |
| 138 |
verify_convenience_header(current) |
| 139 |
for folder in folders_in_folder(current.path): |
| 140 |
fname = folder.name |
| 141 |
if fname not in internal_dirs: |
| 142 |
walk_source_folders(folder) |
| 143 |
|
| 144 |
|
| 145 |
base_dir = [x for x in os.scandir(catchPath + '/src') if x.name == 'catch2'] |
| 146 |
walk_source_folders(base_dir[0]) |
| 147 |
|
| 148 |
|
| 149 |
if not errors_found: |
| 150 |
print('Everything ok') |
| 151 |
exit(errors_found) |