Skip to content

Commit 001e852

Browse files
botaneggsegoon
authored andcommitted
fix chaotic: format golden_test/output before comparing
Now we run clang-format on both sides of diff Its prevents problems due to clang-format version changes Fixes: <#1024> --- Pull Request resolved: <#1025> Co-authored-by: segoon <[email protected]> Co-authored-by: segoon <[email protected]> Co-authored-by: segoon <[email protected]> Co-authored-by: segoon <[email protected]> Co-authored-by: segoon <[email protected]> commit_hash:9d5ed72125bcb43a182439d6e5d0295a32436438
1 parent 91f82ae commit 001e852

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

.mapping.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
"chaotic/golden_tests/schemas/oneof/oneof.yaml":"taxi/uservices/userver/chaotic/golden_tests/schemas/oneof/oneof.yaml",
232232
"chaotic/golden_tests/schemas/oneofdiscriminator/oneofdiscriminator.yaml":"taxi/uservices/userver/chaotic/golden_tests/schemas/oneofdiscriminator/oneofdiscriminator.yaml",
233233
"chaotic/golden_tests/schemas/string/string.yaml":"taxi/uservices/userver/chaotic/golden_tests/schemas/string/string.yaml",
234+
"chaotic/golden_tests/scripts/diff-clang-format.py":"taxi/uservices/userver/chaotic/golden_tests/scripts/diff-clang-format.py",
234235
"chaotic/include/userver/chaotic/array.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/array.hpp",
235236
"chaotic/include/userver/chaotic/convert.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/convert.hpp",
236237
"chaotic/include/userver/chaotic/convert/to.hpp":"taxi/uservices/userver/chaotic/include/userver/chaotic/convert/to.hpp",

chaotic/golden_tests/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ userver_target_generate_chaotic(
1919
RELATIVE_TO "${CMAKE_CURRENT_SOURCE_DIR}"
2020
)
2121

22-
add_test(NAME chaotic-golden COMMAND # Diff returns 0 if files are the same, 1 if they differ
23-
diff -uNrpB "${CMAKE_CURRENT_SOURCE_DIR}/output" "${CMAKE_CURRENT_BINARY_DIR}/src"
22+
add_test(
23+
NAME chaotic-golden
24+
COMMAND "${USERVER_CHAOTIC_PYTEST_PYTHON_BINARY}"
25+
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/diff-clang-format.py
26+
--golden-dir "${CMAKE_CURRENT_SOURCE_DIR}/output"
27+
--generated-dir "${CMAKE_CURRENT_BINARY_DIR}/src"
2428
)
29+
2530
add_custom_target(
2631
update-golden-tests
2732
DEPENDS ${PROJECT_NAME}-chgen
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import argparse
2+
import os
3+
import shutil
4+
import subprocess
5+
import sys
6+
import tempfile
7+
8+
9+
def is_binary_available(binary_name: str) -> bool:
10+
try:
11+
subprocess.checked_call(['which', binary_name], stdout=subprocess.DEVNULL)
12+
return True
13+
except subprocess.CalledProcessError:
14+
return False
15+
16+
17+
def parse_args():
18+
parser = argparse.ArgumentParser(description='Compare formatted source files.')
19+
parser.add_argument(
20+
'--golden-dir', required=True, help='Golden directory (e.g., ${CMAKE_CURRENT_SOURCE_DIR}/output)'
21+
)
22+
parser.add_argument(
23+
'--generated-dir', required=True, help='Generated directory (e.g., ${CMAKE_CURRENT_BINARY_DIR}/src)'
24+
)
25+
return parser.parse_args()
26+
27+
28+
def walk_xpp(path: str) -> list[str]:
29+
result = []
30+
31+
extensions = {'.hpp', '.cpp', '.ipp'}
32+
for root, _, files in os.walk(path):
33+
for file in files:
34+
if file.lower().endswith(extensions):
35+
file_path = os.path.join(root, file)
36+
result.append(file_path)
37+
return result
38+
39+
40+
def main():
41+
args = parse_args()
42+
43+
if not is_binary_available('clang-format'):
44+
print('Error: clang-format is not available in PATH', file=sys.stderr)
45+
sys.exit(1)
46+
47+
if not is_binary_available('diff'):
48+
print('Error: diff is not available in PATH', file=sys.stderr)
49+
sys.exit(1)
50+
51+
with tempfile.TemporaryDirectory() as tmpdir:
52+
golden_copy = os.path.join(tmpdir, 'golden')
53+
generated_copy = os.path.join(tmpdir, 'generated')
54+
shutil.copytree(args.golden_dir, golden_copy)
55+
shutil.copytree(args.generated_dir, generated_copy)
56+
57+
subprocess.check_call(['clang-format', '-i'] + walk_xpp(tmpdir))
58+
59+
result = subprocess.run(['diff', '-uNrpB', golden_copy, generated_copy], capture_output=True, text=True)
60+
61+
if result.returncode != 0:
62+
print(result.stdout)
63+
print(result.stderr, file=sys.stderr)
64+
65+
# Diff returns 0 if files are the same, 1 if they differ
66+
sys.exit(result.returncode)
67+
68+
69+
if __name__ == '__main__':
70+
main()

0 commit comments

Comments
 (0)