Added base for unit testing

+ some example tests
This commit is contained in:
Abdelilah El Aissaoui 2022-03-30 01:04:20 +02:00
parent d544cc4e94
commit b83133f4fe
4 changed files with 161 additions and 0 deletions

View File

@ -35,6 +35,16 @@ dependencies {
implementation("com.google.dagger:dagger:2.41")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
kapt("com.google.dagger:dagger-compiler:2.41")
testImplementation(platform("org.junit:junit-bom:5.8.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("io.mockk:mockk:1.12.3")
}
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
tasks.withType<KotlinCompile>() {

View File

@ -0,0 +1,26 @@
package app
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
object TestUtils {
fun copyDir(src: String, dest: String) {
try {
Files.walk(Paths.get(src)).forEach { pathA: Path ->
val pathB: Path = Paths.get(dest, pathA.toString().substring(src.length))
try {
if (pathA.toString() != src) Files.copy(
pathA,
pathB,
)
} catch (e: IOException) {
e.printStackTrace()
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}

View File

@ -0,0 +1,41 @@
package app.git
import app.credentials.GProcess
import app.credentials.GRemoteSession
import app.credentials.GSessionManager
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.extension.AfterAllCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL
import java.io.File
import kotlin.io.path.createTempDirectory
private const val REPO_URL = "https://github.com/JetpackDuba/Gitnuro_TestsRepo.git"
private val tempDirPath = createTempDirectory("gitnuro_")
val tempDir: File = tempDirPath.toFile()
lateinit var repoDir: File
class BeforeRepoAllTestsExtension : BeforeAllCallback, AfterAllCallback {
private var started = false
override fun beforeAll(context: ExtensionContext) = runBlocking {
if (!started) {
repoDir = File(tempDir, "repo")
started = true
// Your "before all tests" startup logic goes here
// The following line registers a callback hook when the root test context is shut down
context.root.getStore(GLOBAL).put("any unique name", this)
val remoteOperationsManager = RemoteOperationsManager(GSessionManager { GRemoteSession { GProcess() } })
remoteOperationsManager.clone(repoDir, REPO_URL)
}
}
override fun afterAll(context: ExtensionContext?) {
// Your "after all tests" logic goes here
tempDir.deleteRecursively()
}
}

View File

@ -0,0 +1,84 @@
package app.git
import app.TestUtils.copyDir
import kotlinx.coroutines.runBlocking
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Repository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.extension.ExtendWith
import java.io.File
@ExtendWith(BeforeRepoAllTestsExtension::class)
class BranchesManagerTest {
private lateinit var repo: Repository
private lateinit var git: Git
private lateinit var branchesManagerTestDir: File
@BeforeEach
fun setup() {
branchesManagerTestDir = File(tempDir, "branches_manager")
branchesManagerTestDir.mkdir()
copyDir(repoDir.absolutePath, branchesManagerTestDir.absolutePath)
repo = RepositoryManager().openRepository(branchesManagerTestDir)
git = Git(repo)
}
@AfterEach
fun clean() {
repo.close()
branchesManagerTestDir.deleteRecursively()
}
@org.junit.jupiter.api.Test
fun currentBranchRef() = runBlocking {
val branchesManager = BranchesManager()
val currentBranchRef = branchesManager.currentBranchRef(Git(repo))
assertEquals(currentBranchRef?.name, "refs/heads/main")
}
@org.junit.jupiter.api.Test
fun getBranches() = runBlocking {
val branchesManager = BranchesManager()
val branches = branchesManager.getBranches(git)
assertEquals(branches.count(), 1)
val containsMain = branches.any { it.name == "refs/heads/main" }
assert(containsMain) { println("Error: Branch main does not exist") }
}
@org.junit.jupiter.api.Test
fun createBranch() = runBlocking {
val branchName = "test"
val branchesManager = BranchesManager()
branchesManager.createBranch(git, branchName)
val branches = branchesManager.getBranches(git)
assertEquals(branches.count(), 2)
val containsNewBranch = branches.any { it.name == "refs/heads/$branchName" }
assert(containsNewBranch) { println("Error: Branch $branchName does not exist") }
}
//
// @org.junit.jupiter.api.Test
// fun createBranchOnCommit() {
// }
//
// @org.junit.jupiter.api.Test
// fun deleteBranch() {
// }
//
// @org.junit.jupiter.api.Test
// fun deleteLocallyRemoteBranches() {
// }
//
// @org.junit.jupiter.api.Test
// fun remoteBranches() {
// }
//
// @org.junit.jupiter.api.Test
// fun checkoutRef() {
// }
}