Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java-coverage: Add logic to exclude dependencies in JVM coverage report #10860

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions infra/base-images/base-runner/coverage
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,83 @@ elif [[ $FUZZING_LANGUAGE == "jvm" ]]; then
fi
done


# Check if TARGET_PACKAGE_PREFIX has been set and remove classes not
# in the target package prefix to exclude them in the resulting HTML
# report. By default, Jacoco includes all classes in the classes dump
# directory to the resulting HTML report. Removing classes not in target
# package could concentrate the result on small subset of class and
# decrease the noise from project dependencies.
for arg in $COVERAGE_EXTRA_ARGS
do
if [[ $arg == "TARGET_PACKAGE_PREFIX="* ]]
then
TARGET_PACKAGE_PREFIX=`echo $arg | cut -d= -f2`
fi
done

dir_to_remove=
have_target_package="NO"
# Duplicate classes_dir for dependency classes removal
reduced_classes_dir=$DUMPS_DIR/classes_reduced
rm -rf $reduced_classes_dir
cp -r $classes_dir $reduced_classes_dir

if [[ -n ${TARGET_PACKAGE_PREFIX:-} ]]
then
# Translate the colon separated target package prefix environment
# variable to space separated directories for deciding which
# classes directory needed to be removed.
package_prefix=$(echo $TARGET_PACKAGE_PREFIX | sed "s#:# #g" | sed "s#\.#/#g" | sed "s#/\*##g")
for dir in $(find $reduced_classes_dir -mindepth 1 -type d)
do
# Check if each directory matches one of the target package prefix. If
# not, mark them as candidate for removal with a space separated variable.
# As the directory structure of java classes follow their package name,
# then directories that do not match the provided list of package prefix
# can be removed and exclude from the HTML report.
need_remove="YES"
for prefix in $package_prefix
do
prefix=$reduced_classes_dir/$prefix
if [[ "$dir" == "$prefix"* ]]
then
have_target_package="YES"
need_remove="NO"
else
while [[ "$prefix" != "$reduced_classes_dir" ]]
do
if [[ "$dir" == "$prefix" ]]
then
have_target_package="YES"
need_remove="NO"
break
fi
prefix=$(dirname $prefix)
done
fi
done
if [[ "$need_remove" == "YES" ]]
then
# Mark the directory for removal with a space separated variable
dir_to_remove="$dir_to_remove$dir "
arthurscchan marked this conversation as resolved.
Show resolved Hide resolved
fi
done
fi

# Remove unwanted directory for Jacoco HTML report if
# at least one target package prefix does exist
if [[ "$have_target_package" == "YES" ]]
then
for dir in $dir_to_remove
do
rm -rf $dir
done
# Point classes_dir to reduced_classes_dir if at least one target package
# prefix does exist.
classes_dir=$reduced_classes_dir
fi

# Clean up files that can create duplicate class names which breaks Jacoco.
# Remove META-INF folder because some jar may store duplications of the same
# class file in the META-INF for other java versions.
Expand Down