From 581fc7d006b998f43da04569cbc19e24bfc679f2 Mon Sep 17 00:00:00 2001 From: Brenden Blanco Date: Tue, 19 Apr 2016 11:42:49 -0700 Subject: [PATCH] Add ctest for style, using clang-format 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 | 2 +- scripts/style-check.sh | 25 +++++++++++++++++++++++++ tests/CMakeLists.txt | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100755 scripts/style-check.sh diff --git a/scripts/git-clang-format b/scripts/git-clang-format index 359c836..74310b7 100755 --- a/scripts/git-clang-format +++ b/scripts/git-clang-format @@ -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 index 0000000..1e90b16 --- /dev/null +++ b/scripts/style-check.sh @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ed9fdae..9051f89 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) -- 2.7.4