Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage report by Jacoco #4

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions .github/workflows/android_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ on:
jobs:
build:

runs-on: ubuntu-latest
# runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- name: Checkout repository
Expand All @@ -32,15 +33,13 @@ jobs:
- name: Generate coverage report
run: ./gradlew jacocoTestReport

- name: Check coverage
run: |
min_coverage=80
actual_coverage=$(grep -oP '(?<=total=")[0-9]*\.[0-9]*(?=%")' app/build/reports/jacoco/jacocoTestReport/html/index.xml)
is_less=$(echo "$actual_coverage < $min_coverage" | bc)
if [ $is_less -eq 1 ]; then
echo "error: coverage ($actual_coverage%) is below the minimum required ($min_coverage%)"
exit 1
else
echo "success: coverage ($actual_coverage%) is above the minimum required ($min_coverage%)"
fi

- name: Add coverage to PR
id: jacoco
uses: takumi-saito/jacoco-report@v1.7
with:
paths: ${{ github.workspace }}/app/build/reports/jacocoTestReport/jacocoTestReport.xml
token: ${{ secrets.GITHUB_TOKEN }}
min-coverage-overall: 40
min-coverage-changed-files: 60
merge-block: false
debug-mode: true
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a sample of commenting a coverage report to PR using an Android app.

- [Coverage report by Jacoco](https://github.com/takumi-saito/UTSampleApp/pull/4)
- [Coverage report by Kover](https://github.com/takumi-saito/UTSampleApp/pull/5)
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
csv.enabled false
}

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def excludes = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(
dir: "$buildDir/tmp/kotlin-classes/debug",
include: '**/*UseCase.*',
excludes: excludes
)
def mainSrc = "$project.projectDir/src/main/java"

sourceDirectories.from = files([mainSrc])
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/kireaji/utsampleapp/AddItemUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ class AddItemUseCase(private val itemRepository: ItemRepository) {
fun execute(item: String): List<String> {
return itemRepository.addItem(item)
}

fun execute(items: List<String>): List<String> {
return itemRepository.addAllItems(items)
}
}
14 changes: 13 additions & 1 deletion app/src/main/java/com/kireaji/utsampleapp/FindItemUseCase.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.kireaji.utsampleapp

class FindItemUseCase(private val itemRepository: ItemRepository) {
fun execute(item: String): String? {
fun findItem(item: String): String? {
return itemRepository.findItem(item)
}

fun takeItems(take: Int): List<String> {
return itemRepository.takeItems(take)
}

fun firstItem(): String? {
return itemRepository.firstItem()
}

fun lastItem(): String? {
return itemRepository.lastItem()
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/kireaji/utsampleapp/ItemRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.kireaji.utsampleapp

interface ItemRepository {
fun addItem(item: String): List<String>
fun addAllItems(items: List<String>): List<String>
fun removeItem(item: String): List<String>
fun findItem(item: String): String?
fun takeItems(take: Int): List<String>
fun firstItem(): String?
fun lastItem(): String?
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/kireaji/utsampleapp/ItemRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kireaji.utsampleapp

class ItemRepositoryImpl: ItemRepository{

private val itemList: MutableList<String> = mutableListOf()

override fun addItem(item: String): List<String> {
itemList.add(item)
return itemList
}

override fun addAllItems(items: List<String>): List<String> {
itemList.addAll(items)
return itemList
}

override fun removeItem(item: String): List<String> {
itemList.remove(item)
return itemList
}

override fun findItem(item: String): String? {
return itemList.find { it == item }
}

override fun takeItems(take: Int): List<String> {
return itemList.take(take)
}

override fun firstItem(): String? {
return itemList.firstOrNull()
}

override fun lastItem(): String? {
return itemList.lastOrNull()
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/kireaji/utsampleapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ package com.kireaji.utsampleapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val repository = ItemRepositoryImpl()
val findItemUseCase = FindItemUseCase(repository)
val addItemUseCase = AddItemUseCase(repository)
val removeItemUseCase = RemoveItemUseCase(repository)

addItemUseCase.execute("item1")
addItemUseCase.execute(listOf(
"item2",
"item3",
))
Log.d("test", "firstItem: ${findItemUseCase.firstItem()}")
Log.d("test", "findItem: ${findItemUseCase.findItem("item2")}")
Log.d("test", "lastItem: ${findItemUseCase.lastItem()}")
Log.d("test", "takeItems: ${findItemUseCase.takeItems(3)}")
removeItemUseCase.execute("item1")
Log.d("test", "takeItems: ${findItemUseCase.takeItems(3)}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AddItemUseCaseTest {
`when`(itemRepository.findItem(item)).thenReturn(item)

val findItemUseCase = FindItemUseCase(itemRepository)
val result = findItemUseCase.execute(item)
val result = findItemUseCase.findItem(item)

assertEquals(item, result)
}
Expand All @@ -57,7 +57,7 @@ class AddItemUseCaseTest {
`when`(itemRepository.findItem(item)).thenReturn(null)

val findItemUseCase = FindItemUseCase(itemRepository)
val result = findItemUseCase.execute(item)
val result = findItemUseCase.findItem(item)

assertNull(result)
}
Expand Down
Loading