Skip to content
Snippets Groups Projects
Commit 57e9199d authored by Jan Frenzel's avatar Jan Frenzel
Browse files

Merge branch 'fix-check-script' into 'preview'

Refactor and fix check-code-style and pre-commit script

See merge request !703
parents 7b2f6c3c 177dbf52
No related branches found
No related tags found
2 merge requests!719Automated merge from preview to main,!703Refactor and fix check-code-style and pre-commit script
......@@ -3,8 +3,6 @@
"first-heading-h1": true,
"header-increment": true,
"heading-style": { "style": "atx" },
"no-missing-space-atx": true,
"no-multiple-space-atx": true,
"blanks-around-headings": true,
"no-emphasis-as-heading": true,
"first-line-heading": true,
......
......@@ -30,9 +30,6 @@ EOF
}
function pattern_matches() {
local any_fails
any_fails=false
local myfile
myfile=$1
......@@ -49,21 +46,20 @@ function pattern_matches() {
test_res_count=0
if [[ "${ext}" == "sh" ]]; then
# If shell script is provided
test_res_count=$(grep -cP "${pattern}" $myfile || true)
if [[ "${test_res_count}" -gt "0" ]]; then
if grep -qP "${pattern}" "$myfile"; then
echo -e "[WARNING] ${warning}"
echo "[WARNING] This coding style was not used for following lines in file $(realpath ${myfile}):"
grep -nP "${pattern}" $myfile
echo "[WARNING] This coding style was not used for following lines in file $(realpath "${myfile}"):"
grep -nP "${pattern}" "$myfile"
echo ""
any_fails=true
return 0
fi
elif [[ "${ext}" == "md" ]]; then
# If markdown file is provided
# Check if the code snippet exists in the markdown file
if sed -n '/^```bash$/,/^```$/p' $myfile | grep -qv '```'; then
if sed -n '/^```bash$/,/^```$/p' "$myfile" | grep -qv '```'; then
# Extracting code snippet within ```bash ... ```
local test_string
test_string=$(sed -n '/^```bash$/,/^```$/p' $myfile | grep -v '```')
test_string=$(sed -n '/^```bash$/,/^```$/p' "$myfile" | grep -v '```')
# Check the exit code of pattern match
if echo "${test_string}" | grep -qP "${pattern}"; then
......@@ -71,24 +67,22 @@ function pattern_matches() {
fi
if [[ "${test_res_count}" -gt "0" ]]; then
echo -e "[WARNING] ${warning}"
echo "[WARNING] This coding style was not used for following lines in file $(realpath ${myfile}):"
echo "[WARNING] This coding style was not used for following lines in file $(realpath "${myfile}"):"
grep -no -F "$(echo "${test_string}" | grep -P "${pattern}")" "${myfile}"
echo ""
any_fails=true
return 0
fi
fi
fi
if [[ "${any_fails}" == true ]]; then
return 0
fi
# Reached here: Return false/non-zero, i.e. no error/match found
return 1
}
# -----------------------------------------------------------------------------
# Functions End
scriptpath=${BASH_SOURCE[0]}
basedir=`dirname "${scriptpath}"`
basedir=`dirname "${basedir}"`
basedir=$(dirname "${scriptpath}")
basedir=$(dirname "${basedir}")
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
......@@ -101,7 +95,7 @@ if [[ $# -eq 1 ]]; then
;;
-a | --all)
echo "Checking in all files."
files=$(find $basedir -name '*.md' -o -name '*.sh')
files=$(find "$basedir" -name '*.md' -o -name '*.sh')
;;
*)
files="$1"
......@@ -109,7 +103,7 @@ if [[ $# -eq 1 ]]; then
esac
elif [[ $# -eq 0 ]]; then
echo "Search in git-changed files."
files=`git diff --name-only "$(git merge-base HEAD "$branch")" | grep -e '.md$' -e '.sh$' || true`
files=$(git diff --name-only "$(git merge-base HEAD "$branch")" | grep -e '.md$' -e '.sh$' || true)
else
usage
fi
......@@ -118,7 +112,7 @@ any_fails=false
for file in $files; do
# Skip the check of this current ($0) script.
if [[ "$(echo "${file}" | grep -cP "check-code-style.sh$")" -gt "0" ]]; then
if echo "${file}" | grep -qP "check-code-style.sh$"; then
continue
fi
......@@ -193,5 +187,3 @@ done
if [[ "${any_fails}" == true ]]; then
exit 1
fi
#------------------------------------------------------------------------------
# Script End
#!/bin/bash
function testPath(){
path_to_test=doc.zih.tu-dresden.de/docs/$1
test -f "$path_to_test" || echo $path_to_test does not exist
path_to_test="doc.zih.tu-dresden.de/docs/$1"
test -f "$path_to_test" || echo "$path_to_test does not exist"
}
if ! `docker image inspect hpc-compendium:latest > /dev/null 2>&1`
then
function runInDocker(){
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium "$@"
}
function runInDockerDocOnly(){
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)"/doc.zih.tu-dresden.de,target=/docs,type=bind hpc-compendium "$@"
}
if ! docker image inspect hpc-compendium:latest > /dev/null 2>&1; then
echo Container not built, building...
docker build -t hpc-compendium .
fi
......@@ -14,90 +20,77 @@ export -f testPath
exit_ok=yes
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
if [ -f "$GIT_DIR/MERGE_HEAD" ]
then
source_hash=`git merge-base HEAD "$branch"`
if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
source_hash=$(git merge-base HEAD "$branch")
else
source_hash=`git rev-parse HEAD`
source_hash=$(git rev-parse HEAD)
fi
#Remove everything except lines beginning with --- or +++
files=`git diff $source_hash | sed -E -n 's#^(---|\+\+\+) ((/|./)[^[:space:]]+)$#\2#p'`
files=$(git diff "$source_hash" | sed -E -n 's#^(---|\+\+\+) ((/|./)[^[:space:]]+)$#\2#p')
#Assume that we have pairs of lines (starting with --- and +++).
while read oldfile; do
read newfile
while read -r; do
read -r newfile
if [ "$newfile" == doc.zih.tu-dresden.de/mkdocs.yml ]
then
if [ "$newfile" == doc.zih.tu-dresden.de/mkdocs.yml ]; then
echo Testing "$newfile"
sed -n '/^ *- /s#.*: \([A-Za-z_/]*.md\).*#\1#p' doc.zih.tu-dresden.de/mkdocs.yml | xargs -L1 -I {} bash -c "testPath '{}'"
if [ $? -ne 0 ]
then
if ! sed -n '/^ *- /s#.*: \([A-Za-z_/]*.md\).*#\1#p' doc.zih.tu-dresden.de/mkdocs.yml | xargs -L1 -I {} bash -c "testPath '{}'"; then
echo -e "\tFailed"
exit_ok=no
fi
elif [[ "$newfile" =~ ^b/doc.zih.tu-dresden.de/(.*.md)$ ]]
then
filepattern=${BASH_REMATCH[1]}
elif [[ "$newfile" =~ ^b/doc.zih.tu-dresden.de/(.*.md)$ ]]; then
doc_filepath="${BASH_REMATCH[1]}"
echo "Linting..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)"/doc.zih.tu-dresden.de,target=/docs,type=bind hpc-compendium markdownlint $filepattern
if [ $? -ne 0 ]
then
if ! runInDockerDocOnly markdownlint "$doc_filepath"; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Checking links..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)"/doc.zih.tu-dresden.de,target=/docs,type=bind hpc-compendium markdown-link-check $filepattern
if [ $? -ne 0 ]
then
if ! runInDockerDocOnly markdown-link-check "$doc_filepath"; then
echo -e "\tFailed"
exit_ok=no
fi
fi
done <<< "$files"
echo "Testing syntax of bash files..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-bash-syntax.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/check-bash-syntax.sh; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Spell-checking..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-spelling.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/check-spelling.sh; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Forbidden words checking..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/grep-forbidden-patterns.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/grep-forbidden-patterns.sh; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Looking for empty files..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-empty-page.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/check-empty-page.sh; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Looking for files with templates but without examples..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/check-templated-code-snippets.sh; then
echo -e "\tFailed"
exit_ok=no
fi
echo "Checking code styling for files..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-code-style.sh
if [ $? -ne 0 ]
then
if ! runInDocker ./doc.zih.tu-dresden.de/util/check-code-style.sh; then
echo -e "\tFailed"
exit_ok=no
fi
if [ $exit_ok == yes ]
then
if [ $exit_ok == yes ]; then
exit 0
else
exit 1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment