Skip to content

Commit

Permalink
[none] test refactoring, attempting to address another ci only test f…
Browse files Browse the repository at this point in the history
…ailure
  • Loading branch information
robertfmurdock committed Sep 21, 2024
1 parent ae441cb commit 968a635
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.zegreatrob.tools.adapter.git

import kotlinx.datetime.Instant

class GitAdapter(private val workingDirectory: String) {
class GitAdapter(private val workingDirectory: String, private val env: Map<String, String> = emptyMap()) {

fun headCommitId(): String = runProcess(
listOf(
Expand All @@ -11,33 +11,48 @@ class GitAdapter(private val workingDirectory: String) {
"rev-parse",
"HEAD",
),
workingDirectory,
).trim()

private fun runProcess(args: List<String>, env: Map<String, String> = emptyMap()) =
runProcess(args, workingDirectory, env.plus(this.env))

fun newAnnotatedTag(name: String, ref: String, userName: String?, userEmail: String?) {
val command = listOf("git") + inlineConfig("user.name", userName) + inlineConfig("user.email", userEmail) +
listOf(
"tag",
"--annotate",
"--message=$name",
name,
ref,
)
runProcess(
command,
workingDirectory,
listOf("git") + inlineConfig("user.name", userName) + inlineConfig("user.email", userEmail) +
listOf(
"tag",
"--annotate",
"--message=$name",
name,
ref,
),
env = emptyMap<String, String>()
.plus(
if (userName != null) {
mapOf("GIT_COMMITTER_NAME" to userName)
} else {
emptyMap()
},
)
.plus(
if (userEmail != null) {
mapOf("GIT_COMMITTER_EMAIL" to userEmail)
} else {
emptyMap()
},
),
)
}

private fun inlineConfig(property: String, value: String?) =
if (value != null) listOf("-c", "$property=$value") else emptyList()

fun pushTags() {
runProcess(listOf("git", "push", "--tags"), workingDirectory)
runProcess(listOf("git", "push", "--tags"))
}

fun listTags(): List<TagRef> {
val outputText = runProcess(
fun listTags(): List<TagRef> = parseTagRefs(
runProcess(
listOf(
"git",
"--no-pager",
Expand All @@ -46,10 +61,8 @@ class GitAdapter(private val workingDirectory: String) {
"--sort=-taggerdate",
"--format=%(refname:strip=2),%(*objectname),%(creatordate:iso-strict)",
),
workingDirectory,
)
return parseTagRefs(outputText)
}
),
)

private fun parseTagRefs(outputText: String): List<TagRef> {
val output = outputText.split("\n").mapNotNull {
Expand All @@ -75,7 +88,6 @@ class GitAdapter(private val workingDirectory: String) {
"log",
"--format=$gitLogFormat",
),
workingDirectory,
),
)

Expand All @@ -88,7 +100,6 @@ class GitAdapter(private val workingDirectory: String) {
"--format=%(refname:strip=2),%(*objectname),%(creatordate:iso-strict)",
"--points-at=$ref",
),
workingDirectory,
).let(::parseTagRefs)
.firstOrNull()

Expand All @@ -101,7 +112,6 @@ class GitAdapter(private val workingDirectory: String) {
"--format=$gitLogFormat",
"--no-patch",
),
workingDirectory,
),
).firstOrNull()

Expand All @@ -114,7 +124,6 @@ class GitAdapter(private val workingDirectory: String) {
"--format=$gitLogFormat",
begin,
) + if (end != null) listOf(end) else emptyList(),
workingDirectory,
),
)

