Skip to content

Commit

Permalink
introduce JacocoCoverageRule (#6)
Browse files Browse the repository at this point in the history
* introduce JacocoCoverageRule

* add JacocoCoverageRuleTest

* support all nested objects

* improve test coverage

* add more tests
  • Loading branch information
tarek360 authored Sep 19, 2018
1 parent 172dcf4 commit 3da5723
Show file tree
Hide file tree
Showing 76 changed files with 2,454 additions and 296 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
name: Run tests
command: gradle test --stacktrace

- run:
name: Run JaCoCo
command: gradle jacocoFullReport

- run:
name: DistZip
command: gradle distZip
Expand Down
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties

### AndroidStudio Patch ###

!/gradle/wrapper/gradle-wrapper.jar

### Firebase ###
.idea
**/node_modules/*
Expand Down Expand Up @@ -302,6 +298,3 @@ $RECYCLE.BIN/

### Firebase Google Services file ###
google-services.json

# git_diff
.git_diff
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(project(":koshry"))
implementation(project(":rules"))
implementation(project(":test-coverage-rule"))
}

java {
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/io/github/tarek360/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package io.github.tarek360.app

import io.github.tarek360.koshry.Koshry
import io.github.tarek360.koshry.koshry
import io.github.tarek360.rules.coverage.jacocoCoverageRule
import io.github.tarek360.rules.lineRule
import io.github.tarek360.rules.fileRule
import io.github.tarek360.rules.protectedFileRule
import io.github.tarek360.rules.report.Level.ERROR
import io.github.tarek360.rules.core.Level.ERROR

fun main(_args: Array<String>) {

val configuration = koshry {

baseSha = ""

rules {
rule = protectedFileRule {
reportTitle = "Files are protected and can't be modified, ask @tarek360 to modify"
Expand All @@ -35,6 +38,13 @@ fun main(_args: Array<String>) {
reportTitle = "Don't add new Java files, use Kotlin instead."
issueLevel = ERROR
}

// JaCoCo Test Coverage Rule
rule = jacocoCoverageRule {
classCoverageThreshold = 79 //79%
csvFilePath = "build/reports/jacoco/jacoco.csv"
htmlFilePath = "https://tarek360.github.io/koshry/build/reports/"
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ allprojects {
jcenter()
}
}

apply { from("jacoco/jacocoFullReport.gradle") }
1 change: 1 addition & 0 deletions ci-detector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
id("java-library")
id("maven-publish")
jacoco
}

apply { from("../mvn-push.gradle") }
Expand Down
2 changes: 1 addition & 1 deletion ci-detector/src/main/java/io/github/tarek360/ci/Ci.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.tarek360.ci

abstract class Ci {

val gitHostToken: String? by lazy {
open val gitHostToken: String? by lazy {
Environment.getVariable("KOSHRY_GIT_HOST_TOKEN")
}

Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
id("java-library")
id("maven-publish")
jacoco
}

apply { from("../mvn-push.gradle") }
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/io/github/tarek360/core/Collections.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.tarek360.core


inline fun <T, R> Iterable<T>.filterThenMapIfNotNull(transform: (T) -> R?): List<R> {
val size = if (this is Collection<*>) this.size else 10
val list = ArrayList<R>(size)
for (element in this) {
val mapped = transform(element)
if (mapped != null) {
list.add(mapped)
}
}
return list
}

inline fun <T, R> Iterable<T>.filterThenMap(predicate: (T) -> Boolean, transform: (T) -> R): List<R> {
val size = if (this is Collection<*>) this.size else 10
val list = ArrayList<R>(size)
for (element in this) {
if (predicate(element)) {
val mapped = transform(element)
list.add(mapped)
}
}
return list
}

11 changes: 10 additions & 1 deletion core/src/main/java/io/github/tarek360/core/Kssert.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package io.github.tarek360.core

import org.junit.Assert.assertEquals
import org.hamcrest.CoreMatchers.instanceOf
import org.junit.Assert.*

infix fun Any.mustEqual(expected: Any) = assertEquals(expected, this)
infix fun Any.mustInstanceOf(expected: Class<*>) = assertThat(this, instanceOf(expected))

infix fun Any?.mustEqualAndNotNull(expected: Any) {
assertNotNull(this)
assertEquals(expected, this)
}

infix fun Collection<Any>.mustHaveSize(expected: Int) = assertEquals("unexpected size", expected, this.size)

40 changes: 21 additions & 19 deletions core/src/main/java/io/github/tarek360/core/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package io.github.tarek360.core

const val DEBUGGABLE = false
const val DEBUGGABLE = true

val logger: Logger = Logger()

class Logger {

val ANSI_RED = "\u001B[31m"
val ANSI_YELLOW = "\u001B[33m"
val ANSI_WHITE = "\u001B[37m"
val ANSI_GREEN = "\u001B[32m"
val ANSI_RESET = "\u001B[0m"
companion object {
private const val ANSI_RED = "\u001B[31m"
private const val ANSI_YELLOW = "\u001B[33m"
private const val ANSI_WHITE = "\u001B[37m"
private const val ANSI_GREEN = "\u001B[32m"
private const val ANSI_RESET = "\u001B[0m"
}

fun d(msg: () -> String?) {
if (DEBUGGABLE) {
println("$ANSI_WHITE${msg()}$ANSI_RESET")
fun d(msg: () -> String?) {
if (DEBUGGABLE) {
println("$ANSI_WHITE${msg()}$ANSI_RESET")
}
}
}

fun e(msg: () -> String?) {
println("$ANSI_RED${msg()}$ANSI_RESET")
}
fun e(msg: () -> String?) {
println("$ANSI_RED${msg()}$ANSI_RESET")
}

fun w(msg: () -> String?) {
println("$ANSI_YELLOW${msg()}$ANSI_RESET")
}
fun w(msg: () -> String?) {
println("$ANSI_YELLOW${msg()}$ANSI_RESET")
}

fun i(msg: () -> String?) {
println("$ANSI_GREEN${msg()}$ANSI_RESET")
}
fun i(msg: () -> String?) {
println("$ANSI_GREEN${msg()}$ANSI_RESET")
}

}
1 change: 1 addition & 0 deletions gitdiff-parser/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
id("java-library")
id("maven-publish")
jacoco
}

apply { from("../mvn-push.gradle") }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package io.github.tarek360.gitdiff

import io.github.tarek360.gitdiffprovider.GitCommanderProvider

interface GitDiff {

companion object {
fun create(baseSha: String, headSha: String): GitDiff {
val gitCommander = GitCommanderProvider.provide(baseSha, headSha)
return GitDiffImpl(gitCommander)
}
}

fun getAddedFiles(): List<GitFile>

fun getDeletedFiles(): List<GitFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.tarek360.gitdiff

import io.github.tarek360.gitdiffprovider.GitCommanderProvider

class GitDiffProvider {
companion object {
fun provide(baseSha: String, headSha: String): GitDiff {
val gitCommander = GitCommanderProvider.provide(baseSha, headSha)
return GitDiffImpl(gitCommander)
}
}
}
3 changes: 2 additions & 1 deletion gitdiff-provider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
id("java-library")
id("maven-publish")
jacoco
}

apply { from("../mvn-push.gradle") }
Expand All @@ -19,7 +20,7 @@ version = "0.0.1"

dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(project(":core"))
api(project(":core"))
}

java {
Expand Down
4 changes: 3 additions & 1 deletion githost/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
id("java-library")
id("maven-publish")
jacoco
}

apply { from("../mvn-push.gradle") }
Expand All @@ -17,7 +18,7 @@ repositories {
group = "io.github.tarek360"
version = "0.0.1"

val okHttpVersion = "3.8.1"
val okHttpVersion = "3.11.0"

dependencies {
implementation(kotlin("stdlib-jdk8"))
Expand All @@ -27,6 +28,7 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:$okHttpVersion")

implementation("org.json:json:20160810")
testImplementation("junit:junit:4.12")
}

java {
Expand Down
24 changes: 24 additions & 0 deletions githost/src/main/java/io/github/tarek360/githost/UnknownGitHost.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.tarek360.githost

import io.github.tarek360.core.logger

class UnknownGitHost : GitHost {

override fun post(comment: Comment): String? {
logger.w { "Unknown GitHost: Koshry can't post the report" }
return null
}

override fun post(status: Status) {
logger.w { "Unknown GitHost: Koshry can't post the status" }
}

override fun pushFile(filePath: String, branchName: String, commitMsg: String) {
logger.w { "Unknown GitHost: Koshry can't push files" }
}

override fun getPullRequestInfo(): PullRequest? {
logger.w { "Unknown GitHost: Koshry can't get the Pull Request Info" }
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.tarek360.githost.github

import io.github.tarek360.core.DEBUGGABLE
import io.github.tarek360.core.cl.CommanderImpl
import io.github.tarek360.githost.Comment
import io.github.tarek360.githost.GitHost
Expand All @@ -14,9 +13,11 @@ import okhttp3.RequestBody
import org.json.JSONObject
import java.net.HttpURLConnection.HTTP_CREATED

var githubApiBaseUrl: String = "https://api.github.com/"

class GitHub(private val gitHostInfo: GitHostInfo) : GitHost {

private val apiBaseUrl: String = "https://api.github.com/repos/${gitHostInfo.ownerNameRepoName}"
private val apiReposUrl: String = "${githubApiBaseUrl}repos/${gitHostInfo.ownerNameRepoName}"
private val githubCommitCommander = GithubCommitCommander(CommanderImpl(), gitHostInfo)

override fun post(comment: Comment): String? = postPullRequestComment(comment)
Expand All @@ -30,7 +31,7 @@ class GitHub(private val gitHostInfo: GitHostInfo) : GitHost {
}

override fun getPullRequestInfo(): PullRequest? {
val url = "$apiBaseUrl/pulls/${gitHostInfo.pullRequestId}"
val url = "$apiReposUrl/pulls/${gitHostInfo.pullRequestId}"

val request = Request.Builder()
.url(url)
Expand All @@ -40,14 +41,14 @@ class GitHub(private val gitHostInfo: GitHostInfo) : GitHost {

val response = okhttp.newCall(request).execute()

val json = response.body()?.string() ?: ""
val json = response.body()?.string()

return PullRequestParser().parse(json)
}

private fun postPullRequestComment(comment: Comment): String? {

val url = "$apiBaseUrl/issues/${gitHostInfo.pullRequestId}/comments"
val url = "$apiReposUrl/issues/${gitHostInfo.pullRequestId}/comments"

val bodyJson = JSONObject()
bodyJson.put("body", comment.msg)
Expand All @@ -73,7 +74,7 @@ class GitHub(private val gitHostInfo: GitHostInfo) : GitHost {

private fun postCommitStatus(status: Status) {

val url = "$apiBaseUrl/statuses/${status.sha}"
val url = "$apiReposUrl/statuses/${status.sha}"

val bodyJson = JSONObject()
bodyJson.put("context", status.context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.tarek360.githost.github

import io.github.tarek360.githost.Comment
import io.github.tarek360.githost.GitHostInfo
import io.github.tarek360.githost.Status
import org.junit.Test

class GitHubTest {

@Test
fun postComment() {
val gitHostInfo = GitHostInfo("tarek360/RichPath", 1, "abcd1234")
val gitHub = GitHub(gitHostInfo)
gitHub.post(Comment("Hi", false))
}

@Test
fun postStatus() {
val gitHostInfo = GitHostInfo("tarek360/RichPath", 1, "abcd1234")
val gitHub = GitHub(gitHostInfo)
gitHub.post(Status("", Status.Type.SUCCESS, "abcef98765", "Status description", null))
}
}
Loading

0 comments on commit 3da5723

Please sign in to comment.