From cfb117e45bfefba0afed24d3469a1b8e5616cb58 Mon Sep 17 00:00:00 2001 From: bergmann Date: Fri, 13 Dec 2019 11:04:49 +0100 Subject: [PATCH] * Fixed unit test groups * Added line counter tool --- projects/helloworld/test/CMakeLists.txt | 4 +- projects/libhelloworld/test/CMakeLists.txt | 4 +- projects/libinterface/test/CMakeLists.txt | 4 +- tools/count-lines | 164 +++++++++++++++++++++ 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100755 tools/count-lines diff --git a/projects/helloworld/test/CMakeLists.txt b/projects/helloworld/test/CMakeLists.txt index d308bae..1c66413 100644 --- a/projects/helloworld/test/CMakeLists.txt +++ b/projects/helloworld/test/CMakeLists.txt @@ -8,7 +8,7 @@ Find_Package ( Sanitizers QUIET ) # Test ############################################################################################ -Find_Package ( GTest ) +Find_Package ( GTest QUIET ) If ( NOT "${GTest_FOUND}" ) Return ( ) EndIf ( ) @@ -52,7 +52,7 @@ ForEach ( FILE IN LISTS HELLOWORLD_TEST_SOURCE_FILES # test If ( HAS_CMAKE_TESTS ) - Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} ) + Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} GROUP helloworld ) Else ( ) Add_Test ( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) EndIf ( ) diff --git a/projects/libhelloworld/test/CMakeLists.txt b/projects/libhelloworld/test/CMakeLists.txt index 9b15cbe..4410e9e 100644 --- a/projects/libhelloworld/test/CMakeLists.txt +++ b/projects/libhelloworld/test/CMakeLists.txt @@ -8,7 +8,7 @@ Find_Package ( Sanitizers QUIET ) # Test ############################################################################################ -Find_Package ( GTest ) +Find_Package ( GTest QUIET ) If ( NOT "${GTest_FOUND}" ) Return ( ) EndIf ( ) @@ -52,7 +52,7 @@ ForEach ( FILE IN LISTS LIBHELLOWORLD_TEST_SOURCE_FI # test If ( HAS_CMAKE_TESTS ) - Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} ) + Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} GROUP libhelloworld ) Else ( ) Add_Test ( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) EndIf ( ) diff --git a/projects/libinterface/test/CMakeLists.txt b/projects/libinterface/test/CMakeLists.txt index 32247ca..8bf2a98 100644 --- a/projects/libinterface/test/CMakeLists.txt +++ b/projects/libinterface/test/CMakeLists.txt @@ -8,7 +8,7 @@ Find_Package ( Sanitizers QUIET ) # Test ############################################################################################ -Find_Package ( GTest ) +Find_Package ( GTest QUIET ) If ( NOT "${GTest_FOUND}" ) Return ( ) EndIf ( ) @@ -52,7 +52,7 @@ ForEach ( FILE IN LISTS LIBINTERFACE_TEST_SOURCE_FIL # test If ( HAS_CMAKE_TESTS ) - Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} ) + Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} GROUP libinterface ) Else ( ) Add_Test ( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) EndIf ( ) diff --git a/tools/count-lines b/tools/count-lines new file mode 100755 index 0000000..d1d4ab8 --- /dev/null +++ b/tools/count-lines @@ -0,0 +1,164 @@ +#!/bin/bash + +ScriptFile=$(readlink -f "${BASH_SOURCE[0]}") +ScriptDir=$(dirname "${ScriptFile}") +SearchDir="${ScriptDir}" +FileExts="h;in;hpp;inl;c;cpp;make;cmake;txt;json;js;jsx;ts;tsx;html;tpl;css" +Verbose=0 + +function Log() +{ + printf "$@\n" +} + +function Error() +{ + >&2 printf "$@\n" +} + +function Panic() +{ + Error "$@" + exit 1 +} + +function PrintHelp() +{ + printf " +This is a tool to calculate the number of code lines within a certain folder. + +The tool looks for a 'CMakeLists.txt' or a 'Makefile' in the subfolders. +If such a file exists, the folder is treated as subproject. +Subprojects have their own counters. + +Parameters: + + -d|--dir + Directory to search in + (default ${SearchDir}). + + -e|--ext + File extension to look for + (default: ${FileExts}) + + -v|--verbose + Verbose output (Print lines of code for each file extension) + (default: false) + + -?|-h|--help + Print this help. + +" +} + +# Parse arguments +while [ $# -gt 0 ]; do + case $1 in + "-d" | "--dir" ) + if [ $# -lt 2 ]; then + Panic "Parameter $1 expects exactly one parameter!" + fi + SearchDir=$(readlink -m "$2") + shift + ;; + + "-e" | "--ext" ) + if [ $# -lt 2 ]; then + Panic "Parameter $1 expects exactly one parameter!" + fi + FileExts="$2" + shift + ;; + + "-v" | "--verbose" ) + Verbose=1 + ;; + + "-h" | "-?" | "--help" ) + PrintHelp + exit 0 + ;; + + * ) + Panic "Invalid or unknown parameter: $1" + ;; + esac + shift +done + +OLD_IFS=${IFS} +IFS=';' +read -ra Exts <<< "${FileExts}" +IFS=${OLD_IFS} + +Projects="$(find ${SearchDir} -type d -exec sh -c 'test -f "$1/CMakeLists.txt" || test -f "$1/Makefile"' -- {} \; -print -prune)" +First=1 +declare -A ExtLines +TotalLines=0 +echo "=======================================" +echo "= Lines of Code =" +echo "=======================================" +for Project in ${Projects[@]}; do + Name=${Project#"$SearchDir"} + + if [ "$Verbose" -eq 1 ]; then + if [ "$First" -eq 1 ]; then + echo "_______________________________________" + fi + printf "%-30s\n" "${Name:-root}:" + echo " \`+-----------------------------------" + fi + + ProjectLines=0 + for ext in "${Exts[@]}"; do + Lines=$(find ${Project} \ + -type f \ + -iname *.${ext} \ + 2>/dev/null \ + | xargs cat 2>/dev/null \ + | wc -l) + + if [ "$Verbose" -eq 1 ] && [ "$Lines" -gt 0 ]; then + printf " |%-26s %8d\n" "${ext}" ${Lines} + fi + + if [ "$First" -eq 1 ]; then + ExtLines[$ext]=$Lines + else + let ExtLines[$ext]=${ExtLines[$ext]}+${Lines} + fi + + let ProjectLines=${ProjectLines}+${Lines} + done + + if [ "$Verbose" -eq 1 ]; then + echo " +-----------------------------------" + printf " |%-26s %8d\n" "total:" ${ProjectLines} + else + printf "%-30s %8d\n" "${Name:-root}:" ${ProjectLines} + fi + + First=0 + + let TotalLines=${TotalLines}+${ProjectLines} +done + +echo "=======================================" + +if [ "$Verbose" -eq 1 ]; then + printf "%-30s\n" "total:" + echo " \`+-----------------------------------" + + for ext in "${Exts[@]}"; do + if [ "${ExtLines[$ext]}" -gt 0 ]; then + printf " |%-26s %8d\n" "${ext}" ${ExtLines[$ext]} + fi + done + + echo " +-----------------------------------" + printf " |%-26s %8d\n" "total:" ${TotalLines} +else + printf "%-30s %8d\n" "total:" ${TotalLines} +fi + +echo "======================================="