From 08169811dcddf9b28bf12441652936d53ad634d9 Mon Sep 17 00:00:00 2001 From: Jan Frenzel <jan.frenzel@tu-dresden.de> Date: Mon, 8 Nov 2021 14:55:19 +0100 Subject: [PATCH] Renamed grep-forbidden-words-testdoc.md to grep-forbidden-patterns.testdoc and allowed grep-forbidden-words.sh to test arbitrary files when used directly with a file argument. --- ...doc.md => grep-forbidden-patterns.testdoc} | 0 .../util/grep-forbidden-words.sh | 59 +++++++++++-------- .../util/test-grep-forbidden-words.sh | 8 ++- 3 files changed, 39 insertions(+), 28 deletions(-) rename doc.zih.tu-dresden.de/util/{grep-forbidden-words-testdoc.md => grep-forbidden-patterns.testdoc} (100%) diff --git a/doc.zih.tu-dresden.de/util/grep-forbidden-words-testdoc.md b/doc.zih.tu-dresden.de/util/grep-forbidden-patterns.testdoc similarity index 100% rename from doc.zih.tu-dresden.de/util/grep-forbidden-words-testdoc.md rename to doc.zih.tu-dresden.de/util/grep-forbidden-patterns.testdoc diff --git a/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh b/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh index a47314c36..e1a6846b2 100755 --- a/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh +++ b/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh @@ -39,6 +39,28 @@ function grepExceptions () { fi } +function checkFile(){ + f=$1 + echo "Check wording in file $f" + while IFS=$'\t' read -r flags pattern exceptionPatterns; do + while IFS=$'\t' read -r -a exceptionPatternsArray; do + if [ $silent = false ]; then + echo " Pattern: $pattern" + fi + grepflag= + case "$flags" in + "i") + grepflag=-i + ;; + esac + if grep -n $grepflag $color "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then + number_of_matches=`grep -n $grepflag $color "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" | wc -l` + ((cnt=cnt+$number_of_matches)) + fi + done <<< $exceptionPatterns + done <<< $ruleset +} + function usage () { echo "$0 [options]" echo "Search forbidden patterns in markdown files." @@ -95,32 +117,19 @@ fi echo "... $files ..." cnt=0 -for f in $files; do - if [ "${f: -3}" == ".md" -a -f "$f" ]; then - if (printf '%s\n' "${whitelist[@]}" | grep -xq $f); then - echo "Skip whitelisted file $f" - continue +if [[ ! -z $file ]]; then + checkFile $file +else + for f in $files; do + if [ "${f: -3}" == ".md" -a -f "$f" ]; then + if (printf '%s\n' "${whitelist[@]}" | grep -xq $f); then + echo "Skip whitelisted file $f" + continue + fi + checkFile $f fi - echo "Check wording in file $f" - while IFS=$'\t' read -r flags pattern exceptionPatterns; do - while IFS=$'\t' read -r -a exceptionPatternsArray; do - if [ $silent = false ]; then - echo " Pattern: $pattern" - fi - grepflag= - case "$flags" in - "i") - grepflag=-i - ;; - esac - if grep -n $grepflag $color "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then - number_of_matches=`grep -n $grepflag $color "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" | wc -l` - ((cnt=cnt+$number_of_matches)) - fi - done <<< $exceptionPatterns - done <<< $ruleset - fi -done + done +fi echo "" case $cnt in diff --git a/doc.zih.tu-dresden.de/util/test-grep-forbidden-words.sh b/doc.zih.tu-dresden.de/util/test-grep-forbidden-words.sh index 8efd92432..d5d13ffc4 100755 --- a/doc.zih.tu-dresden.de/util/test-grep-forbidden-words.sh +++ b/doc.zih.tu-dresden.de/util/test-grep-forbidden-words.sh @@ -1,11 +1,13 @@ #!/bin/bash -number_of_matches=$(bash ./doc.zih.tu-dresden.de/util/grep-forbidden-words.sh -f doc.zih.tu-dresden.de/util/grep-forbidden-words-testdoc.md -c -c | grep "Forbidden Patterns:" | sed -e 's/.*: //' | sed -e 's/ matches.*//') +expected_match_count=32 -if [ $number_of_matches -eq 32 ]; then +number_of_matches=$(bash ./doc.zih.tu-dresden.de/util/grep-forbidden-words.sh -f doc.zih.tu-dresden.de/util/grep-forbidden-patterns.testdoc -c -c | grep "Forbidden Patterns:" | sed -e 's/.*: //' | sed -e 's/ matches.*//') + +if [ $number_of_matches -eq $expected_match_count ]; then echo "Test OK" exit 0 else - echo "Test failed" + echo "Test failed: $expected_match_count matches expected, but only $number_of_matches found" exit 1 fi -- GitLab