Expand Down Expand Up @@ -144,7 +153,6 @@ class GitAdapter(private val workingDirectory: String) {
"--branch",
"--ahead-behind",
),
workingDirectory,
)
.let { output ->
val lines = output.split("\n")
Expand All @@ -170,20 +178,20 @@ class GitAdapter(private val workingDirectory: String) {
find { it.startsWith(prefix) }?.substring(prefix.length)

fun describe(abbrev: Int): String? = runCatching {
runProcess(listOf("git", "describe", "--abbrev=$abbrev"), workingDirectory)
runProcess(listOf("git", "describe", "--abbrev=$abbrev"))
.trim()
}.getOrNull()

fun init() {
runProcess(listOf("git", "init"), workingDirectory)
runProcess(listOf("git", "init"))
}

fun config(name: String, value: String) {
runProcess(listOf("git", "config", name, value), workingDirectory)
runProcess(listOf("git", "config", name, value))
}

fun add(vararg files: String) {
runProcess(listOf("git", "add") + files, workingDirectory)
runProcess(listOf("git", "add") + files)
}

fun commit(
Expand All @@ -206,24 +214,22 @@ class GitAdapter(private val workingDirectory: String) {
"GIT_COMMITTER_NAME" to committerName,
"GIT_COMMITTER_EMAIL" to committerEmail,
),
workingDirectory = workingDirectory,
)
}

fun addRemote(name: String, url: String) {
runProcess(listOf("git", "remote", "add", name, url), workingDirectory)
runProcess(listOf("git", "remote", "add", name, url))
}

fun fetch() {
runProcess(listOf("git", "fetch"), workingDirectory)
runProcess(listOf("git", "fetch"))
}

fun merge(branch: String, noCommit: Boolean, ffOnly: Boolean) {
runProcess(
listOf("git", "merge") + inlineFlag("--no-commit", noCommit) + inlineFlag("--ff-only", ffOnly) + listOf(
branch,
),
workingDirectory,
)
}

Expand All @@ -234,7 +240,6 @@ class GitAdapter(private val workingDirectory: String) {
listOf("git", "checkout") + (if (newBranch) listOf("-b") else emptyList()) + listOf(
branch,
),
workingDirectory,
)
}

Expand All @@ -257,19 +262,10 @@ class GitAdapter(private val workingDirectory: String) {
} else {
emptyList()
},
workingDirectory,
)
}

fun setBranchUpstream(upstream: String, branch: String) {
runProcess(listOf("git", "branch", "--set-upstream-to=$upstream", branch), workingDirectory)
runProcess(listOf("git", "branch", "--set-upstream-to=$upstream", branch))
}
}

data class GitStatus(
val isClean: Boolean,
val ahead: Int,
val behind: Int,
val head: String,
val upstream: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.zegreatrob.tools.adapter.git

data class GitStatus(
val isClean: Boolean,
val ahead: Int,
val behind: Int,
val head: String,
val upstream: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ fun initializeGitRepo(
commits: List<String> = listOf(),
initialTag: String? = null,
): GitAdapter {
val gitAdapter = GitAdapter(directory)
val gitAdapter = GitAdapter(
directory,
mapOf(
"GIT_CONFIG_GLOBAL" to (System.getenv("GIT_CONFIG_GLOBAL") ?: ""),
"GIT_CONFIG_SYSTEM" to (System.getenv("GIT_CONFIG_SYSTEM") ?: ""),
),
)
gitAdapter.init()
gitAdapter.config("commit.gpgsign", "false")
gitAdapter.config("user.useConfigOnly", "true")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.zegreatrob.tools.adapter.git.GitAdapter
import com.zegreatrob.tools.test.git.addCommitWithMessage
import com.zegreatrob.tools.test.git.initializeGitRepo
import java.io.File
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
Expand Down Expand Up @@ -35,6 +36,20 @@ interface CalculateVersionTestSpec {
commits = commits,
)

@BeforeTest
fun checkPrerequisites() {
assertEquals(
"/dev/null",
System.getenv("GIT_CONFIG_GLOBAL"),
"Ensure this is set for the test to work as intended",
)
assertEquals(
"/dev/null",
System.getenv("GIT_CONFIG_SYSTEM"),
"Ensure this is set for the test to work as intended",
)
}

fun execute(): TestResult
fun runCalculateVersionSuccessfully(): String =
when (val result = execute()) {
Expand Down

0 comments on commit 968a635

Please sign in to comment.