Skip to content

Commit 6c17f45

Browse files
committed
Added tests for git url transforms
1 parent 935a9aa commit 6c17f45

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

invert-gradle-plugin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ dependencies {
3838

3939
implementation(libs.kotlinx.serialization.json)
4040
implementation(libs.kotlinx.coroutines.core)
41+
42+
testImplementation(libs.kotlin.test)
4143
}
4244

4345
kotlin {

invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/GitDataCollector.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import java.io.IOException
1111
*/
1212
internal object GitDataCollector {
1313

14+
private const val GIT_EXTENSION = ".git"
15+
1416
fun currentBranch(): GitBranch {
1517
return exec("git rev-parse --abbrev-ref HEAD").stdOut.lines()[0]
1618
}
@@ -32,16 +34,19 @@ internal object GitDataCollector {
3234
it
3335
}
3436
}.map {
35-
val gitExtension = ".git"
36-
if (it.endsWith(gitExtension)) {
37-
it.substring(0, it.length - gitExtension.length)
37+
if (it.endsWith(GIT_EXTENSION)) {
38+
it.substringBeforeLast(GIT_EXTENSION)
3839
} else {
3940
it
4041
}
4142
}.map {
42-
buildString {
43-
append("https://")
44-
append(it.replace(":", "/"))
43+
if (!it.startsWith("https://")) {
44+
buildString {
45+
append("https://")
46+
append(it.replace(":", "/"))
47+
}
48+
} else {
49+
it
4550
}
4651
}[0]
4752
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.squareup.invert.internal
2+
3+
import com.squareup.invert.internal.exec
4+
import com.squareup.invert.models.GitBranch
5+
import com.squareup.invert.models.GitSha
6+
import org.gradle.api.GradleException
7+
import org.gradle.api.logging.Logger
8+
import org.junit.Test
9+
import java.io.IOException
10+
import kotlin.test.assertEquals
11+
12+
/**
13+
* Executes `git` commands and parses the output.
14+
*/
15+
class GitDataCollectorTest {
16+
17+
@Test
18+
fun testOrgAtGit() {
19+
val actual = GitDataCollector.remoteRepoGitUrlToHttps(
20+
"org-123@github.com:company/my-repo.git"
21+
)
22+
assertEquals(
23+
expected = "https://github.com/company/my-repo",
24+
actual = actual
25+
)
26+
}
27+
28+
@Test
29+
fun testHttpsDotGit() {
30+
val actual = GitDataCollector.remoteRepoGitUrlToHttps(
31+
"git@github.com:square/invert.git"
32+
)
33+
assertEquals(
34+
expected = "https://github.com/square/invert",
35+
actual = actual
36+
)
37+
}
38+
39+
40+
@Test
41+
fun testHttps() {
42+
val actual = GitDataCollector.remoteRepoGitUrlToHttps(
43+
"https://github.com/company/my-repo"
44+
)
45+
assertEquals(
46+
expected = "https://github.com/company/my-repo",
47+
actual = actual
48+
)
49+
}
50+
}

0 commit comments

Comments
 (0)