Skip to content

Commit

Permalink
Merge pull request #385 from Raleksan/bug_discovering_metrics
Browse files Browse the repository at this point in the history
#245 Add bug discovering metric
  • Loading branch information
yegor256 authored Oct 16, 2024
2 parents 9f68eca + cecf425 commit 778143a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
49 changes: 49 additions & 0 deletions metrics/bug_discover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# The MIT License (MIT)
#
# Copyright (c) 2021-2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set -e
set -o pipefail

java=$1
output=$(realpath "$2")

tmp=$(mktemp -d)
mkdir -p "${tmp}"

cat <<EOT > "${tmp}/config.xml"
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="CAM_bug_discover" 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>Bug discovering</description>
<rule ref="category/java/errorprone.xml"/>
</ruleset>
EOT
cp "${java}" "${tmp}/foo.java"

export PMD_JAVA_OPTS=${JVM_OPTS}
# We don't use --cache here, because it becomes too big and leads to "Out Of Memory" error
pmd check -R "${tmp}/config.xml" -d "${tmp}" --format xml --no-fail-on-error --no-fail-on-violation > "${tmp}/result.xml" 2> "${tmp}/stderr.txt" || (cat "${tmp}/stderr.txt"; exit 1)

violation_num=$(awk '/<violation/ {count++} END {print count+0}' "${tmp}/result.xml")
printf "BugNum %s The number of issues detected by PMD\n" "${violation_num}" > "${output}"
47 changes: 47 additions & 0 deletions tests/metrics/test-bug_discover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# The MIT License (MIT)
#
# Copyright (c) 2021-2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -e
set -o pipefail

temp=$1
stdout=$2

{
pmd --version
xmllint --version
} > "${stdout}" 2>&1
echo "👍🏻 PMD dependencies are installed"

{
java="${temp}/Foo long 'weird' name (--).java"
echo "
public class StaticField {
static int x;
public FinalFields(int y) {
x = y; // unsafe
}
}" > "${java}"
"${LOCAL}/metrics/bug_discover.sh" "${java}" "${temp}/stdout"
grep "BugNum 1 " "${temp}/stdout"
} > "${stdout}" 2>&1
echo "👍🏻 Correctly calculates the bug discovery metric"

0 comments on commit 778143a

Please sign in to comment.