From 42afd9412bfd8b8dd7ca39abb8f08af96a8503e5 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Wed, 9 Jun 2021 16:25:09 +0200
Subject: [PATCH] use jacoco for coverage

---
 .gitlab-ci.yml                                   | 15 ++++++++++++++-
 ragconnect.tests/build.gradle                    | 12 ++++++++++++
 ragconnect.tests/extract-coverage-from-report.py |  8 ++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 ragconnect.tests/extract-coverage-from-report.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 56c0e1b..108611c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -37,13 +37,26 @@ test:
   needs:
     - build
   script:
-    - ./gradlew --console=plain --no-daemon allTests
+    - ./gradlew --console=plain --no-daemon allTests jacocoTestReport
   artifacts:
     when: always
+    paths:
+      - "ragconnect.tests/build/reports/jacoco/all-tests/jacoco*Report.xml"
     reports:
       junit: "ragconnect.tests/build/test-results/**/TEST-*.xml"
     expire_in: 1 week
 
+extractCoverage:
+  image: python:3.8-buster
+  stage: test
+  needs:
+    - test
+  script:
+    - pip install --user untangle
+    - cd ragconnect.tests/
+    - python extract-coverage-from-report.py
+  coverage: "/Covered (\\d{1,3}\\.\\d{2}%) of instructions for all projects\\./"
+
 publish:
   image: openjdk:11
   stage: publish
diff --git a/ragconnect.tests/build.gradle b/ragconnect.tests/build.gradle
index 485a59d..33ce1df 100644
--- a/ragconnect.tests/build.gradle
+++ b/ragconnect.tests/build.gradle
@@ -22,6 +22,7 @@ plugins {
     id 'idea'
     id 'com.github.ben-manes.versions' version '0.36.0'
     id 'com.google.protobuf' version "0.8.14"
+    id 'jacoco'
 }
 
 apply plugin: 'jastadd'
@@ -83,6 +84,17 @@ protobuf {
     }
 }
 
+jacocoTestReport {
+    executionData tasks.withType(Test).findAll { it.state.executed }
+    getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
+
+    reports {
+        xml.enabled true
+        xml.destination(file("${jacoco.reportsDir}/all-tests/jacocoAllTestReport.xml"))
+        html.enabled false
+    }
+}
+
 task allTests(type: Test, dependsOn: testClasses) {
     description = 'Run every test'
     group = 'verification'
diff --git a/ragconnect.tests/extract-coverage-from-report.py b/ragconnect.tests/extract-coverage-from-report.py
new file mode 100644
index 0000000..b8e2668
--- /dev/null
+++ b/ragconnect.tests/extract-coverage-from-report.py
@@ -0,0 +1,8 @@
+import os
+import untangle
+print('Current path: ' + os.path.abspath(os.curdir))
+obj = untangle.parse('build/reports/jacoco/test/jacocoTestReport.xml')
+instructions = [o for o in obj.report.counter if o['type'] == 'INSTRUCTION'][0]
+missed, covered = int(instructions['missed']), int(instructions['covered'])
+# print missed / (missed + covered)
+print('Covered %.2f%% of instructions for all projects.' % (missed * 100.0 / (missed + covered)))
-- 
GitLab