From 04a0c008882c8368810806af51b2df7736572175 Mon Sep 17 00:00:00 2001 From: Kyle Mayes Date: Thu, 2 Nov 2023 02:33:17 -0400 Subject: [PATCH] Add test for calling git from generator --- .../kylemayes/generator/support/Command.kt | 13 ++++++---- .../generator/support/CommandTest.kt | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 generator/src/test/kotlin/com/kylemayes/generator/support/CommandTest.kt diff --git a/generator/src/main/kotlin/com/kylemayes/generator/support/Command.kt b/generator/src/main/kotlin/com/kylemayes/generator/support/Command.kt index 26394f7..b8a2c6d 100644 --- a/generator/src/main/kotlin/com/kylemayes/generator/support/Command.kt +++ b/generator/src/main/kotlin/com/kylemayes/generator/support/Command.kt @@ -3,6 +3,7 @@ package com.kylemayes.generator.support import mu.KotlinLogging +import java.nio.file.Path import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit @@ -15,17 +16,19 @@ private val log = KotlinLogging.logger { /* */ } fun bindgen(vararg args: String): String = execute("bindgen", arrayOf(*args)) /** Executes the `git` command and prints the output. */ -fun git(vararg args: String) { +fun git(vararg args: String, directory: Path? = null) { println("> git ${args.joinToString(" ")}") - println(execute("git", arrayOf(*args))) + println(execute("git", arrayOf(*args), directory = directory)) } /** Executes the `rustfmt` command and returns the output. */ -fun rustfmt(rust: String): String = execute("rustfmt", emptyArray(), rust) +fun rustfmt(rust: String): String = execute("rustfmt", emptyArray(), input = rust) /** Executes a command (with a time limit) and returns the output. */ -private fun execute(command: String, args: Array, input: String? = null): String { - val process = ProcessBuilder(command, *args).start() +private fun execute(command: String, args: Array, input: String? = null, directory: Path? = null): String { + var builder = ProcessBuilder(command, *args) + if (directory != null) builder = builder.directory(directory.toFile()) + val process = builder.start() val errors = ConcurrentHashMap() val latch = CountDownLatch(4) diff --git a/generator/src/test/kotlin/com/kylemayes/generator/support/CommandTest.kt b/generator/src/test/kotlin/com/kylemayes/generator/support/CommandTest.kt new file mode 100644 index 0000000..09f105c --- /dev/null +++ b/generator/src/test/kotlin/com/kylemayes/generator/support/CommandTest.kt @@ -0,0 +1,26 @@ +package com.kylemayes.generator.support + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File +import java.nio.file.Files + +class CommandTest { + @field:TempDir + lateinit var temp: File + + @Test + fun git() { + val directory = temp.toPath() + val file = directory.resolve("file.txt") + + git("init", directory = directory) + + git("add", "-A", directory = directory) + git("status", directory = directory) + + Files.writeString(file, "Text.") + git("add", "-A", directory = directory) + git("status", directory = directory) + } +}