scripts: Add wrapper for clang-format
BUG=b:244479571
TEST=./run_tests --network scripts/clang_format_unittest.py lib/cache_unittest.py
TEST=./clang-format --version
TEST=ls ../.cache/chromium-clang-format/
Change-Id: I016552f5c266ea759ef7c17387a1a030a36d456d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4279663
Reviewed-by: Brian Norris <briannorris@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Li-Yu Yu <aaronyu@google.com>
Commit-Queue: Li-Yu Yu <aaronyu@google.com>
diff --git a/scripts/clang_format_unittest.py b/scripts/clang_format_unittest.py
new file mode 100644
index 0000000..c3fc02d
--- /dev/null
+++ b/scripts/clang_format_unittest.py
@@ -0,0 +1,35 @@
+# Copyright 2023 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Tests the clang-format wrapper."""
+
+from chromite.lib import cros_build_lib
+from chromite.lib import cros_test_lib
+from chromite.scripts import clang_format
+
+
+@cros_test_lib.pytestmark_network_test
+class ClangFormatTest(cros_test_lib.TestCase):
+ """Tests the clang-format wrapper."""
+
+ def testClangFormatVersion(self):
+ """Check that clang-format can return the version."""
+ with clang_format.ClangFormat() as prog:
+ result = cros_build_lib.run(
+ [prog, "--version"],
+ encoding="utf-8",
+ capture_output=True,
+ )
+ self.assertStartsWith(result.stdout, "clang-format version ")
+
+ def testClangFormatStdin(self):
+ """Check clang-format can format from stdin."""
+ with clang_format.ClangFormat() as prog:
+ result = cros_build_lib.run(
+ [prog, "--style={BasedOnStyle: Chromium}"],
+ input="int main(){ }",
+ encoding="utf-8",
+ capture_output=True,
+ )
+ self.assertEqual(result.stdout, "int main() {}")