Add option to runtest.sh to generate code coverage reports for coreclr
authorWilliam Godbe <william.godbe@comcast.net>
Wed, 10 Feb 2016 22:41:03 +0000 (14:41 -0800)
committerWilliam Godbe <william.godbe@comcast.net>
Fri, 19 Feb 2016 23:47:33 +0000 (15:47 -0800)
Commit migrated from https://github.com/dotnet/coreclr/commit/c219cf99af9f0bc10de97e502574db8d34b2f45d

src/coreclr/tests/runtest.sh

index 9741ced..b81094c 100755 (executable)
@@ -41,6 +41,14 @@ function print_usage {
     echo '  -h|--help                    : Show usage information.'
     echo '  --useServerGC                : Enable server GC for this test run'
     echo ''
+    echo 'Runtime Code Coverage options:'
+    echo '  --coreclr-coverage           : Optional argument to get coreclr code coverage reports'
+    echo '  --coreclr-objs=<path>        : Location of root of the object directory'
+    echo '                                 containing the linux/mac coreclr build'
+    echo '  --coreclr-src=<path>         : Location of root of the directory'
+    echo '                                 containing the coreclr source files'
+    echo '  --coverage-output-dir=<path> : Directory where coverage output will be written to'
+    echo ''
 }
 
 function print_results {
@@ -565,6 +573,38 @@ function run_tests_in_directory {
     done
 }
 
+function coreclr_code_coverage()
+{
+
+  local coverageDir="$coverageOutputDir/Coverage"
+  local toolsDir="$coverageOutputDir/Coverage/tools"
+  local reportsDir="$coverageOutputDir/Coverage/reports"
+  local packageName="unix-code-coverage-tools.1.0.0.nupkg"
+  rm -rf $coverageDir
+  mkdir -p $coverageDir
+  mkdir -p $toolsDir
+  mkdir -p $reportsDir
+  pushd $toolsDir > /dev/null
+
+  echo "Pulling down code coverage tools"
+  wget -q https://www.myget.org/F/dotnet-buildtools/api/v2/package/unix-code-coverage-tools/1.0.0 -O $packageName
+  echo "Unzipping to $toolsDir"
+  unzip -q -o $packageName
+
+  # Invoke gcovr
+  chmod a+rwx ./gcovr
+  chmod a+rwx ./$OSName/llvm-cov
+
+  echo
+  echo "Generating coreclr code coverage reports at $reportsDir/coreclr.html"
+  echo "./gcovr $coreClrObjs --gcov-executable=$toolsDir/$OS/llvm-cov -r $coreClrSrc --html --html-details -o $reportsDir/coreclr.html"
+  echo
+  ./gcovr $coreClrObjs --gcov-executable=$toolsDir/$OSName/llvm-cov -r $coreClrSrc --html --html-details -o $reportsDir/coreclr.html
+  exitCode=$?
+  popd > /dev/null
+  exit $exitCode
+}
+
 # Exit code constants
 readonly EXIT_CODE_SUCCESS=0       # Script ran normally.
 readonly EXIT_CODE_EXCEPTION=1     # Script exited because something exceptional happened (e.g. bad arguments, Ctrl-C interrupt).
@@ -578,6 +618,10 @@ coreClrBinDir=
 mscorlibDir=
 coreFxBinDir=
 coreFxNativeBinDir=
+coreClrObjs=
+coreClrSrc=
+coverageOutputDir=
+
 ((disableEventLogging = 0))
 ((serverGC = 0))
 
@@ -632,6 +676,18 @@ do
         --useServerGC)
             ((serverGC = 1))
             ;;
+        --coreclr-coverage)
+            CoreClrCoverage=ON
+            ;;
+        --coreclr-objs=*)
+            coreClrObjs=${i#*=}
+            ;;
+        --coreclr-src=*)
+            coreClrSrc=${i#*=}
+            ;;
+        --coverage-output-dir=*)
+            coverageOutputDir=${i#*=}
+            ;;
         *)
             echo "Unknown switch: $i"
             print_usage
@@ -661,6 +717,39 @@ fi
 # order for interop tests to run on linux.
 cp $mscorlibDir/bin/* $mscorlibDir   
 
+# If this is a coverage run, make sure the appropriate args have been passed
+if [ "$CoreClrCoverage" == "ON" ]
+then
+    echo "Code coverage is enabled for this run"
+    echo ""
+    if [ ! "$OSName" == "Darwin" ] && [ ! "$OSName" == "Linux" ]
+    then
+        echo "Code Coverage not supported on $OS"
+        exit 1
+    fi
+
+    if [ -z "$coreClrObjs" ]
+    then
+        echo "Coreclr obj files are required to generate code coverage reports"
+        echo "Coreclr obj files root path can be passed using '--coreclr-obj' argument"
+        exit 1
+    fi
+
+    if [ -z "$coreClrSrc" ]
+    then
+        echo "Coreclr src files are required to generate code coverage reports"
+        echo "Coreclr src files root path can be passed using '--coreclr-src' argument"
+        exit 1
+    fi
+
+    if [ -z "$coverageOutputDir" ]
+    then
+        echo "Output directory for coverage results must be specified"
+        echo "Output path can be specified '--coverage-output-dir' argument"
+        exit 1
+    fi
+fi
+
 xunit_output_begin
 create_core_overlay
 copy_test_native_bin_to_test_root
@@ -691,6 +780,11 @@ finish_remaining_tests
 print_results
 xunit_output_end
 
+if [ "$CoreClrCoverage" == "ON" ]
+then
+    coreclr_code_coverage
+fi
+
 if ((countFailedTests > 0)); then
     exit $EXIT_CODE_TEST_FAILURE
 fi