test_cpp.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import json
  2. import os
  3. import subprocess
  4. import pytest
  5. import yaml
  6. import yatest
  7. from library.python.testing.style import rules
  8. import library.python.resource as lpr
  9. STYLE_CONFIG_JSON_14 = json.dumps(yaml.safe_load(lpr.find('resfs/file/config.clang-format')))
  10. STYLE_CONFIG_JSON_16 = json.dumps(yaml.safe_load(lpr.find('resfs/file/config.clang-format-16')))
  11. RES_FILE_PREFIX = '/cpp_style/files/'
  12. CHECKED_PATHS = list(lpr.iterkeys(RES_FILE_PREFIX, strip_prefix=True))
  13. def check_style(filename, actual_source):
  14. try:
  15. clang_format_binary = yatest.common.binary_path('contrib/libs/clang14/tools/clang-format/clang-format')
  16. config = STYLE_CONFIG_JSON_14
  17. except Exception:
  18. clang_format_binary = yatest.common.binary_path('contrib/libs/clang16/tools/clang-format/clang-format')
  19. config = STYLE_CONFIG_JSON_16
  20. command = [clang_format_binary, '-assume-filename=' + filename, '-style=' + config]
  21. styled_source = subprocess.check_output(command, input=actual_source)
  22. assert actual_source.decode() == styled_source.decode()
  23. @pytest.mark.parametrize('path', CHECKED_PATHS)
  24. def test_cpp_style(path):
  25. data = lpr.find(RES_FILE_PREFIX + path)
  26. skip_reason = rules.get_skip_reason(path, data, skip_links=False)
  27. if skip_reason:
  28. raise pytest.skip("style check is omitted: {}".format(skip_reason))
  29. else:
  30. check_style(os.path.basename(path), data)