From a9d8d7a587cbfe64fca1bd194a3710dc811e908d Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Fri, 18 Aug 2023 19:13:52 +0000 Subject: [PATCH 1/2] Allow specifying target package prefix for Jacoco coverage report Signed-off-by: Arthur Chan --- infra/base-images/base-runner/coverage | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/infra/base-images/base-runner/coverage b/infra/base-images/base-runner/coverage index df8c3d15152a..541d640cd69d 100755 --- a/infra/base-images/base-runner/coverage +++ b/infra/base-images/base-runner/coverage @@ -459,6 +459,60 @@ 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. + dir_to_remove= + have_target_package="NO" + # Duplicate classes_dir for dependency classes removal + reduced_classes_dir=$DUMPS_DIR/classes_reduced + 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 + if [[ "./$prefix" == "$dir"* ]] + then + have_target_package="YES" + need_remove="NO" + fi + done + if [[ "$need_remove" == "YES" ]] + then + # Mark the directory for removal with a space separated variable + dir_to_remove="$dir_to_remove$dir " + 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. From 39d59c84c9108188bd99f4d8c869ed2522f78a28 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Fri, 10 Nov 2023 22:03:03 +0000 Subject: [PATCH 2/2] Fix logic Signed-off-by: Arthur Chan --- infra/base-images/base-runner/coverage | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/infra/base-images/base-runner/coverage b/infra/base-images/base-runner/coverage index 541d640cd69d..499ecab01d65 100755 --- a/infra/base-images/base-runner/coverage +++ b/infra/base-images/base-runner/coverage @@ -459,17 +459,28 @@ 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 @@ -486,10 +497,22 @@ elif [[ $FUZZING_LANGUAGE == "jvm" ]]; then need_remove="YES" for prefix in $package_prefix do - if [[ "./$prefix" == "$dir"* ]] + 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" ]]