Add ctest for style, using clang-format
authorBrenden Blanco <bblanco@plumgrid.com>
Tue, 19 Apr 2016 18:42:49 +0000 (11:42 -0700)
committerBrenden Blanco <bblanco@plumgrid.com>
Tue, 19 Apr 2016 18:57:22 +0000 (11:57 -0700)
When user touches a file, that file will be fed to `git clang-format`,
and if the tool reports a new diff it will consider the test failed. The
files to check will be anything in the current workspace compared to
origin/master.

scripts/git-clang-format
scripts/style-check.sh [new file with mode: 0755]
tests/CMakeLists.txt

index 359c836..74310b7 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python2
 #
 #===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===#
 #
diff --git a/scripts/style-check.sh b/scripts/style-check.sh
new file mode 100755 (executable)
index 0000000..1e90b16
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# Runs clang-format on the files changed between HEAD and $1, which defaults to
+# origin/master.
+
+# to pick up git-clang-format from scripts/
+export PATH=$(dirname $0):$PATH
+
+CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
+GITREF=${1:-origin/master}
+
+if ! hash $CLANG_FORMAT 2> /dev/null; then
+  echo "Could not find clang-format tool" 1>&2
+  exit 1
+fi
+
+n=$(git clang-format $GITREF --binary $CLANG_FORMAT --style llvm --diff --quiet | wc -l)
+if [ $n -gt 0 ]; then
+  echo "git clang-format $GITREF --binary $CLANG_FORMAT --style llvm --diff"
+  echo
+  git clang-format $GITREF --binary $CLANG_FORMAT --style llvm --diff
+  echo
+  echo "clang-format returned non-empty diff, please fixup the style" 1>&2
+  exit 1
+fi
index ed9fdae..9051f89 100644 (file)
@@ -4,6 +4,8 @@ configure_file(wrapper.sh.in "${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh" @ONLY)
 
 set(TEST_WRAPPER ${CMAKE_CURRENT_BINARY_DIR}/wrapper.sh)
 
+add_test(NAME style-check COMMAND ${CMAKE_SOURCE_DIR}/scripts/style-check.sh)
+
 add_subdirectory(cc)
 add_subdirectory(python)
 add_subdirectory(lua)