Skip to content

Commit e25ed2f

Browse files
authored
Improve self-test CLI to auto-remove host trailing slash (#77)
1 parent f4f85bc commit e25ed2f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

self-testing-partner-cli/src/main/scala/com/databricks/labs/DatabricksConfigParser.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ object DatabricksConfigParser {
2727
configurationProfileName: String,
2828
sysEnv: Map[String, String]
2929
): (Option[String], Option[String]) = {
30-
if (useEnvVariables) {
31-
getConfigFromEnvVariables(sysEnv)
32-
} else {
33-
getDatabricksConfigFromProfile(configurationProfileName, sysEnv)
34-
}
30+
val (host, token) = if (useEnvVariables) getConfigFromEnvVariables(sysEnv) else getDatabricksConfigFromProfile(configurationProfileName, sysEnv)
31+
return (host.map(_.stripSuffix("/")), token)
3532
}
3633

3734
/** Finds the value for a field in a line from a Databricks configuration
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[DEFAULT]
22
host = defaulthost
33
token = defaulttoken
4+
5+
[TRAILING_SLASH]
6+
host = https://anotherhost.com/
7+
token = defaulttoken

self-testing-partner-cli/src/test/scala/DatabricksConfigParserSuite.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.scalatest.funsuite.AnyFunSuite
88

99
class DatabricksConfigParserSuite extends AnyFunSuite {
1010
private val testHost = "defaulthost"
11+
private val trailingSlashRemovedHost = "https://anotherhost.com"
1112
private val testToken = "defaulttoken"
1213

1314
test("PROFILE - Successfully gets host/token from profile") {
@@ -46,6 +47,20 @@ class DatabricksConfigParserSuite extends AnyFunSuite {
4647
stream.close()
4748
}
4849

50+
test("PROFILE - Successfully remove host trailing slash from profile") {
51+
val profileName = "TRAILING_SLASH"
52+
val stream = new java.io.ByteArrayOutputStream()
53+
val env =
54+
Map(configFilePathEnvVariable -> getClass.getResource(".testcfg").getPath)
55+
val (host, token) = Console.withErr(stream) {
56+
getHostAndToken(useEnvVariables = false, profileName, env)
57+
}
58+
assert(stream.toString.isEmpty)
59+
assert(host.get == trailingSlashRemovedHost)
60+
assert(token.get == testToken)
61+
stream.close()
62+
}
63+
4964
test(
5065
"ENVIRONMENT VARIABLES - Successfully gets host/token from env variables"
5166
) {
@@ -87,4 +102,16 @@ class DatabricksConfigParserSuite extends AnyFunSuite {
87102
)
88103
stream.close()
89104
}
105+
106+
test("ENVIRONMENT VARIABLES - Successfully remove host trailing slash") {
107+
val env = Map(hostEnvVariable -> s"$trailingSlashRemovedHost/", tokenEnvVariable -> testToken)
108+
val stream = new java.io.ByteArrayOutputStream()
109+
val (host, token) = Console.withOut(stream) {
110+
getHostAndToken(useEnvVariables = true, "default", env)
111+
}
112+
assert(stream.toString.isEmpty)
113+
assert(host.get == trailingSlashRemovedHost)
114+
assert(token.get == testToken)
115+
stream.close()
116+
}
90117
}

0 commit comments

Comments
 (0)