Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Create temporary directories for Git clone operations in the same dir…
Browse files Browse the repository at this point in the history
…ectory as the target path.

Resolves #1421.
  • Loading branch information
charleskorn committed Dec 28, 2022
1 parent 534c0cf commit 5943e63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 3 additions & 2 deletions libs/git-client/src/main/kotlin/batect/git/GitClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import batect.os.ProcessRunner
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.io.path.name

class GitClient(
private val processRunner: ProcessRunner,
private val temporaryDirectoryCreator: () -> Path = { Files.createTempDirectory("batect-git-") }
private val temporaryDirectoryCreator: (parent: Path, name: String) -> Path = { parent, name -> Files.createTempDirectory(parent, name) }
) {
fun clone(repo: String, ref: String, destination: Path) {
try {
val temporaryDirectory = temporaryDirectoryCreator()
val temporaryDirectory = temporaryDirectoryCreator(destination.parent, destination.name)
temporaryDirectory.toFile().deleteOnExit()

val cloneExitCode = processRunner.runWithConsoleAttached(listOf("git", "clone", "--quiet", "--no-checkout", "--", repo, temporaryDirectory.toString()))
Expand Down
22 changes: 15 additions & 7 deletions libs/git-client/src/unitTest/kotlin/batect/git/GitClientSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import org.mockito.kotlin.whenever
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.name

object GitClientSpec : Spek({
describe("a Git client") {
Expand All @@ -44,19 +46,25 @@ object GitClientSpec : Spek({
dir
}

val temporaryDirectoryCreator = { temporaryDirectory }
val targetDirectory by createForEachTest {
val root = Files.createTempDirectory("git-client-spec-target")
root.toFile().deleteOnExit()
root.resolve("repo")
}

val temporaryDirectoryCreator = { parent: Path, name: String ->
assertThat(parent, equalTo(targetDirectory.parent))
assertThat(name, equalTo(targetDirectory.name))

temporaryDirectory
}

val client by createForEachTest { GitClient(processRunner, temporaryDirectoryCreator) }

describe("cloning a repository") {
val repo = "http://github.com/me/my-repo.git"
val ref = "the-reference"

val targetDirectory by createForEachTest {
val root = Files.createTempDirectory("git-client-spec-target")
root.toFile().deleteOnExit()
root.resolve("repo")
}

val cloneCommand by createForEachTest { listOf("git", "clone", "--quiet", "--no-checkout", "--", repo, temporaryDirectory.toString()) }

given("cloning the repository succeeds") {
Expand Down

0 comments on commit 5943e63

Please sign in to comment.