Skip to content

Commit b113f2b

Browse files
committed
started junit-test suite to complement to cli integration tests
1 parent 39ac3d1 commit b113f2b

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies {
1414
compile "com.offbytwo:docopt:0.6.0.20150202"
1515

1616
testCompile group: 'junit', name: 'junit', version: '4.11'
17-
testCompile "io.kotlintest:kotlintest:1.1.6"
17+
testCompile "io.kotlintest:kotlintest:2.0.0"
1818
}
1919

2020
repositories {

src/main/kotlin/kscript/app/Kscript.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ fun main(args: Array<String>) {
9797
}
9898

9999
// Find all //DEPS directives and concatenate their values
100-
val dependencies = scriptText
101-
.filter { it.startsWith("//DEPS ") }
102-
.flatMap { it.split("[ ;,]+".toRegex()).drop(1) }
103-
.map(String::trim)
100+
val dependencies = collectDependencies(scriptText)
104101

105102

106103
// Create temopary dev environment
@@ -113,7 +110,7 @@ fun main(args: Array<String>) {
113110
val classpath = resolveDependencies(dependencies)
114111

115112
// Extract kotlin arguments
116-
val kotlinOpts = extractKotlinOptions(scriptText)
113+
val kotlinOpts = collectRuntimeOptions(scriptText)
117114

118115

119116
// Optionally enter interactive mode
@@ -222,13 +219,20 @@ fun main(args: Array<String>) {
222219
println("kotlin ${kotlinOpts} -classpath ${jarFile}${CP_SEPARATOR_CHAR}${KOTLIN_HOME}${File.separatorChar}lib${File.separatorChar}kotlin-script-runtime.jar${CP_SEPARATOR_CHAR}${classpath} ${execClassName} ${shiftedArgs} ")
223220
}
224221

222+
fun collectDependencies(scriptText: List<String>): List<String> {
223+
return scriptText
224+
.filter { it.startsWith("//DEPS ") }
225+
.flatMap { it.split("[ ;,]+".toRegex()).drop(1) }
226+
.map(String::trim)
227+
}
228+
225229

226-
private fun extractKotlinOptions(scriptText: List<String>): String {
230+
fun collectRuntimeOptions(scriptText: List<String>): String {
227231
val koptsPrefix = "//KOTLIN_OPTS "
228232

229233
return scriptText.
230234
filter { it.startsWith(koptsPrefix) }.
231-
map { it.replace(koptsPrefix, "") }.
235+
map { it.replace(koptsPrefix, "").trim() }.
232236
joinToString(" ")
233237
}
234238

src/test/kotlin/Tests.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import io.kotlintest.matchers.shouldBe
2+
import kscript.app.collectDependencies
3+
import kscript.app.collectRuntimeOptions
4+
import org.junit.Test
5+
6+
/**
7+
* @author Holger Brandl
8+
*/
9+
10+
class Tests {
11+
12+
// "comma separated dependencies should be parsed correctly"
13+
@Test
14+
fun dependencyCollect() {
15+
val lines = listOf(
16+
"//DEPS de.mpicbg.scicomp.joblist:joblist-kotlin:1.1, de.mpicbg.scicomp:kutils:0.7",
17+
"//DEPS log4j:log4j:1.2.14"
18+
)
19+
20+
val expected = listOf(
21+
"de.mpicbg.scicomp.joblist:joblist-kotlin:1.1",
22+
"de.mpicbg.scicomp:kutils:0.7",
23+
"log4j:log4j:1.2.14"
24+
)
25+
26+
collectDependencies(lines) shouldBe expected
27+
}
28+
29+
// combine kotlin opts spread over multiple lines
30+
@Test
31+
fun optsCollect() {
32+
val lines = listOf(
33+
"//KOTLIN_OPTS -foo 3 'some file.txt'",
34+
"//KOTLIN_OPTS --bar"
35+
)
36+
37+
collectRuntimeOptions(lines) shouldBe "-foo 3 'some file.txt' --bar"
38+
}
39+
40+
41+
@Test
42+
fun foo() {
43+
1 shouldBe 2
44+
}
45+
46+
}

0 commit comments

Comments
 (0)