Skip to content

Commit

Permalink
georgberky#9 start error handling tests
Browse files Browse the repository at this point in the history
 Co-Authored-By: Sandra Parsick <[email protected]>
  • Loading branch information
georgberky authored and sparsick committed May 18, 2021
1 parent d8221e2 commit 3b17d4c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/kotlin/com/github/helpermethod/NativeGitProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.helpermethod
import org.apache.commons.io.IOUtils
import java.nio.file.Path

class NativeGitProvider(val localRepositoryDirectory: Path) : GitProvider {
open class NativeGitProvider(val localRepositoryDirectory: Path) : GitProvider {
//TODO: find the right path to git command

override fun hasRemoteBranch(remoteBranchName: String): Boolean {
Expand All @@ -23,9 +23,14 @@ class NativeGitProvider(val localRepositoryDirectory: Path) : GitProvider {
}

override fun add(filePattern: String) {
val returnValue = runInProcess("git", "add", filePattern)

//TODO: handle non-zero return values
val command = arrayOf("git", "add", filePattern)
val returnValue = runInProcess(*command)

if(returnValue != 0) {
throw ProcessException("Native git invocation failed. " +
"Command: ${command.joinToString(" ")}, " +
"return value was: $returnValue")
}
}

override fun commit(author: String, message: String) {
Expand All @@ -49,7 +54,7 @@ class NativeGitProvider(val localRepositoryDirectory: Path) : GitProvider {
return runInProcessWithOutput(*command).first
}

fun runInProcessWithOutput(vararg command: String): Pair<Int, String> {
open fun runInProcessWithOutput(vararg command: String): Pair<Int, String> {
val process = ProcessBuilder(*command)
.directory(localRepositoryDirectory.toFile())
.start()
Expand All @@ -59,4 +64,8 @@ class NativeGitProvider(val localRepositoryDirectory: Path) : GitProvider {

return Pair(returnValue, processOutput)
}

class ProcessException(message: String?) : RuntimeException(message) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.helpermethod

import org.assertj.core.api.Assertions.assertThatCode
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File

internal class NativeGitProviderErrorHandlingTest {

private var processOutput: String = ""
private var returnValue: Int = 0
private lateinit var gitProvider: NativeGitProvider

@TempDir
lateinit var tempDir: File

@BeforeEach
internal fun setUp() {
gitProvider = object : NativeGitProvider(tempDir.toPath()) {
override fun runInProcessWithOutput(vararg command: String): Pair<Int, String> {
return Pair(returnValue, processOutput)
}
}
}

@Test
internal fun `error handling`() {
returnValue = -1

val callAdd = { gitProvider.add("") }

assertThatThrownBy(callAdd)
.isInstanceOf(NativeGitProvider.ProcessException::class.java)
.hasMessageContaining("return value")
.hasMessageContaining("-1")
.hasMessageContaining("git add")
}
}

0 comments on commit 3b17d4c

Please sign in to comment.