valkey/utils/generate-unit-test-header.py
Mikhail Koviazin af811748e7
clang-format: set ColumnLimit to 0 and reformat (#1045)
This commit hopefully improves the formatting of the codebase by setting
ColumnLimit to 0 and hence stopping clang-format from trying to put as
much stuff in one line as possible.

This change enabled us to remove most of `clang-format off` directives
and fixed a bunch of lines that looked like this:

```c
#define KEY \
    VALUE /* comment */
```

Additionally, one pair of `clang-format off` / `clang-format on` had
`clang-format off` as the second comment and hence didn't enable the
formatting for the rest of the file. This commit addresses this issue as
well.

Please tell me if anything in the changes seem off. If everything is
fine, I will add this commit to `.git-blame-ignore-revs` later.

---------

Signed-off-by: Mikhail Koviazin <mikhail.koviazin@aiven.io>
2024-09-25 01:22:54 +02:00

60 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import re
UNIT_DIR = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/../src/unit')
TEST_FILE = UNIT_DIR + '/test_files.h'
TEST_PROTOTYPE = r'(int (test_[a-zA-Z0-9_]*)\(.*\)).*{'
if __name__ == '__main__':
with open(TEST_FILE, 'w') as output:
# Find each test file and collect the test names.
test_suites = []
for root, dirs, files in os.walk(UNIT_DIR):
for file in files:
file_path = UNIT_DIR + '/' + file
if not file.endswith('.c') or file == 'test_main.c':
continue
tests = []
with open(file_path, 'r') as f:
for line in f:
match = re.match(TEST_PROTOTYPE, line)
if match:
function = match.group(1)
test_name = match.group(2)
tests.append((test_name, function))
test_suites.append({'file': file, 'tests': tests})
test_suites.sort(key=lambda test_suite: test_suite['file'])
output.write("""/* Do not modify this file, it's automatically generated from utils/generate-unit-test-header.py */
typedef int unitTestProc(int argc, char **argv, int flags);
typedef struct unitTest {
char *name;
unitTestProc *proc;
} unitTest;
""")
# Write the headers for the functions
for test_suite in test_suites:
for test in test_suite['tests']:
output.write('{};\n'.format(test[1]))
output.write("\n")
# Create test suite lists
for test_suite in test_suites:
output.write('unitTest __{}[] = {{'.format(test_suite['file'].replace('.c', '_c')))
for test in test_suite['tests']:
output.write('{{"{}", {}}}, '.format(test[0], test[0]))
output.write('{NULL, NULL}};\n')
output.write("""
struct unitTestSuite {
char *filename;
unitTest *tests;
} unitTestSuite[] = {
""")
for test_suite in test_suites:
output.write(' {{"{0}", __{1}}},\n'.format(test_suite['file'], test_suite['file'].replace('.c', '_c')))
output.write('};\n')