diff --git a/build.gradle.kts b/build.gradle.kts index 5253084..e1cfb47 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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() { diff --git a/src/test/kotlin/app/TestUtils.kt b/src/test/kotlin/app/TestUtils.kt new file mode 100644 index 0000000..8fdc48c --- /dev/null +++ b/src/test/kotlin/app/TestUtils.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/app/git/BeforeRepoAllTestsExtension.kt b/src/test/kotlin/app/git/BeforeRepoAllTestsExtension.kt new file mode 100644 index 0000000..c5ad37e --- /dev/null +++ b/src/test/kotlin/app/git/BeforeRepoAllTestsExtension.kt @@ -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() + } +} diff --git a/src/test/kotlin/app/git/BranchesManagerTest.kt b/src/test/kotlin/app/git/BranchesManagerTest.kt new file mode 100644 index 0000000..947a055 --- /dev/null +++ b/src/test/kotlin/app/git/BranchesManagerTest.kt @@ -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() { +// } +} \ No newline at end of file