Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for calling git from generator #240

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>, input: String? = null): String {
val process = ProcessBuilder(command, *args).start()
private fun execute(command: String, args: Array<String>, 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<String, Throwable>()
val latch = CountDownLatch(4)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading