这是我自己的问题的答案,也许它可以节省一些时间给其他人。
我终于决定编写一个shell脚本来扫描src/test/java
目录(通过TEST_SOURCES_PATH
环境变量提供),并打印出每个缺少有效注释的@Ignore注释的错误。该脚本作为预构建步骤运行。
要求:
检测与无意见或评论不匹配给定的正则表达式的@Ignored测试的所有Java测试文件。如果发现任何错误,则失败构建。
脚本:
#!/bin/sh
# Variables declaration
path=$TEST_SOURCES_PATH
ignoreAnnotationPattern="^\\s*@Ignore"
ignoreCommentDetectionPattern="^\\s*@Ignore\\s*(\\(\"(.*)\"\\))?\\s*$"
validIgnoreCommentPattern="^JIRA\\s[A-Z]*-[1-9][0-9]*\s:\s.*$"
errorsCounter=0
# Change internal field separator to iterate over newlines instead of whitespaces
IFS_backup=$IFS
IFS=$'\n'
# Start detection
echo "Detection of malformed @Ignore annotations in test files started..."
# First find all test files containing an ignored test
testFiles=$(find $path -name "*Test.java" | xargs egrep -l $ignoreAnnotationPattern)
if [ ! -z "$testFiles" ]
then
for testFile in $testFiles
do
# Then keep only lines with the @Ignore annotation
lines=$(egrep $ignoreAnnotationPattern $testFile)
for line in $lines
do
# try to extract the @Ignore comment
if [[ $line =~ $ignoreCommentDetectionPattern ]]
then
# if the comment is not valid then print out a message and increment errors counter
if [[ ! ${BASH_REMATCH[2]} =~ $validIgnoreCommentPattern ]]
then
echo "Test file '"$testFile"' contains a malformed @Ignore annotation: '"$line"'."
errorsCounter=`expr $errorsCounter + 1`
fi
fi
done
done
fi
# Restore initial IFS value
IFS=$IFS_backup
# Exit based on detection result
if [ "$errorsCounter" != 0 ]
then
echo $errorsCounter" errors were found, aborting build !"
exit 1
else
echo "No errors were found, resuming build."
fi
输出示例:
Detection of malformed @Ignore annotations in test files started...
Test file 'src/test/java/MySeventhTest.java' contains a malformed @Ignore annotation: '@Ignore("XXX-999")'.
Test file 'src/test/java/MySecondTest.java' contains a malformed @Ignore annotation: '@Ignore'.
Test file 'src/test/java/MySixthTest.java' contains a malformed @Ignore annotation: '@Ignore("See JIRA XXX-99")'.
Test file 'src/test/java/MyFourthTest.java' contains a malformed @Ignore annotation: '@Ignore("See JIRA XXX-999 : smart reason")'.
Test file 'src/test/java/MyTest.java' contains a malformed @Ignore annotation: '@Ignore("")'.
5 errors were found, aborting build !
优点:
- 快速检测在Java之前完成建立(快速反馈)
- 有效忽略意见式样可以轻松地修改
可能的改进:
- 提供有关错误的更多细节
- ,以检查呼叫JIRA Web服务该票存在并且未关闭
听起来像Checkstyle的工作? – khmarbaise 2014-12-04 08:46:11