Skip to content

Commit

Permalink
Improve self-test CLI to auto-remove host trailing slash (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyhu-db authored Oct 31, 2024
1 parent f4f85bc commit e25ed2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ object DatabricksConfigParser {
configurationProfileName: String,
sysEnv: Map[String, String]
): (Option[String], Option[String]) = {
if (useEnvVariables) {
getConfigFromEnvVariables(sysEnv)
} else {
getDatabricksConfigFromProfile(configurationProfileName, sysEnv)
}
val (host, token) = if (useEnvVariables) getConfigFromEnvVariables(sysEnv) else getDatabricksConfigFromProfile(configurationProfileName, sysEnv)
return (host.map(_.stripSuffix("/")), token)
}

/** Finds the value for a field in a line from a Databricks configuration
Expand Down
4 changes: 4 additions & 0 deletions self-testing-partner-cli/src/test/resources/.testcfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[DEFAULT]
host = defaulthost
token = defaulttoken

[TRAILING_SLASH]
host = https://anotherhost.com/
token = defaulttoken
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.scalatest.funsuite.AnyFunSuite

class DatabricksConfigParserSuite extends AnyFunSuite {
private val testHost = "defaulthost"
private val trailingSlashRemovedHost = "https://anotherhost.com"
private val testToken = "defaulttoken"

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

test("PROFILE - Successfully remove host trailing slash from profile") {
val profileName = "TRAILING_SLASH"
val stream = new java.io.ByteArrayOutputStream()
val env =
Map(configFilePathEnvVariable -> getClass.getResource(".testcfg").getPath)
val (host, token) = Console.withErr(stream) {
getHostAndToken(useEnvVariables = false, profileName, env)
}
assert(stream.toString.isEmpty)
assert(host.get == trailingSlashRemovedHost)
assert(token.get == testToken)
stream.close()
}

test(
"ENVIRONMENT VARIABLES - Successfully gets host/token from env variables"
) {
Expand Down Expand Up @@ -87,4 +102,16 @@ class DatabricksConfigParserSuite extends AnyFunSuite {
)
stream.close()
}

test("ENVIRONMENT VARIABLES - Successfully remove host trailing slash") {
val env = Map(hostEnvVariable -> s"$trailingSlashRemovedHost/", tokenEnvVariable -> testToken)
val stream = new java.io.ByteArrayOutputStream()
val (host, token) = Console.withOut(stream) {
getHostAndToken(useEnvVariables = true, "default", env)
}
assert(stream.toString.isEmpty)
assert(host.get == trailingSlashRemovedHost)
assert(token.get == testToken)
stream.close()
}
}

0 comments on commit e25ed2f

Please sign in to comment.