|
| 1 | +import groovy.transform.Memoized |
| 2 | + |
| 3 | +apply plugin: 'jacoco' |
| 4 | + |
| 5 | +jacoco { |
| 6 | + toolVersion = '0.8.7' |
| 7 | +} |
| 8 | + |
| 9 | +android { |
| 10 | + jacoco { |
| 11 | + version = '0.8.7' |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +@Memoized |
| 16 | +Properties getLocalProperties() { |
| 17 | + final propertiesFile = project.rootProject.file('local.properties') |
| 18 | + |
| 19 | + final properties = new Properties() |
| 20 | + |
| 21 | + if (propertiesFile.exists()) { |
| 22 | + properties.load(propertiesFile.newDataInputStream()) |
| 23 | + } |
| 24 | + |
| 25 | + return properties |
| 26 | +} |
| 27 | + |
| 28 | +def openReport(htmlOutDir) { |
| 29 | + final reportPath = "$htmlOutDir/index.html" |
| 30 | + |
| 31 | + println "HTML Report: $reportPath" |
| 32 | + |
| 33 | + if (!project.hasProperty('open-report')) { |
| 34 | + println "to open the report automatically in your default browser add '-Popen-report' cli argument" |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + def os = org.gradle.internal.os.OperatingSystem.current() |
| 39 | + if (os.isWindows()) { |
| 40 | + exec { commandLine 'cmd', '/c', "start $reportPath" } |
| 41 | + } else if (os.isMacOsX()) { |
| 42 | + exec { commandLine 'open', "$reportPath" } |
| 43 | + } else if (os.isLinux()) { |
| 44 | + try { |
| 45 | + exec { commandLine 'xdg-open', "$reportPath" } |
| 46 | + } catch (Exception ignored) { |
| 47 | + if (localProperties.containsKey('linux-html-cmd')) { |
| 48 | + exec { commandLine properties.get('linux-html-cmd'), "$reportPath" } |
| 49 | + } else { |
| 50 | + println "'linux-html-cmd' property could not be found in 'local.properties'" |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +tasks.withType(Test) { |
| 57 | + jacoco.includeNoLocationClasses = true |
| 58 | + jacoco.excludes = ['jdk.internal.*'] |
| 59 | +} |
| 60 | + |
| 61 | +// Our merge report task |
| 62 | +task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'connectedDebugAndroidTest']) { |
| 63 | + def htmlOutDir = layout.buildDirectory.dir("reports/jacoco/$name/html").get().asFile |
| 64 | + |
| 65 | + doLast { |
| 66 | + openReport htmlOutDir |
| 67 | + } |
| 68 | + |
| 69 | + reports { |
| 70 | + xml.enabled = true |
| 71 | + html.destination htmlOutDir |
| 72 | + } |
| 73 | + |
| 74 | + def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'] |
| 75 | + def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter) |
| 76 | + def debugTree2 = fileTree(dir: "$project.buildDir/../../node_modules/@react-native-firebase/android/build/intermediates/javac/debug/classes", excludes: fileFilter) |
| 77 | + def mainSrc = "$project.projectDir/src/main/java" |
| 78 | + def mainSrc2 = "$project.projectDir/../../node_modules/@react-native-firebase/android/src/main/java" |
| 79 | + |
| 80 | + sourceDirectories.from = files([mainSrc, mainSrc2]) |
| 81 | + classDirectories.from = files([debugTree, debugTree2]) |
| 82 | + executionData.from = fileTree(dir: project.buildDir, includes: [ |
| 83 | + '**/*.exec', |
| 84 | + '**/*.ec' |
| 85 | + ]) |
| 86 | +} |
| 87 | + |
| 88 | +// A unit-test only report task |
| 89 | +task jacocoUnitTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { |
| 90 | + def htmlOutDir = layout.buildDirectory.dir("reports/jacoco/$name/html").get().asFile |
| 91 | + |
| 92 | + // Runs normal test but with this added: |
| 93 | + // -javaagent:build/tmp/expandedArchives/org.jacoco.agent-0.8.7.jar_3a83c50b4a016f281c4e9f3500d16b55/jacocoagent.jar=destfile=build/jacoco/testPlayDebugUnitTest.exec,append=true,excludes=jdk.internal.*,inclnolocationclasses=true,dumponexit=true,output=file,jmx=false |
| 94 | + |
| 95 | + doLast { |
| 96 | + openReport htmlOutDir |
| 97 | + } |
| 98 | + |
| 99 | + reports { |
| 100 | + xml.enabled = true |
| 101 | + html.destination htmlOutDir |
| 102 | + } |
| 103 | + |
| 104 | + def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'] |
| 105 | + def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter) |
| 106 | + def mainSrc = "$project.projectDir/src/main/java" |
| 107 | + |
| 108 | + sourceDirectories.from = files([mainSrc]) |
| 109 | + classDirectories.from = files([debugTree]) |
| 110 | + executionData.from = fileTree(dir: project.buildDir, includes: [ |
| 111 | + '**/*.exec' |
| 112 | + |
| 113 | + ]) |
| 114 | +} |
| 115 | + |
| 116 | +// A connected android tests only report task |
| 117 | +task jacocoAndroidTestReport(type: JacocoReport) { |
| 118 | + def htmlOutDir = layout.buildDirectory.dir("reports/jacoco/$name/html").get().asFile |
| 119 | + |
| 120 | + doLast { |
| 121 | + openReport htmlOutDir |
| 122 | + } |
| 123 | + |
| 124 | + reports { |
| 125 | + xml.enabled = true |
| 126 | + html.destination htmlOutDir |
| 127 | + } |
| 128 | + |
| 129 | + def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'] |
| 130 | + |
| 131 | + // use our collected firebase module names to aggregate source / class files for reporting |
| 132 | + def classFiles = [] |
| 133 | + def srcFiles = [] |
| 134 | + rootProject.ext.firebaseModulePaths.forEach { projectPath -> |
| 135 | + classFiles << fileTree(dir: "$projectPath/build/intermediates/javac/debug/classes", excludes: fileFilter) |
| 136 | + srcFiles << "$projectPath/src/main/java" |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + sourceDirectories.from = files(srcFiles) |
| 141 | + classDirectories.from = files(classFiles) |
| 142 | + executionData.from = fileTree(dir: project.buildDir, includes: [ |
| 143 | + '**/*.ec' |
| 144 | + ]) |
| 145 | +} |
0 commit comments