summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorMichal Cmarada <mcmarada@cisco.com>2019-02-08 12:28:09 +0100
committerMichal Cmarada <mcmarada@cisco.com>2019-03-11 12:12:37 +0100
commitdc0453b3a97feb02e66e4888e8d071e8116aa806 (patch)
tree91ee70acc176c09ad394883d848d596aaa56c108 /scripts
parent2388db4347cf0c4a1032cb945db2fdb1011d74d7 (diff)
add test and checkstyle support
All tests are in "tests" directory. To run tests use: make test For verbose output use: export V=2;make test ARGS="-V" Test log file is generated in: Testing/Temporary/LastTest.log To run checkstyle for the current commit use: ./scripts/checkstyle.sh To run full check on all files use: ./scripts/checkstyle.sh --full Change-Id: Ic83b3dbd44d2a264d27935f4e65cf9e737cfc0a0 Signed-off-by: Michal Cmarada <mcmarada@cisco.com>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkstyle.sh151
1 files changed, 151 insertions, 0 deletions
diff --git a/scripts/checkstyle.sh b/scripts/checkstyle.sh
new file mode 100755
index 0000000..b129e62
--- /dev/null
+++ b/scripts/checkstyle.sh
@@ -0,0 +1,151 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2019 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+dir=$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )
+JVPP_DIR="$dir/../"
+EXIT_CODE=0
+FIX="0"
+FULL="0"
+CHECKSTYLED_FILES=""
+UNCHECKSTYLED_FILES=""
+
+# If the user provides --fix, then actually fix things
+# Note: this is meant for use outside of the CI Jobs, by users cleaning things up
+
+while true; do
+ case ${1} in
+ --fix)
+ FIX="1"
+ ;;
+ --full)
+ FULL="1"
+ ;;
+ esac
+ shift || break
+done
+
+if [[ "${FULL}" == "1" ]]; then
+ FILELIST=$(git ls-tree -r HEAD --name-only | grep -v checkstyle.sh)
+else
+ FILELIST=$(((git diff HEAD~1.. --name-only; git ls-files -m) | sort -u) | grep -v checkstyle.sh)
+fi
+
+# Check to make sure we have indent. Exit if we don't with an error message, but
+# don't *fail*.
+command -v indent > /dev/null
+if [ $? != 0 ]; then
+ echo "Cound not find required command \"indent\". Checkstyle aborted"
+ exit ${EXIT_CODE}
+fi
+indent --version
+
+# Check to make sure we have clang-format. Exit if we don't with an error message, but
+# don't *fail*.
+HAVE_CLANG_FORMAT=0
+command -v clang-format > /dev/null
+if [ $? != 0 ]; then
+ echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
+else
+ clang-format --version
+ x=$(echo "" | clang-format 2>&1)
+ if [[ "$x" == "" ]]; then
+ HAVE_CLANG_FORMAT=1
+ else
+ echo "Output produced while formatting empty file (expected empty string):"
+ echo "$x"
+ echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
+ fi
+fi
+
+cd ${JVPP_DIR}
+git status
+for i in ${FILELIST}; do
+ if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
+ grep -q "fd.io coding-style-patch-verification: ON" ${i}
+ if [ $? == 0 ]; then
+ EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
+ case ${EXTENSION} in
+ hpp|cpp|cc|hh)
+ CMD="clang-format"
+ if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
+ echo "C++ file detected. Abort. (missing clang-format)"
+ exit ${EXIT_CODE}
+ fi
+ ;;
+ *)
+ CMD="indent"
+ ;;
+ esac
+ CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
+ if [ ${FIX} == 0 ]; then
+ if [ "${CMD}" == "clang-format" ]
+ then
+ clang-format ${i} > ${i}.out2
+ else
+ indent ${i} -o ${i}.out1 > /dev/null 2>&1
+ indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
+ fi
+ # Remove trailing whitespace
+ sed -i -e 's/[[:space:]]*$//' ${i}.out2
+ diff -q ${i} ${i}.out2
+ else
+ if [ "${CMD}" == "clang-format" ]; then
+ clang-format -i ${i} > /dev/null 2>&1
+ else
+ indent ${i}
+ indent ${i}
+ fi
+ # Remove trailing whitespace
+ sed -i -e 's/[[:space:]]*$//' ${i}
+ fi
+ if [ $? != 0 ]; then
+ EXIT_CODE=1
+ echo
+ echo "Checkstyle failed for ${i}."
+ if [ "${CMD}" == "clang-format" ]; then
+ echo "Run clang-format as shown to fix the problem:"
+ echo "clang-format -i ${JVPP_DIR}${i}"
+ else
+ echo "Run indent (twice!) as shown to fix the problem:"
+ echo "indent ${JVPP_DIR}${i}"
+ echo "indent ${JVPP_DIR}${i}"
+ fi
+ fi
+ if [ -f ${i}.out1 ]; then
+ rm ${i}.out1
+ fi
+ if [ -f ${i}.out2 ]; then
+ rm ${i}.out2
+ fi
+ else
+ UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
+ fi
+ else
+ UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
+ fi
+done
+
+if [ ${EXIT_CODE} == 0 ]; then
+ echo "*******************************************************************"
+ echo "* JVPP CHECKSTYLE SUCCESSFULLY COMPLETED"
+ echo "*******************************************************************"
+else
+ echo "*******************************************************************"
+ echo "* JVPP CHECKSTYLE FAILED"
+ echo "* CONSULT FAILURE LOG ABOVE"
+ echo "* NOTE: Running './scripts/checkstyle.sh --fix' *MAY* fix the issue"
+ echo "*******************************************************************"
+fi
+exit ${EXIT_CODE}