Skip to content
Snippets Groups Projects
Commit 71f6a13f authored by Danny Marc Rotscher's avatar Danny Marc Rotscher
Browse files

Merge branch '233-add-test-for-templated-code-snippets' into 'preview'

Added check-templated-code-snippets.py and check-templated-code-snippets.sh.

Closes #233

See merge request !481
parents 000d832a 46527d11
No related branches found
No related tags found
2 merge requests!530Automated merge from preview to main,!481Added check-templated-code-snippets.py and check-templated-code-snippets.sh.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import re
import sys
def escapeSomeSigns(someString):
return someString.replace("$", "\\$").replace("(", "\\(").replace(")", "\\)").replace("*", "\\*")
fileName = sys.argv[1]
print("FILE: " + fileName)
lines = []
NORMAL_MODE = 0
CODE_MODE = 1
readMode = NORMAL_MODE
pattern = re.compile(r"<[^<>']*>")
with open(fileName) as f:
lineNumber = 1
for line in f:
if "```" in line:
# toggle read mode if we find a line with ```, so that we know that we are in a code block or not
readMode = CODE_MODE if readMode == NORMAL_MODE else NORMAL_MODE
strippedLine = line.strip()
# We want tuples with lineNumber, the line itself, whether it is a code line, whether it contains a template (e. g. <FILENAME>) and the line again with all templats replaced by '\\S'
lines.append((lineNumber, strippedLine, readMode, pattern.search(strippedLine) != None, pattern.sub(r"\\S*", escapeSomeSigns(strippedLine))))
lineNumber += 1
# those tuples with the CODE_MODE as field 2 represent code lines
codeLines = list(filter(lambda line: line[2] == CODE_MODE, lines))
# we take line number, the line and a regular expression from the those code lines which contain a template, call them templatedLines
templatedLines = list(map(lambda line: (line[0], line[1], re.compile(line[4])), filter(lambda line: line[3], codeLines)))
allPatternsFound = True
for templateLine in templatedLines:
# find code lines which have a higher line number than the templateLine, contain no template themselves and match the pattern of the templateLine
matchingCodeLines = list(filter(lambda line: (line[0] > templateLine[0]) and (not line[3]) and (templateLine[2].match(line[1]) != None), codeLines))
if len(matchingCodeLines) == 0:
allPatternsFound = False
print(" Example for \"" + templateLine[1] + "\" (Line " + str(templateLine[0]) + ") missing")
if not allPatternsFound:
sys.exit(1)
#!/bin/bash
set -eo pipefail
scriptpath=${BASH_SOURCE[0]}
basedir=`dirname "$scriptpath"`
pythonscript="$basedir/check-templated-code-snippets.py"
basedir=`dirname "$basedir"`
function usage() {
cat <<-EOF
usage: $0 [file | -a]
Search for code snippets that use templates but do not give examples.
If file is given, outputs all lines where no example could be found.
If parameter -a (or --all) is given instead of the file, checks all markdown files.
Otherwise, checks whether any changed file contains code snippets with templates without examples.
EOF
}
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
# Options
if [ $# -eq 1 ]; then
case $1 in
help | -help | --help)
usage
exit
;;
-a | --all)
echo "Search in all markdown files."
files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/ | grep .md)
;;
*)
files="$1"
;;
esac
elif [ $# -eq 0 ]; then
echo "Search in git-changed files."
files=`git diff --name-only "$(git merge-base HEAD "$branch")" | grep .md || true`
else
usage
fi
all_ok=''
for f in $files; do
if ! $pythonscript $f; then
all_ok='no'
fi
done
if [ -z "$all_ok" ]; then
echo "Success!"
else
echo "Fail!"
exit 1
fi
......@@ -82,6 +82,13 @@ then
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
exit_ok=no
fi
if [ $exit_ok == yes ]
then
exit 0
......
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