Skip to content

Commit

Permalink
#108 pmd fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Oct 12, 2023
1 parent 7bbacfd commit ce4fa98
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 6 additions & 5 deletions metrics/pmd.sh
Expand Up @@ -35,6 +35,7 @@ cat <<EOT > "${tmp}/config.xml"
<ruleset name="cam" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Only CoCo</description>
<rule ref="category/java/design.xml/CognitiveComplexity">
<properties>
<property name="reportLevel" value="1" />
Expand All @@ -45,7 +46,7 @@ EOT

cp "${java}" "${tmp}/foo.java"

pmd pmd -R "${tmp}/config.xml" -d "${tmp}" -format xml -failOnViolation false > "${tmp}/result.xml"
pmd pmd -R "${tmp}/config.xml" -d "${tmp}" --cache "${TARGET}/temp/pmd-cache" --format xml --fail-on-violation false > "${tmp}/result.xml" 2>/dev/null

sed 's/xmlns=".*"//g' "${tmp}/result.xml" | \
(xmllint --xpath '//violation[@rule="CognitiveComplexity"]/text()' - || echo '') | \
Expand All @@ -54,10 +55,10 @@ sed 's/xmlns=".*"//g' "${tmp}/result.xml" | \
ruby -e '
a = STDIN.read.split(" ").map(&:to_i)
sum = a.inject(&:+)
puts "coc #{a.empty? ? 0 : sum} Total Cognitive Complexity of All Methods"
puts "acoc #{a.empty? ? 0 : sum / a.count} Average Cognitive Complexity of a Method"
puts "mxcoc #{a.empty? ? 0 : a.max} Max Cognitive Complexity of a Method"
puts "mncoc #{a.empty? ? 0 : a.min} Min Cognitive Complexity of a Method"
puts "coco #{a.empty? ? 0 : sum} Total Cognitive Complexity of All Methods"
puts "acoco #{a.empty? ? 0 : sum / a.count} Average Cognitive Complexity of a Method"
puts "mxcoco #{a.empty? ? 0 : a.max} Max Cognitive Complexity of a Method"
puts "mncoco #{a.empty? ? 0 : a.min} Min Cognitive Complexity of a Method"
' > "${output}"

rm -rf "${tmp}"
Expand Down
5 changes: 4 additions & 1 deletion tests/metrics/test-pmd.sh
Expand Up @@ -26,9 +26,12 @@ set -o pipefail
temp=$1
stdout=$2

# Just to make sure it's installed
pmd pmd --help > /dev/null

java="${temp}/Foo long 'weird' name (--).java"
mkdir -p "$(dirname "${java}")"
echo "class Foo {}" > "${java}"
"${LOCAL}/metrics/pmd.sh" "${java}" "${temp}/stdout" > "${stdout}" 2>&1
grep "coc 0" "${temp}/stdout" > "${stdout}" 2>&1
grep "coco 0" "${temp}/stdout" > "${stdout}" 2>&1
echo "👍🏻 Correctly calculated congitive complexity"

0 comments on commit ce4fa98

Please sign in to comment.