apply plugin: "jacoco"

jacoco {
  toolVersion = "0.8.6"
}

jacocoTestReport {
  dependsOn test
  reports {
    xml.enabled true
    csv.enabled false
    html.destination file("${buildDir}/reports/jacoco/")
  }
}

if (!project.ext.hasProperty("excludedClassesCoverage")) {
  project.ext.excludedClassesCoverage = []
}

if (!project.ext.hasProperty("excludedClassesBranchCoverage")) {
  project.ext.excludedClassesBranchCoverage = []
}

if (!project.ext.hasProperty("excludedClassesInstructionCoverage")) {
  project.ext.excludedClassesInstructionCoverage = []
}

// defaults can be overridden per project:
project.ext.minimumBranchCoverage = 0.9
project.ext.minimumInstructionCoverage = 0.9

afterEvaluate {
  tasks.withType(Test) {
    jacoco {
      // Make sure that excluded classes do not get jacoco instrumentation applied since it may confuse apm agent in some cases
      excludes = project.excludedClassesCoverage
    }
  }

  jacocoTestCoverageVerification {
    violationRules {
      rule {
        element = 'CLASS'
        excludes = project.excludedClassesBranchCoverage + project.excludedClassesCoverage
        limit {
          counter = 'BRANCH'
          minimum = project.minimumBranchCoverage
        }
      }

      rule {
        element = 'CLASS'
        excludes = project.excludedClassesInstructionCoverage + project.excludedClassesCoverage
        limit {
          counter = 'INSTRUCTION'
          minimum = project.minimumInstructionCoverage
        }
      }
    }
  }

  jacocoTestCoverageVerification.dependsOn jacocoTestReport
  check.dependsOn jacocoTestCoverageVerification
}
