Skip to content
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
compile("org.apache.maven:maven-core:3.0.3")
compile("org.slf4j:slf4j-nop:1.7.25")

testCompile("com.github.stefanbirkner:system-rules:1.19.0")
testCompile("junit:junit:4.12")
testCompile( "io.kotlintest:kotlintest:2.0.7")
testCompile(kotlin("script-runtime"))
Expand Down
21 changes: 17 additions & 4 deletions src/main/kotlin/kscript/app/Script.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,28 @@ fun Script.collectRepos(): List<MavenRepo> {
.map { keyVal -> keyVal.split(keyValSep).map { it.trim(' ', '\"') }.let{ it.first() to it.last()}}
.toMap()

MavenRepo(annotationParams[0], annotationParams[1], namedArgs.getOrDefault("user", ""), namedArgs.getOrDefault("password", ""))
MavenRepo(annotationParams[0], annotationParams[1], resolveOptionalVariableInString(namedArgs.getOrDefault("user", "")), resolveOptionalVariableInString(namedArgs.getOrDefault("password", "")))
}
}

}

/** If the provided string contains {{something}}, the string will be treated as an environment variable.
* When the variable exists, the string will be replaced with the value of the environment variable. */
private fun resolveOptionalVariableInString(stringWithOptionalVar: String): String {
val varRegex = "\\{\\{(.*)\\}\\}".toRegex()
val usrFind = varRegex.find(stringWithOptionalVar)
if (usrFind != null && usrFind.groupValues.size > 1) {
val varString = usrFind.groupValues[1]
if (System.getenv().containsKey(varString)) {
return System.getenv(varString)
} else {
throw RuntimeException("The specified environment variable '{{$varString}}' is not found.")
}
}
return stringWithOptionalVar // No match, so input string is returned
}

//
// Runtime Configuration
//


/**
Expand Down
28 changes: 27 additions & 1 deletion src/test/kotlin/Tests.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import io.kotlintest.matchers.shouldBe
import io.kotlintest.matchers.shouldThrow
import kscript.app.*
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import java.io.File
import org.junit.contrib.java.lang.system.EnvironmentVariables;

/**
* @author Holger Brandl
*/

class Tests {

@JvmField @Rule
val environmentVariables = EnvironmentVariables()

// "comma separated dependencies should be parsed correctly"
@Test
fun directiveDependencyCollect() {
Expand Down Expand Up @@ -124,6 +129,27 @@ class Tests {

}

@Test
fun customRepoWithCredsFromEnvironment() {
environmentVariables.set("MY_USER_ENV", "my_user")
environmentVariables.set("MY_PASS_ENV", "my_pass")

val validLines = listOf("""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="{{MY_USER_ENV}}", password="{{MY_PASS_ENV}}") """)

with(Script(validLines)) {
collectRepos() shouldBe listOf(MavenRepo("imagej-releases", "http://maven.imagej.net/content/repositories/releases", "my_user", "my_pass"))
}

val invalidLines = listOf("""@file:MavenRepository("imagej-releases", "http://maven.imagej.net/content/repositories/releases", user="{{MY_USER_ENV}}", password="{{MY_PASS_EN}}") """)

with(Script(invalidLines)) {
shouldThrow<RuntimeException> {
collectRepos()
}
}

}


// combine kotlin opts spread over multiple lines
@Test
Expand Down
2 changes: 1 addition & 1 deletion test/test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export -f kscript_nocall
assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/include_variations.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0

## Ensure relative includes with in shebang mode
assert_raises resources/includes/shebang_mode_includes 0
assert_raises "${KSCRIPT_HOME}/test/resources/includes/shebang_mode_includes" 0

## support diamond-shaped include schemes (see #133)
assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/diamond.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0
Expand Down